Skip to content

Commit

Permalink
Refactor RealmAppCredentialsHandle (now CredentialsHandle)
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsenko committed Apr 15, 2024
1 parent 1d474d4 commit 5ca0803
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 120 deletions.
24 changes: 12 additions & 12 deletions packages/realm_dart/lib/src/credentials.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,44 +57,44 @@ extension AuthProviderTypeInternal on AuthProviderType {
/// A class, representing the credentials used for authenticating a [User]
/// {@category Application}
class Credentials implements Finalizable {
final RealmAppCredentialsHandle _handle;
final CredentialsHandle _handle;

/// Returns a [Credentials] object that can be used to authenticate an anonymous user.
/// Setting [reuseCredentials] to `false` will create a new anonymous user, upon [App.logIn].
/// [Anonymous Authentication Docs](https://www.mongodb.com/docs/atlas/app-services/authentication/anonymous/#anonymous-authentication)
Credentials.anonymous({bool reuseCredentials = true}) : _handle = realmCore.createAppCredentialsAnonymous(reuseCredentials);
Credentials.anonymous({bool reuseCredentials = true}) : _handle = CredentialsHandle.anonymous(reuseCredentials);

/// Returns a [Credentials] object that can be used to authenticate a user with a Google account using an id token.
Credentials.apple(String idToken) : _handle = realmCore.createAppCredentialsApple(idToken);
Credentials.apple(String idToken) : _handle = CredentialsHandle.apple(idToken);

/// Returns a [Credentials] object that can be used to authenticate a user with their email and password.
/// A user can login with email and password only after they have registered their account and verified their
/// email.
/// [Email/Password Authentication Docs](https://www.mongodb.com/docs/atlas/app-services/authentication/email-password/#email-password-authentication)
Credentials.emailPassword(String email, String password) : _handle = realmCore.createAppCredentialsEmailPassword(email, password);
Credentials.emailPassword(String email, String password) : _handle = CredentialsHandle.emailPassword(email, password);

/// Returns a [Credentials] object that can be used to authenticate a user with a custom JWT.
/// [Custom-JWT Authentication Docs](https://www.mongodb.com/docs/atlas/app-services/authentication/custom-jwt/#custom-jwt-authentication)
Credentials.jwt(String token) : _handle = realmCore.createAppCredentialsJwt(token);
Credentials.jwt(String token) : _handle = CredentialsHandle.jwt(token);

/// Returns a [Credentials] object that can be used to authenticate a user with a Facebook account.
Credentials.facebook(String accessToken) : _handle = realmCore.createAppCredentialsFacebook(accessToken);
Credentials.facebook(String accessToken) : _handle = CredentialsHandle.facebook(accessToken);

/// Returns a [Credentials] object that can be used to authenticate a user with a Google account using an authentication code.
Credentials.googleAuthCode(String authCode) : _handle = realmCore.createAppCredentialsGoogleAuthCode(authCode);
Credentials.googleAuthCode(String authCode) : _handle = CredentialsHandle.googleAuthCode(authCode);

/// Returns a [Credentials] object that can be used to authenticate a user with a Google account using an id token.
Credentials.googleIdToken(String idToken) : _handle = realmCore.createAppCredentialsGoogleIdToken(idToken);
Credentials.googleIdToken(String idToken) : _handle = CredentialsHandle.googleIdToken(idToken);

/// Returns a [Credentials] object that can be used to authenticate a user with a custom Function.
/// [Custom Function Authentication Docs](https://www.mongodb.com/docs/atlas/app-services/authentication/custom-function/)
Credentials.function(String payload) : _handle = realmCore.createAppCredentialsFunction(payload);
Credentials.function(String payload) : _handle = CredentialsHandle.function(payload);

/// Returns a [Credentials] object that can be used to authenticate a user with an API key.
/// To generate an API key, use [ApiKeyClient.create] or the App Services web UI.
Credentials.apiKey(String key) : _handle = realmCore.createAppCredentialsApiKey(key);
Credentials.apiKey(String key) : _handle = CredentialsHandle.apiKey(key);

AuthProviderType get provider => realmCore.userGetCredentialsProviderType(this);
AuthProviderType get provider => handle.providerType;
}

/// @nodoc
Expand All @@ -104,7 +104,7 @@ extension CredentialsInternal on Credentials {
_handle.keepAlive();
}

RealmAppCredentialsHandle get handle => _handle;
CredentialsHandle get handle => _handle;
}

