From fb99c2bd429443bdc9b577ec7c8f3242335d0048 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Overg=C3=A5rd=20Nielsen?= Date: Wed, 10 Aug 2022 13:39:21 +0200 Subject: [PATCH 1/3] Update realm-core to v12.5.0 --- CHANGELOG.md | 3 ++ lib/src/credentials.dart | 12 +++-- lib/src/native/realm_bindings.dart | 78 ++++++++++++++++++------------ lib/src/native/realm_core.dart | 23 ++++++--- src/realm-core | 2 +- 5 files changed, 75 insertions(+), 43 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 834d743e5..826f4d6d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,9 +10,12 @@ * Support `Configuration.defaultStoragePath ` for getting the platform specific storage paths. ([#665](https://github.com/realm/realm-dart/pull/665)) * Support `App.deleteUser ` for deleting user accounts. ([#679](https://github.com/realm/realm-dart/pull/679)) * Support Apple, Facebook and Google authentication. ([#740](https://github.com/realm/realm-dart/pull/740)) +* Allow multiple anonymous sessions. When using anonymous authentication you can now easily log in with a different anonymous user than last time. ([#750](https://github.com/realm/realm-dart/pull/750)). ### Internal * Added a command to `realm_dart` for deleting Atlas App Services applications. Usage: `dart run realm_dart delete-apps`. By default it will delete apps from `http://localhost:9090` which is the endpoint of the local docker image. If `--atlas-cluster` is provided, it will authenticate, delete the application from the provided cluster. (PR [#663](https://github.com/realm/realm-dart/pull/663)) +* Uses Realm Core v12.5.0 + ## 0.3.1+beta (2022-06-07) diff --git a/lib/src/credentials.dart b/lib/src/credentials.dart index 08b86084f..0531b3aac 100644 --- a/lib/src/credentials.dart +++ b/lib/src/credentials.dart @@ -28,6 +28,9 @@ enum AuthProviderType { /// For authenticating without credentials. anonymous, + /// For authenticating without credentials. + anonymousNoReuse, + /// Authenticate with Apple Id apple, @@ -36,7 +39,7 @@ enum AuthProviderType { /// Authenticate with Google account google, - + _custom, /// For authenticating with an email and a password. @@ -54,10 +57,11 @@ class Credentials { final AuthProviderType provider; /// 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://docs.mongodb.com/realm/authentication/anonymous) - Credentials.anonymous() - : _handle = realmCore.createAppCredentialsAnonymous(), - provider = AuthProviderType.anonymous; + Credentials.anonymous({bool reuseCredentials = true}) + : _handle = realmCore.createAppCredentialsAnonymous(reuseCredentials), + provider = reuseCredentials ? AuthProviderType.anonymous : AuthProviderType.anonymousNoReuse; /// Returns a [Credentials] object that can be used to authenticate a user with a Google account using an id token. Credentials.apple(String idToken) diff --git a/lib/src/native/realm_bindings.dart b/lib/src/native/realm_bindings.dart index 96a470017..0a190ad98 100644 --- a/lib/src/native/realm_bindings.dart +++ b/lib/src/native/realm_bindings.dart @@ -320,16 +320,21 @@ class RealmLibrary { ffi.Pointer Function(ffi.Pointer, ffi.Pointer)>(); - ffi.Pointer realm_app_credentials_new_anonymous() { - return _realm_app_credentials_new_anonymous(); + ffi.Pointer realm_app_credentials_new_anonymous( + bool reuse_credentials, + ) { + return _realm_app_credentials_new_anonymous( + reuse_credentials, + ); } late final _realm_app_credentials_new_anonymousPtr = _lookup< - ffi.NativeFunction Function()>>( - 'realm_app_credentials_new_anonymous'); + ffi.NativeFunction< + ffi.Pointer Function( + ffi.Bool)>>('realm_app_credentials_new_anonymous'); late final _realm_app_credentials_new_anonymous = _realm_app_credentials_new_anonymousPtr - .asFunction Function()>(); + .asFunction Function(bool)>(); ffi.Pointer realm_app_credentials_new_apple( ffi.Pointer id_token, @@ -6157,7 +6162,7 @@ class RealmLibrary { ffi.Pointer arg0, ffi.Pointer query_string, int num_args, - ffi.Pointer args, + ffi.Pointer args, ) { return _realm_query_append_query( arg0, @@ -6173,11 +6178,11 @@ class RealmLibrary { ffi.Pointer, ffi.Pointer, ffi.Size, - ffi.Pointer)>>('realm_query_append_query'); + ffi.Pointer)>>('realm_query_append_query'); late final _realm_query_append_query = _realm_query_append_queryPtr.asFunction< ffi.Pointer Function(ffi.Pointer, - ffi.Pointer, int, ffi.Pointer)>(); + ffi.Pointer, int, ffi.Pointer)>(); /// Count the number of objects found by this query. bool realm_query_count( @@ -6301,7 +6306,7 @@ class RealmLibrary { int target_table, ffi.Pointer query_string, int num_args, - ffi.Pointer args, + ffi.Pointer args, ) { return _realm_query_parse( arg0, @@ -6319,10 +6324,10 @@ class RealmLibrary { realm_class_key_t, ffi.Pointer, ffi.Size, - ffi.Pointer)>>('realm_query_parse'); + ffi.Pointer)>>('realm_query_parse'); late final _realm_query_parse = _realm_query_parsePtr.asFunction< ffi.Pointer Function(ffi.Pointer, int, - ffi.Pointer, int, ffi.Pointer)>(); + ffi.Pointer, int, ffi.Pointer)>(); /// Parse a query string and bind it to a list. /// @@ -6340,7 +6345,7 @@ class RealmLibrary { ffi.Pointer target_list, ffi.Pointer query_string, int num_args, - ffi.Pointer args, + ffi.Pointer args, ) { return _realm_query_parse_for_list( target_list, @@ -6356,11 +6361,11 @@ class RealmLibrary { ffi.Pointer, ffi.Pointer, ffi.Size, - ffi.Pointer)>>('realm_query_parse_for_list'); + ffi.Pointer)>>('realm_query_parse_for_list'); late final _realm_query_parse_for_list = _realm_query_parse_for_listPtr.asFunction< ffi.Pointer Function(ffi.Pointer, - ffi.Pointer, int, ffi.Pointer)>(); + ffi.Pointer, int, ffi.Pointer)>(); /// Parse a query string and bind it to another query result. /// @@ -6379,7 +6384,7 @@ class RealmLibrary { ffi.Pointer target_results, ffi.Pointer query_string, int num_args, - ffi.Pointer args, + ffi.Pointer args, ) { return _realm_query_parse_for_results( target_results, @@ -6390,16 +6395,17 @@ class RealmLibrary { } late final _realm_query_parse_for_resultsPtr = _lookup< - ffi.NativeFunction< - ffi.Pointer Function( - ffi.Pointer, - ffi.Pointer, - ffi.Size, - ffi.Pointer)>>('realm_query_parse_for_results'); + ffi.NativeFunction< + ffi.Pointer Function( + ffi.Pointer, + ffi.Pointer, + ffi.Size, + ffi.Pointer)>>( + 'realm_query_parse_for_results'); late final _realm_query_parse_for_results = _realm_query_parse_for_resultsPtr.asFunction< ffi.Pointer Function(ffi.Pointer, - ffi.Pointer, int, ffi.Pointer)>(); + ffi.Pointer, int, ffi.Pointer)>(); /// Refresh the view of the realm file. /// @@ -9881,14 +9887,15 @@ typedef realm_async_open_task_t = realm_async_open_task; abstract class realm_auth_provider { static const int RLM_AUTH_PROVIDER_ANONYMOUS = 0; - static const int RLM_AUTH_PROVIDER_FACEBOOK = 1; - static const int RLM_AUTH_PROVIDER_GOOGLE = 2; - static const int RLM_AUTH_PROVIDER_APPLE = 3; - static const int RLM_AUTH_PROVIDER_CUSTOM = 4; - static const int RLM_AUTH_PROVIDER_EMAIL_PASSWORD = 5; - static const int RLM_AUTH_PROVIDER_FUNCTION = 6; - static const int RLM_AUTH_PROVIDER_USER_API_KEY = 7; - static const int RLM_AUTH_PROVIDER_SERVER_API_KEY = 8; + static const int RLM_AUTH_PROVIDER_ANONYMOUS_NO_REUSE = 1; + static const int RLM_AUTH_PROVIDER_FACEBOOK = 2; + static const int RLM_AUTH_PROVIDER_GOOGLE = 3; + static const int RLM_AUTH_PROVIDER_APPLE = 4; + static const int RLM_AUTH_PROVIDER_CUSTOM = 5; + static const int RLM_AUTH_PROVIDER_EMAIL_PASSWORD = 6; + static const int RLM_AUTH_PROVIDER_FUNCTION = 7; + static const int RLM_AUTH_PROVIDER_USER_API_KEY = 8; + static const int RLM_AUTH_PROVIDER_SERVER_API_KEY = 9; } class realm_binary extends ffi.Struct { @@ -10349,6 +10356,17 @@ abstract class realm_property_type { class realm_query extends ffi.Opaque {} +class realm_query_arg extends ffi.Struct { + @ffi.Size() + external int nb_args; + + @ffi.Bool() + external bool is_list; + + external ffi.Pointer arg; +} + +typedef realm_query_arg_t = realm_query_arg; typedef realm_query_t = realm_query; class realm_results extends ffi.Opaque {} diff --git a/lib/src/native/realm_core.dart b/lib/src/native/realm_core.dart index 10a5cfbcf..6cf72cf70 100644 --- a/lib/src/native/realm_core.dart +++ b/lib/src/native/realm_core.dart @@ -586,9 +586,9 @@ class _RealmCore { RealmResultsHandle queryClass(Realm realm, int classKey, String query, List args) { return using((arena) { final length = args.length; - final argsPointer = arena(length); + final argsPointer = arena(length); for (var i = 0; i < length; ++i) { - _intoRealmValue(args[i], argsPointer.elementAt(i), arena); + _intoRealmQueryArg(args[i], argsPointer.elementAt(i), arena); } final queryHandle = RealmQueryHandle._(_realmLib.invokeGetPointer( () => _realmLib.realm_query_parse( @@ -606,9 +606,9 @@ class _RealmCore { RealmResultsHandle queryResults(RealmResults target, String query, List args) { return using((arena) { final length = args.length; - final argsPointer = arena(length); + final argsPointer = arena(length); for (var i = 0; i < length; ++i) { - _intoRealmValue(args[i], argsPointer.elementAt(i), arena); + _intoRealmQueryArg(args[i], argsPointer.elementAt(i), arena); } final queryHandle = RealmQueryHandle._(_realmLib.invokeGetPointer( () => _realmLib.realm_query_parse_for_results( @@ -630,9 +630,9 @@ class _RealmCore { RealmResultsHandle queryList(RealmList target, String query, List args) { return using((arena) { final length = args.length; - final argsPointer = arena(length); + final argsPointer = arena(length); for (var i = 0; i < length; ++i) { - _intoRealmValue(args[i], argsPointer.elementAt(i), arena); + _intoRealmQueryArg(args[i], argsPointer.elementAt(i), arena); } final queryHandle = RealmQueryHandle._(_realmLib.invokeGetPointer( () => _realmLib.realm_query_parse_for_list( @@ -919,8 +919,8 @@ class _RealmCore { }); } - RealmAppCredentialsHandle createAppCredentialsAnonymous() { - return RealmAppCredentialsHandle._(_realmLib.realm_app_credentials_new_anonymous()); + RealmAppCredentialsHandle createAppCredentialsAnonymous(bool reuseCredentials) { + return RealmAppCredentialsHandle._(_realmLib.realm_app_credentials_new_anonymous(reuseCredentials)); } RealmAppCredentialsHandle createAppCredentialsEmailPassword(String email, String password) { @@ -1852,6 +1852,13 @@ Pointer _toRealmValue(Object? value, Allocator allocator) { const int _microsecondsPerSecond = 1000 * 1000; const int _nanosecondsPerMicrosecond = 1000; +void _intoRealmQueryArg(Object? value, Pointer realm_query_arg, Allocator allocator) { + realm_query_arg.ref.arg = allocator(); + realm_query_arg.ref.nb_args = 1; + realm_query_arg.ref.is_list = false; + _intoRealmValue(value, realm_query_arg.ref.arg, allocator); +} + void _intoRealmValue(Object? value, Pointer realm_value, Allocator allocator) { if (value == null) { realm_value.ref.type = realm_value_type.RLM_TYPE_NULL; diff --git a/src/realm-core b/src/realm-core index ab2b4947c..2b8def17b 160000 --- a/src/realm-core +++ b/src/realm-core @@ -1 +1 @@ -Subproject commit ab2b4947cd559746c775a3024972c5a9a09c47ba +Subproject commit 2b8def17bb8e0cddbcc754db6b6466b8487feebf From 9cd7c78c97f72f187dda0d1c35f4c742d8dd91a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Overg=C3=A5rd=20Nielsen?= Date: Fri, 12 Aug 2022 08:22:27 +0200 Subject: [PATCH 2/3] Update doc comment --- lib/src/credentials.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/credentials.dart b/lib/src/credentials.dart index 0531b3aac..4fdb508eb 100644 --- a/lib/src/credentials.dart +++ b/lib/src/credentials.dart @@ -28,7 +28,7 @@ enum AuthProviderType { /// For authenticating without credentials. anonymous, - /// For authenticating without credentials. + /// For authenticating without credentials using a new anonymous user. anonymousNoReuse, /// Authenticate with Apple Id From 90fccd60bf214e3297df2893e6f42b03a1c72424 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Overg=C3=A5rd=20Nielsen?= Date: Fri, 12 Aug 2022 08:24:47 +0200 Subject: [PATCH 3/3] Add test for Credentials.anonymous(reuseCredentials: false) --- test/credentials_test.dart | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/credentials_test.dart b/test/credentials_test.dart index 716c8cf90..ccf8342c1 100644 --- a/test/credentials_test.dart +++ b/test/credentials_test.dart @@ -30,6 +30,16 @@ Future main([List? args]) async { expect(credentials.provider, AuthProviderType.anonymous); }); + baasTest('Anonymous - new user', (configuration) async { + final app = App(configuration); + final user1 = await app.logIn(Credentials.anonymous()); + final user2 = await app.logIn(Credentials.anonymous()); + final user3 = await app.logIn(Credentials.anonymous(reuseCredentials: false)); + + expect(user1, user2); + expect(user1, isNot(user3)); + }); + test('Credentials email/password', () { final credentials = Credentials.emailPassword("test@email.com", "000000"); expect(credentials.provider, AuthProviderType.emailPassword);