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 1 commit
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
44 changes: 44 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,50 @@ class NhostAuthClient implements HasuraAuthClient {
return res;
}

/// Authenticates a user using an [idToken].
///
/// If the user has multi-factor authentication enabled, the returned
onehassan marked this conversation as resolved.
Show resolved Hide resolved
/// [AuthResponse] will only have its [AuthResponse.mfa] field set, which can
/// then be used to complete the sign in via [completeMfaSignIn] alongside the
/// user's one-time-password.
///
/// Throws an [NhostException] if sign in fails.
@override
Future<AuthResponse> signInIdToken({
required String provider,
required String idToken,
String? nonce
onehassan marked this conversation as resolved.
Show resolved Hide resolved
}) 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,
},
responseDeserializer: AuthResponse.fromJson,
);
} catch (e, st) {
log.finer('Sign in failed', e, st);
await clearSession();
rethrow;
}

// If multi-factor is enabled, a second step is required before we've fully
// logged in.
if (res!.mfa != null) {
log.finer('Sign in requires MFA');
return res;
}

log.finer('Sign in successful');
await setSession(res.session!);
return res;
}

/// Signs in a user with a magic link.
///
/// An email will be sent to the [email] with a link. When the user
Expand Down
3 changes: 3 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,9 @@ abstract class HasuraAuthClient {
required String password,
});

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

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