Skip to content

Commit 53a2427

Browse files
authored
feat(gotrue): Implement linkIdentityWithIdToken method (#1206)
* feat(gotrue): implement linkIdentityWithIdToken for OIDC support - Add linkIdentityWithIdToken method to link identities using ID tokens - Support OAuth providers with ID token verification - Include nonce and access token validation for enhanced security - Emit userUpdated event when identity is successfully linked Closes CLIBS-282 * pass body directly
1 parent 9b42fab commit 53a2427

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

packages/gotrue/lib/src/gotrue_client.dart

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,57 @@ class GoTrueClient {
902902
return res.user?.identities ?? [];
903903
}
904904

905+
/// Link an identity to the current user using an ID token.
906+
///
907+
/// [provider] is the OAuth provider
908+
///
909+
/// [idToken] is the ID token from the OAuth provider
910+
///
911+
/// [accessToken] is the access token from the OAuth provider
912+
///
913+
/// [nonce] is the nonce used for the OAuth flow
914+
///
915+
/// [captchaToken] is the verification token received when the user
916+
/// completes the captcha on the app.
917+
Future<AuthResponse> linkIdentityWithIdToken({
918+
required OAuthProvider provider,
919+
required String idToken,
920+
String? accessToken,
921+
String? nonce,
922+
String? captchaToken,
923+
}) async {
924+
final response = await _fetch.request(
925+
'$_url/token',
926+
RequestMethodType.post,
927+
options: GotrueRequestOptions(
928+
headers: _headers,
929+
jwt: _currentSession?.accessToken,
930+
body: {
931+
'provider': provider.snakeCase,
932+
'id_token': idToken,
933+
'nonce': nonce,
934+
'gotrue_meta_security': {'captcha_token': captchaToken},
935+
'access_token': accessToken,
936+
'link_identity': true,
937+
},
938+
query: {'grant_type': 'id_token'},
939+
),
940+
);
941+
942+
final authResponse = AuthResponse.fromJson(response);
943+
944+
if (authResponse.session == null) {
945+
throw AuthException(
946+
'An error occurred on token verification.',
947+
);
948+
}
949+
950+
_saveSession(authResponse.session!);
951+
notifyAllSubscribers(AuthChangeEvent.userUpdated);
952+
953+
return authResponse;
954+
}
955+
905956
/// Returns the URL to link the user's identity with an OAuth provider.
906957
Future<OAuthResponse> getLinkIdentityUrl(
907958
OAuthProvider provider, {

0 commit comments

Comments
 (0)