@@ -460,6 +460,8 @@ export default class GoTrueClient {
460
460
const { data, error } = res
461
461
462
462
if ( error || ! data ) {
463
+ await removeItemAsync ( this . storage , `${ this . storageKey } -code-verifier` )
464
+ await this . _notifyAllSubscribers ( 'STORAGE_UPDATED' , null )
463
465
return { data : { user : null , session : null } , error : error }
464
466
}
465
467
@@ -473,6 +475,8 @@ export default class GoTrueClient {
473
475
474
476
return { data : { user, session } , error : null }
475
477
} catch ( error ) {
478
+ await removeItemAsync ( this . storage , `${ this . storageKey } -code-verifier` )
479
+ await this . _notifyAllSubscribers ( 'STORAGE_UPDATED' , null )
476
480
if ( isAuthError ( error ) ) {
477
481
return { data : { user : null , session : null } , error }
478
482
}
@@ -613,6 +617,8 @@ export default class GoTrueClient {
613
617
return { data : { ...data , redirectType : redirectType ?? null } , error }
614
618
} catch ( error ) {
615
619
if ( isAuthError ( error ) ) {
620
+ await removeItemAsync ( this . storage , `${ this . storageKey } -code-verifier` )
621
+ await this . _notifyAllSubscribers ( 'STORAGE_UPDATED' , null )
616
622
return { data : { user : null , session : null , redirectType : null } , error }
617
623
}
618
624
@@ -721,6 +727,8 @@ export default class GoTrueClient {
721
727
}
722
728
throw new AuthInvalidCredentialsError ( 'You must provide either an email or phone number.' )
723
729
} catch ( error ) {
730
+ await removeItemAsync ( this . storage , `${ this . storageKey } -code-verifier` )
731
+ await this . _notifyAllSubscribers ( 'STORAGE_UPDATED' , null )
724
732
if ( isAuthError ( error ) ) {
725
733
return { data : { user : null , session : null } , error }
726
734
}
@@ -820,6 +828,8 @@ export default class GoTrueClient {
820
828
xform : _ssoResponse ,
821
829
} )
822
830
} catch ( error ) {
831
+ await removeItemAsync ( this . storage , `${ this . storageKey } -code-verifier` )
832
+ await this . _notifyAllSubscribers ( 'STORAGE_UPDATED' , null )
823
833
if ( isAuthError ( error ) ) {
824
834
return { data : null , error }
825
835
}
@@ -1221,6 +1231,7 @@ export default class GoTrueClient {
1221
1231
emailRedirectTo ?: string | undefined
1222
1232
} = { }
1223
1233
) : Promise < UserResponse > {
1234
+ let session : Session | null = null
1224
1235
try {
1225
1236
return await this . _useSession ( async ( result ) => {
1226
1237
const { data : sessionData , error : sessionError } = result
@@ -1230,7 +1241,7 @@ export default class GoTrueClient {
1230
1241
if ( ! sessionData . session ) {
1231
1242
throw new AuthSessionMissingError ( )
1232
1243
}
1233
- const session : Session = sessionData . session
1244
+ session = sessionData . session
1234
1245
let codeChallenge : string | null = null
1235
1246
let codeChallengeMethod : string | null = null
1236
1247
if ( this . flowType === 'pkce' && attributes . email != null ) {
@@ -1258,6 +1269,8 @@ export default class GoTrueClient {
1258
1269
return { data : { user : session . user } , error : null }
1259
1270
} )
1260
1271
} catch ( error ) {
1272
+ await removeItemAsync ( this . storage , `${ this . storageKey } -code-verifier` )
1273
+ await this . _notifyAllSubscribers ( 'STORAGE_UPDATED' , session )
1261
1274
if ( isAuthError ( error ) ) {
1262
1275
return { data : { user : null } , error }
1263
1276
}
@@ -1686,6 +1699,8 @@ export default class GoTrueClient {
1686
1699
redirectTo : options . redirectTo ,
1687
1700
} )
1688
1701
} catch ( error ) {
1702
+ await removeItemAsync ( this . storage , `${ this . storageKey } -code-verifier` )
1703
+ await this . _notifyAllSubscribers ( 'STORAGE_UPDATED' , null )
1689
1704
if ( isAuthError ( error ) ) {
1690
1705
return { data : null , error }
1691
1706
}
@@ -1722,9 +1737,11 @@ export default class GoTrueClient {
1722
1737
* This method supports the PKCE flow.
1723
1738
*/
1724
1739
async linkIdentity ( credentials : SignInWithOAuthCredentials ) : Promise < OAuthResponse > {
1740
+ let session : Session | null = null
1725
1741
try {
1726
1742
const { data, error } = await this . _useSession ( async ( result ) => {
1727
1743
const { data, error } = result
1744
+ session = data . session
1728
1745
if ( error ) throw error
1729
1746
const url : string = await this . _getUrlForProvider (
1730
1747
`${ this . url } /user/identities/authorize` ,
@@ -1747,6 +1764,8 @@ export default class GoTrueClient {
1747
1764
}
1748
1765
return { data : { provider : credentials . provider , url : data ?. url } , error : null }
1749
1766
} catch ( error ) {
1767
+ await removeItemAsync ( this . storage , `${ this . storageKey } -code-verifier` )
1768
+ await this . _notifyAllSubscribers ( 'STORAGE_UPDATED' , session )
1750
1769
if ( isAuthError ( error ) ) {
1751
1770
return { data : { provider : credentials . provider , url : null } , error }
1752
1771
}
@@ -2032,6 +2051,9 @@ export default class GoTrueClient {
2032
2051
// so we can safely suppress the warning returned by future getSession calls
2033
2052
this . suppressGetSessionWarning = true
2034
2053
await setItemAsync ( this . storage , this . storageKey , session )
2054
+
2055
+ // cleanup potentially unused code verifier
2056
+ await removeItemAsync ( this . storage , `${ this . storageKey } -code-verifier` )
2035
2057
}
2036
2058
2037
2059
private async _removeSession ( ) {
0 commit comments