/// A class, encapsulating functionality for users, logged in with [Credentials.emailPassword()].
Expand Down
6 changes: 3 additions & 3 deletions packages/realm_dart/lib/src/map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -262,19 +262,19 @@ extension RealmMapInternal<T extends Object?> on RealmMap<T> {
throw RealmError("Can't add to map an embedded object that is already managed");
}

final objHandle = realmCore.mapInsertEmbeddedObject(realm, handle, key);
final objHandle = handle.insertEmbedded(key);
realm.manageEmbedded(objHandle, value);
return;
}

if (value is RealmValue && value.type.isCollection) {
realmCore.mapInsertCollection(handle, realm, key, value);
handle.insertCollection(realm, key, value);
return;
}

realm.addUnmanagedRealmObjectFromValue(value, update);

realmCore.mapInsertValue(handle, key, value);
handle.insert(key, value);
} on Exception catch (e) {
throw RealmException("Error setting value at key $key. Error: $e");
}
Expand Down
75 changes: 75 additions & 0 deletions packages/realm_dart/lib/src/native/credentials_handle.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2024 MongoDB, Inc.
// SPDX-License-Identifier: Apache-2.0

part of 'realm_core.dart';

class CredentialsHandle extends HandleBase<realm_app_credentials> {
CredentialsHandle._(Pointer<realm_app_credentials> pointer) : super(pointer, 16);

factory CredentialsHandle.anonymous(bool reuseCredentials) {
return CredentialsHandle._(realmLib.realm_app_credentials_new_anonymous(reuseCredentials));
}

factory CredentialsHandle.emailPassword(String email, String password) {
return using((arena) {
final emailPtr = email.toCharPtr(arena);
final passwordPtr = password.toRealmString(arena);
return CredentialsHandle._(realmLib.realm_app_credentials_new_email_password(emailPtr, passwordPtr.ref));
});
}

factory CredentialsHandle.jwt(String token) {
return using((arena) {
final tokenPtr = token.toCharPtr(arena);
return CredentialsHandle._(realmLib.realm_app_credentials_new_jwt(tokenPtr));
});
}

factory CredentialsHandle.apple(String idToken) {
return using((arena) {
final idTokenPtr = idToken.toCharPtr(arena);
return CredentialsHandle._(realmLib.realm_app_credentials_new_apple(idTokenPtr));
});
}

factory CredentialsHandle.facebook(String accessToken) {
return using((arena) {
final accessTokenPtr = accessToken.toCharPtr(arena);
return CredentialsHandle._(realmLib.realm_app_credentials_new_facebook(accessTokenPtr));
});
}

factory CredentialsHandle.googleIdToken(String idToken) {
return using((arena) {
final idTokenPtr = idToken.toCharPtr(arena);
return CredentialsHandle._(realmLib.realm_app_credentials_new_google_id_token(idTokenPtr));
});
}

factory CredentialsHandle.googleAuthCode(String authCode) {
return using((arena) {
final authCodePtr = authCode.toCharPtr(arena);
return CredentialsHandle._(realmLib.realm_app_credentials_new_google_auth_code(authCodePtr));
});
}

factory CredentialsHandle.function(String payload) {
return using((arena) {
final payloadPtr = payload.toCharPtr(arena);
final credentialsPtr = invokeGetPointer(() => realmLib.realm_app_credentials_new_function(payloadPtr));
return CredentialsHandle._(credentialsPtr);
});
}

factory CredentialsHandle.apiKey(String key) {
return using((arena) {
final keyPtr = key.toCharPtr(arena);
return CredentialsHandle._(realmLib.realm_app_credentials_new_api_key(keyPtr));
});
}

AuthProviderType get providerType {
final provider = realmLib.realm_auth_credentials_get_provider(pointer);
return AuthProviderTypeInternal.getByValue(provider);
}
}
16 changes: 8 additions & 8 deletions packages/realm_dart/lib/src/native/map_handle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,21 @@ class MapHandle extends CollectionHandleBase<realm_dictionary> {

bool containsValue(Object? value) => indexOf(value) > -1;

ObjectHandle mapInsertEmbeddedObject(Realm realm, MapHandle handle, String key) {
ObjectHandle insertEmbedded(String key) {
return using((Arena arena) {
final keyNative = _toRealmValue(key, arena);
final ptr = invokeGetPointer(() => realmLib.realm_dictionary_insert_embedded(handle.pointer, keyNative.ref));
return ObjectHandle._(ptr, realm.handle);
final ptr = invokeGetPointer(() => realmLib.realm_dictionary_insert_embedded(pointer, keyNative.ref));
return ObjectHandle._(ptr, _root);
});
}

void mapInsertValue(MapHandle handle, String key, Object? value) {
void insert(String key, Object? value) {
using((Arena arena) {
final keyNative = _toRealmValue(key, arena);
final valueNative = _toRealmValue(value, arena);
invokeGetBool(
() => realmLib.realm_dictionary_insert(
handle.pointer,
pointer,
keyNative.ref,
valueNative.ref,
nullptr,
Expand All @@ -108,14 +108,14 @@ class MapHandle extends CollectionHandleBase<realm_dictionary> {
});
}

void mapInsertCollection(MapHandle handle, Realm realm, String key, RealmValue value) {
void insertCollection(Realm realm, String key, RealmValue value) {
using((Arena arena) {
final keyNative = _toRealmValue(key, arena);
_createCollection(
realm,
value,
() => realmLib.realm_dictionary_insert_list(handle.pointer, keyNative.ref),
() => realmLib.realm_dictionary_insert_dictionary(handle.pointer, keyNative.ref),
() => realmLib.realm_dictionary_insert_list(pointer, keyNative.ref),
() => realmLib.realm_dictionary_insert_dictionary(pointer, keyNative.ref),
);
});
}
Expand Down
98 changes: 2 additions & 96 deletions packages/realm_dart/lib/src/native/realm_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ import 'handle_base.dart';
import 'realm_bindings.dart';
import 'realm_library.dart';

// TODO: Use regular
// TODO: Use regular imports
part 'app_handle.dart';
part 'config_handle.dart';
part 'convert.dart';
part 'credentials_handle.dart';
part 'decimal128.dart';
part 'error_handling.dart';
part 'list_handle.dart';
Expand Down Expand Up @@ -902,30 +903,6 @@ class _RealmCore {
return SetHandle._(pointer, object.realm.handle);
}

ObjectHandle mapInsertEmbeddedObject(Realm realm, MapHandle handle, String key) {
return using((Arena arena) {
final realm_value = _toRealmValue(key, arena);
final ptr = invokeGetPointer(() => realmLib.realm_dictionary_insert_embedded(handle.pointer, realm_value.ref));
return ObjectHandle._(ptr, realm.handle);
});
}

void mapInsertValue(MapHandle handle, String key, Object? value) {
using((Arena arena) {
final key_value = _toRealmValue(key, arena);
final value_value = _toRealmValue(value, arena);
invokeGetBool(() => realmLib.realm_dictionary_insert(handle.pointer, key_value.ref, value_value.ref, nullptr, nullptr));
});
}

void mapInsertCollection(MapHandle handle, Realm realm, String key, RealmValue value) {
using((Arena arena) {
final key_value = _toRealmValue(key, arena);
_createCollection(realm, value, () => realmLib.realm_dictionary_insert_list(handle.pointer, key_value.ref),
() => realmLib.realm_dictionary_insert_dictionary(handle.pointer, key_value.ref));
});
}

MapHandle getMapProperty(RealmObjectBase object, int propertyKey) {
final pointer = invokeGetPointer(() => realmLib.realm_get_dictionary(object.handle.pointer, propertyKey));
return MapHandle._(pointer, object.realm.handle);
Expand Down Expand Up @@ -1010,68 +987,6 @@ class _RealmCore {
});
}

RealmAppCredentialsHandle createAppCredentialsAnonymous(bool reuseCredentials) {
return RealmAppCredentialsHandle._(realmLib.realm_app_credentials_new_anonymous(reuseCredentials));
}

RealmAppCredentialsHandle createAppCredentialsEmailPassword(String email, String password) {
return using((arena) {
final emailPtr = email.toCharPtr(arena);
final passwordPtr = password.toRealmString(arena);
return RealmAppCredentialsHandle._(realmLib.realm_app_credentials_new_email_password(emailPtr, passwordPtr.ref));
});
}

RealmAppCredentialsHandle createAppCredentialsJwt(String token) {
return using((arena) {
final tokenPtr = token.toCharPtr(arena);
return RealmAppCredentialsHandle._(realmLib.realm_app_credentials_new_jwt(tokenPtr));
});
}

RealmAppCredentialsHandle createAppCredentialsApple(String idToken) {
return using((arena) {
final idTokenPtr = idToken.toCharPtr(arena);
return RealmAppCredentialsHandle._(realmLib.realm_app_credentials_new_apple(idTokenPtr));
});
}

RealmAppCredentialsHandle createAppCredentialsFacebook(String accessToken) {
return using((arena) {
final accessTokenPtr = accessToken.toCharPtr(arena);
return RealmAppCredentialsHandle._(realmLib.realm_app_credentials_new_facebook(accessTokenPtr));
});
}

RealmAppCredentialsHandle createAppCredentialsGoogleIdToken(String idToken) {
return using((arena) {
final idTokenPtr = idToken.toCharPtr(arena);
return RealmAppCredentialsHandle._(realmLib.realm_app_credentials_new_google_id_token(idTokenPtr));
});
}

RealmAppCredentialsHandle createAppCredentialsGoogleAuthCode(String authCode) {
return using((arena) {
final authCodePtr = authCode.toCharPtr(arena);
return RealmAppCredentialsHandle._(realmLib.realm_app_credentials_new_google_auth_code(authCodePtr));
});
}

RealmAppCredentialsHandle createAppCredentialsFunction(String payload) {
return using((arena) {
final payloadPtr = payload.toCharPtr(arena);
final credentialsPtr = invokeGetPointer(() => realmLib.realm_app_credentials_new_function(payloadPtr));
return RealmAppCredentialsHandle._(credentialsPtr);
});
}

RealmAppCredentialsHandle createAppCredentialsApiKey(String key) {
return using((arena) {
final keyPtr = key.toCharPtr(arena);
return RealmAppCredentialsHandle._(realmLib.realm_app_credentials_new_api_key(keyPtr));
});
}

void logMessage(LogCategory category, LogLevel logLevel, String message) {
return using((arena) {
realmLib.realm_dart_log(logLevel.index, category.toString().toCharPtr(arena), message.toCharPtr(arena));
Expand All @@ -1094,11 +1009,6 @@ class _RealmCore {
realmLib.realm_clear_cached_apps();
}

AuthProviderType userGetCredentialsProviderType(Credentials credentials) {
final provider = realmLib.realm_auth_credentials_get_provider(credentials.handle.pointer);
return AuthProviderTypeInternal.getByValue(provider);
}

RealmSyncSessionConnectionStateNotificationTokenHandle sessionRegisterProgressNotifier(
Session session, ProgressDirection direction, ProgressMode mode, SessionProgressNotificationsController controller) {
final isStreaming = mode == ProgressMode.reportIndefinitely;
Expand Down Expand Up @@ -1311,10 +1221,6 @@ class RealmObjectChangesHandle extends HandleBase<realm_object_changes> {
RealmObjectChangesHandle._(Pointer<realm_object_changes> pointer) : super(pointer, 256);
}

class RealmAppCredentialsHandle extends HandleBase<realm_app_credentials> {
RealmAppCredentialsHandle._(Pointer<realm_app_credentials> pointer) : super(pointer, 16);
}

class RealmAsyncOpenTaskHandle extends HandleBase<realm_async_open_task_t> {
RealmAsyncOpenTaskHandle._(Pointer<realm_async_open_task_t> pointer) : super(pointer, 32);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/realm_dart/lib/src/native/user_handle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class UserHandle extends HandleBase<realm_user> {
return customDataPtr.cast<Utf8>().toRealmDartString(freeRealmMemory: true, treatEmptyAsNull: true);
}

Future<UserHandle> linkCredentials(AppHandle app, RealmAppCredentialsHandle credentials) {
Future<UserHandle> linkCredentials(AppHandle app, CredentialsHandle credentials) {
final completer = Completer<UserHandle>();
invokeGetBool(
() => realmLib.realm_app_link_user(
Expand Down

0 comments on commit 5ca0803

Please sign in to comment.