Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apple, Facebook and Google authentication #740

Merged
merged 3 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Support `Configuration.defaultRealmPath` for setting a custom default path for realms. ([#665](https://github.com/realm/realm-dart/pull/665))
* 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))

### 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))
Expand Down
37 changes: 31 additions & 6 deletions lib/src/credentials.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,17 @@ enum AuthProviderType {
/// For authenticating without credentials.
anonymous,

_facebook,
_google,
_apple,
_custom,
/// Authenticate with Apple Id
apple,

/// Authenticate with Facebook account.
facebook,

/// Authenticate with Google account
google,

_custom,

/// For authenticating with an email and a password.
emailPassword,

Expand All @@ -44,8 +50,7 @@ enum AuthProviderType {
/// A class, representing the credentials used for authenticating a [User]
/// {@category Application}
class Credentials {
late final RealmAppCredentialsHandle _handle;

final RealmAppCredentialsHandle _handle;
final AuthProviderType provider;

/// Returns a [Credentials] object that can be used to authenticate an anonymous user.
Expand All @@ -54,13 +59,33 @@ class Credentials {
: _handle = realmCore.createAppCredentialsAnonymous(),
provider = AuthProviderType.anonymous;

/// 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),
provider = AuthProviderType.apple;

/// 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://docs.mongodb.com/realm/authentication/email-password)
Credentials.emailPassword(String email, String password)
: _handle = realmCore.createAppCredentialsEmailPassword(email, password),
provider = AuthProviderType.emailPassword;

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

/// 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),
provider = AuthProviderType.google;

/// 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),
provider = AuthProviderType.google;
}

/// @nodoc
Expand Down
28 changes: 28 additions & 0 deletions lib/src/native/realm_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,34 @@ class _RealmCore {
});
}

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));
});
}

RealmHttpTransportHandle _createHttpTransport(HttpClient httpClient) {
final requestCallback = Pointer.fromFunction<Void Function(Handle, realm_http_request, Pointer<Void>)>(_request_callback);
final requestCallbackUserdata = _realmLib.realm_dart_userdata_async_new(httpClient, requestCallback.cast(), scheduler.handle._pointer);
Expand Down