Skip to content

Commit

Permalink
Merge 18ec3ca into 07be0ae
Browse files Browse the repository at this point in the history
  • Loading branch information
desistefanova committed Apr 12, 2022
2 parents 07be0ae + 18ec3ca commit 76793ec
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ x.x.x Release notes (yyyy-MM-dd)
* Support application ([#446](https://github.com/realm/realm-dart/pull/446/))
* Support should realm compact on open callback `Configuration.shouldCompactCallback` as option when configuring a Realm to determine if it should be compacted before being returned. ([#466](https://github.com/realm/realm-dart/pull/466/))
* Support ObjectId ([#468](https://github.com/realm/realm-dart/pull/468))
* Support application user login method ([#469](https://github.com/realm/realm-dart/pull/469/))

### Fixed
* Fixed an issue that would result in the wrong transaction being rolled back if you start a write transaction inside a write transaction. ([#442](https://github.com/realm/realm-dart/issues/442))
Expand Down
6 changes: 6 additions & 0 deletions lib/src/application.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import 'dart:io';
import 'package:meta/meta.dart';
import 'native/realm_core.dart';
import 'credentials.dart';
import 'user.dart';

/// Specify if and how to persists user objects.
enum MetadataPersistenceMode {
Expand Down Expand Up @@ -105,6 +107,10 @@ class Application {
final ApplicationConfiguration configuration;

Application(this.configuration) : _handle = realmCore.getApp(configuration);

Future<User> logIn(Credentials credentials) async {
return UserInternal.create(await realmCore.logIn(this, credentials));
}
}

extension ApplicationInternal on Application {
Expand Down
37 changes: 35 additions & 2 deletions lib/src/native/realm_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import 'dart:typed_data';

// Hide StringUtf8Pointer.toNativeUtf8 and StringUtf16Pointer since these allows silently allocating memory. Use toUtf8Ptr instead
import 'package:ffi/ffi.dart' hide StringUtf8Pointer, StringUtf16Pointer;
import 'package:pub_semver/pub_semver.dart';

import '../application.dart';
import '../credentials.dart';
Expand Down Expand Up @@ -671,7 +672,7 @@ class _RealmCore {
_realmLib.realm_app_config_set_base_url(handle._pointer, configuration.baseUrl.toString().toUtf8Ptr(arena));

_realmLib.realm_app_config_set_default_request_timeout(handle._pointer, configuration.defaultRequestTimeout.inMilliseconds);

if (configuration.localAppName != null) {
_realmLib.realm_app_config_set_local_app_name(handle._pointer, configuration.localAppName!.toUtf8Ptr(arena));
}
Expand Down Expand Up @@ -849,6 +850,35 @@ class _RealmCore {
final syncClientConfig = createSyncClientConfig(configuration);
return AppHandle._(_realmLib.invokeGetPointer(() => _realmLib.realm_app_get(appConfig._pointer, syncClientConfig._pointer)));
}

static void _logInCallback(Pointer<Void> userdata, Pointer<realm_user> user, Pointer<realm_app_error> error) {
final Completer<UserHandle>? userHandleCompleter = userdata.toObject();
if (userHandleCompleter == null) {
return;
}
if (error == nullptr) {
userHandleCompleter.complete(UserHandle._(_realmLib.realm_clone(user.cast()).cast()));
} else {
final message = error.ref.message.cast<Utf8>().toDartString();
userHandleCompleter.completeError(RealmException(message));
}
}

static void _freeCallback(Pointer<Void> userdata) {
userdata.toObject(); // TODO: release
}

Future<UserHandle> logIn(Application application, Credentials credentials) async {
final completer = Completer<UserHandle>();
_realmLib.realm_app_log_in_with_credentials(
application.handle._pointer,
credentials.handle._pointer,
Pointer.fromFunction(_logInCallback),
completer.toGCHandle(),
Pointer.fromFunction(_freeCallback),
);
return completer.future;
}
}

class LastError {
Expand Down Expand Up @@ -965,8 +995,11 @@ class AppHandle extends Handle<realm_app> {
AppHandle._(Pointer<realm_app> pointer) : super(pointer, 16);
}

class UserHandle extends Handle<realm_user> {
UserHandle._(Pointer<realm_user> pointer) : super(pointer, 256); // TODO: What should hint be?
}
extension on List<int> {
Pointer<Uint8> toUint8Ptr(Allocator allocator) {
Pointer<Uint8> toUint8Ptr(Allocator allocator) {
final nativeSize = length + 1;
final result = allocator<Uint8>(nativeSize);
final Uint8List native = result.asTypedList(nativeSize);
Expand Down
14 changes: 13 additions & 1 deletion lib/src/realm_class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,25 @@ import 'results.dart';
// always expose with `show` to explicitly control the public API surface
export 'application.dart' show ApplicationConfiguration, MetadataPersistenceMode, Application;
export 'package:realm_common/realm_common.dart'
show Ignored, Indexed, MapTo, PrimaryKey, RealmError, RealmModel, RealmUnsupportedSetError, RealmStateError, RealmCollectionType, RealmPropertyType, ObjectId;
show
Ignored,
Indexed,
MapTo,
PrimaryKey,
RealmError,
RealmModel,
RealmUnsupportedSetError,
RealmStateError,
RealmCollectionType,
RealmPropertyType,
ObjectId;
export "configuration.dart" show Configuration, RealmSchema, SchemaObject;
export 'list.dart' show RealmList, RealmListOfObject, RealmListChanges;
export 'realm_object.dart' show RealmEntity, RealmException, RealmObject, RealmObjectChanges;
export 'realm_property.dart';
export 'results.dart' show RealmResults, RealmResultsChanges;
export 'credentials.dart' show Credentials, AuthProvider;
export 'user.dart' show User;

/// A [Realm] instance represents a `Realm` database.
///
Expand Down
29 changes: 29 additions & 0 deletions lib/src/user.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2022 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////////
import 'native/realm_core.dart';

class User {
final UserHandle _handle;

User._(this._handle);
}

extension UserInternal on User {
static User create(UserHandle handle) => User._(handle);
}
7 changes: 7 additions & 0 deletions test/application_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
//
////////////////////////////////////////////////////////////////////////////////
import 'dart:async';
import 'dart:io';

import 'package:test/expect.dart';
Expand Down Expand Up @@ -58,4 +59,10 @@ Future<void> main([List<String>? args]) async {
final application = Application(configuration);
expect(application.configuration, configuration);
});

testWithBaaS('Application log in', (configuration) async {
final application = Application(configuration);
final credentials = Credentials.anonymous();
final user = await application.logIn(credentials);
});
}
27 changes: 27 additions & 0 deletions test/test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
//
////////////////////////////////////////////////////////////////////////////////
import 'dart:async';
import 'dart:collection';
import 'dart:io';
import 'dart:math';
import 'package:meta/meta.dart';
import 'package:path/path.dart' as _path;
import 'package:test/test.dart' hide test;
import 'package:test/test.dart' as testing;
Expand Down Expand Up @@ -196,3 +198,28 @@ Future<void> setupBaas() async {
baasApps.addAll(await client.getOrCreateApps());
}

@isTest
Future<void> testWithBaaS(
String? name,
FutureOr<void> Function(ApplicationConfiguration configuration) testFunction, {
String appName = 'flexible',
bool skip = false,
}) async {
final url = Uri.tryParse(Platform.environment['BAAS_URL'] ?? 'https://realm-dev.mongodb.com');
final apiKey = Platform.environment['BAAS_API_KEY'];
final projectId = Platform.environment['BAAS_PROJECT_ID'];

final missingOrSkip = skip || url == null || apiKey == null || projectId == null;
test(name, () async {
if (!missingOrSkip) {
final app = baasApps[appName] ?? baasApps.values.first;
final temporary = await Directory.systemTemp.createTemp('realm_dart_test_');
final configuration = ApplicationConfiguration(
app.clientAppId,
baseUrl: url,
baseFilePath: temporary,
);
return await testFunction(configuration);
}
}, skip: missingOrSkip);
}

0 comments on commit 76793ec

Please sign in to comment.