@@ -2176,11 +2176,27 @@ export default class GoTrueClient {
2176
2176
throw error
2177
2177
}
2178
2178
}
2179
+
2179
2180
/**
2180
2181
* Links an oauth identity to an existing user.
2181
2182
* This method supports the PKCE flow.
2182
2183
*/
2183
- async linkIdentity ( credentials : SignInWithOAuthCredentials ) : Promise < OAuthResponse > {
2184
+ async linkIdentity ( credentials : SignInWithOAuthCredentials ) : Promise < OAuthResponse >
2185
+
2186
+ /**
2187
+ * Links an OIDC identity to an existing user.
2188
+ */
2189
+ async linkIdentity ( credentials : SignInWithIdTokenCredentials ) : Promise < AuthTokenResponse >
2190
+
2191
+ async linkIdentity ( credentials : any ) : Promise < any > {
2192
+ if ( 'token' in credentials ) {
2193
+ return this . linkIdentityIdToken ( credentials )
2194
+ }
2195
+
2196
+ return this . linkIdentityOAuth ( credentials )
2197
+ }
2198
+
2199
+ private async linkIdentityOAuth ( credentials : SignInWithOAuthCredentials ) : Promise < OAuthResponse > {
2184
2200
try {
2185
2201
const { data, error } = await this . _useSession ( async ( result ) => {
2186
2202
const { data, error } = result
@@ -2213,6 +2229,56 @@ export default class GoTrueClient {
2213
2229
}
2214
2230
}
2215
2231
2232
+ private async linkIdentityIdToken (
2233
+ credentials : SignInWithIdTokenCredentials
2234
+ ) : Promise < AuthTokenResponse > {
2235
+ return await this . _useSession ( async ( result ) => {
2236
+ try {
2237
+ const {
2238
+ error : sessionError ,
2239
+ data : { session } ,
2240
+ } = result
2241
+ if ( sessionError ) throw sessionError
2242
+
2243
+ const { options, provider, token, access_token, nonce } = credentials
2244
+
2245
+ const res = await _request ( this . fetch , 'POST' , `${ this . url } /token?grant_type=id_token` , {
2246
+ headers : this . headers ,
2247
+ jwt : session ?. access_token ?? undefined ,
2248
+ body : {
2249
+ provider,
2250
+ id_token : token ,
2251
+ access_token,
2252
+ nonce,
2253
+ link_identity : true ,
2254
+ gotrue_meta_security : { captcha_token : options ?. captchaToken } ,
2255
+ } ,
2256
+ xform : _sessionResponse ,
2257
+ } )
2258
+
2259
+ const { data, error } = res
2260
+ if ( error ) {
2261
+ return { data : { user : null , session : null } , error }
2262
+ } else if ( ! data || ! data . session || ! data . user ) {
2263
+ return {
2264
+ data : { user : null , session : null } ,
2265
+ error : new AuthInvalidTokenResponseError ( ) ,
2266
+ }
2267
+ }
2268
+ if ( data . session ) {
2269
+ await this . _saveSession ( data . session )
2270
+ await this . _notifyAllSubscribers ( 'USER_UPDATED' , data . session )
2271
+ }
2272
+ return { data, error }
2273
+ } catch ( error ) {
2274
+ if ( isAuthError ( error ) ) {
2275
+ return { data : { user : null , session : null } , error }
2276
+ }
2277
+ throw error
2278
+ }
2279
+ } )
2280
+ }
2281
+
2216
2282
/**
2217
2283
* Unlinks an identity from a user by deleting it. The user will no longer be able to sign in with that identity once it's unlinked.
2218
2284
*/
0 commit comments