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

feat: add support for sign-in with id token #149

Merged
merged 4 commits into from
Nov 28, 2024
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
77 changes: 77 additions & 0 deletions packages/nhost_auth_dart/lib/src/auth_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,83 @@ class NhostAuthClient implements HasuraAuthClient {
return res;
}

/// Authenticates a user using an ID token from a third-party provider.
///
/// This method allows users to sign in using an OpenID Connect [idToken] from a specified
/// [provider] (google, apple). An optional [nonce] parameter can be provided for additional security.
///
/// Throws an [NhostException] if sign in fails.
@override
Future<AuthResponse> signInIdToken({
required String provider,
required String idToken,
String? nonce,
String? locale,
String? defaultRole,
Map<String, Object?>? metadata,
List<String>? roles,
String? displayName,
String? redirectTo,
}) async {
log.finer('Attempting sign in (idToken)');
AuthResponse? res;

try {
res = await _apiClient.post(
'/signin/idtoken',
jsonBody: {
'provider': provider,
'idToken': idToken,
if (nonce != null) 'nonce': nonce,
if (locale != null) 'locale': locale,
if (defaultRole != null) 'defaultRole': defaultRole,
if (metadata != null) 'metadata': metadata,
if (roles != null) 'roles': roles,
if (displayName != null) 'displayName': displayName,
if (redirectTo != null) 'redirectTo': redirectTo,
},
responseDeserializer: AuthResponse.fromJson,
);
} catch (e, st) {
log.finer('Sign in failed', e, st);
await clearSession();
rethrow;
}

if (res != null) {
log.finer('Sign in successful');
await setSession(res.session!);
return res;
} else {
throw AuthServiceException(
'Sign in failed',
);
}
}

/// Links an existing user account to a third-party provider using an OpenID Connect [idToken].
///
/// This method enables linking a user account with an OpenID Connect [idToken] from a specified
/// [provider], such as "google" or "apple". You can optionally provide a [nonce] for enhanced security.
///
/// Throws an [NhostException] if the link attempt fails.
@override
Future<void> linkIdToken({
required String provider,
required String idToken,
String? nonce,
}) async {
await _apiClient.post<String>(
'/link/idtoken',
jsonBody: {
'provider': provider,
'idToken': idToken,
if (nonce != null) 'nonce': nonce,
},
headers: _session.authenticationHeaders,
);
}

/// Signs in a user with a magic link.
///
/// An email will be sent to the [email] with a link. When the user
Expand Down
6 changes: 6 additions & 0 deletions packages/nhost_sdk/lib/src/base/hasura_auth_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ abstract class HasuraAuthClient {
required String password,
});

Future<AuthResponse> signInIdToken(
{required String provider, required String idToken, String? nonce});

Future<void> linkIdToken(
{required String provider, required String idToken, String? nonce});

Future<void> signInWithEmailPasswordless(
String email, {
String? redirectTo,
Expand Down
Loading