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

fix: code verifier remains in storage during edge cases #986

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 23 additions & 1 deletion src/GoTrueClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,8 @@ export default class GoTrueClient {
const { data, error } = res

if (error || !data) {
await removeItemAsync(this.storage, `${this.storageKey}-code-verifier`)
await this._notifyAllSubscribers('STORAGE_UPDATED', null)
return { data: { user: null, session: null }, error: error }
}

Expand All @@ -490,6 +492,8 @@ export default class GoTrueClient {

return { data: { user, session }, error: null }
} catch (error) {
await removeItemAsync(this.storage, `${this.storageKey}-code-verifier`)
await this._notifyAllSubscribers('STORAGE_UPDATED', null)
if (isAuthError(error)) {
return { data: { user: null, session: null }, error }
}
Expand Down Expand Up @@ -630,6 +634,8 @@ export default class GoTrueClient {
return { data: { ...data, redirectType: redirectType ?? null }, error }
} catch (error) {
if (isAuthError(error)) {
await removeItemAsync(this.storage, `${this.storageKey}-code-verifier`)
await this._notifyAllSubscribers('STORAGE_UPDATED', null)
return { data: { user: null, session: null, redirectType: null }, error }
}

Expand Down Expand Up @@ -738,6 +744,8 @@ export default class GoTrueClient {
}
throw new AuthInvalidCredentialsError('You must provide either an email or phone number.')
} catch (error) {
await removeItemAsync(this.storage, `${this.storageKey}-code-verifier`)
await this._notifyAllSubscribers('STORAGE_UPDATED', null)
if (isAuthError(error)) {
return { data: { user: null, session: null }, error }
}
Expand Down Expand Up @@ -837,6 +845,8 @@ export default class GoTrueClient {
xform: _ssoResponse,
})
} catch (error) {
await removeItemAsync(this.storage, `${this.storageKey}-code-verifier`)
await this._notifyAllSubscribers('STORAGE_UPDATED', null)
if (isAuthError(error)) {
return { data: null, error }
}
Expand Down Expand Up @@ -1238,6 +1248,7 @@ export default class GoTrueClient {
emailRedirectTo?: string | undefined
} = {}
): Promise<UserResponse> {
let session: Session | null = null
try {
return await this._useSession(async (result) => {
const { data: sessionData, error: sessionError } = result
Expand All @@ -1247,7 +1258,7 @@ export default class GoTrueClient {
if (!sessionData.session) {
throw new AuthSessionMissingError()
}
const session: Session = sessionData.session
session = sessionData.session
let codeChallenge: string | null = null
let codeChallengeMethod: string | null = null
if (this.flowType === 'pkce' && attributes.email != null) {
Expand Down Expand Up @@ -1275,6 +1286,8 @@ export default class GoTrueClient {
return { data: { user: session.user }, error: null }
})
} catch (error) {
await removeItemAsync(this.storage, `${this.storageKey}-code-verifier`)
await this._notifyAllSubscribers('STORAGE_UPDATED', session)
if (isAuthError(error)) {
return { data: { user: null }, error }
}
Expand Down Expand Up @@ -1716,6 +1729,8 @@ export default class GoTrueClient {
redirectTo: options.redirectTo,
})
} catch (error) {
await removeItemAsync(this.storage, `${this.storageKey}-code-verifier`)
await this._notifyAllSubscribers('STORAGE_UPDATED', null)
if (isAuthError(error)) {
return { data: null, error }
}
Expand Down Expand Up @@ -1752,9 +1767,11 @@ export default class GoTrueClient {
* This method supports the PKCE flow.
*/
async linkIdentity(credentials: SignInWithOAuthCredentials): Promise<OAuthResponse> {
let session: Session | null = null
try {
const { data, error } = await this._useSession(async (result) => {
const { data, error } = result
session = data.session
if (error) throw error
const url: string = await this._getUrlForProvider(
`${this.url}/user/identities/authorize`,
Expand All @@ -1777,6 +1794,8 @@ export default class GoTrueClient {
}
return { data: { provider: credentials.provider, url: data?.url }, error: null }
} catch (error) {
await removeItemAsync(this.storage, `${this.storageKey}-code-verifier`)
await this._notifyAllSubscribers('STORAGE_UPDATED', session)
if (isAuthError(error)) {
return { data: { provider: credentials.provider, url: null }, error }
}
Expand Down Expand Up @@ -2062,6 +2081,9 @@ export default class GoTrueClient {
// so we can safely suppress the warning returned by future getSession calls
this.suppressGetSessionWarning = true
await setItemAsync(this.storage, this.storageKey, session)

// cleanup potentially unused code verifier
await removeItemAsync(this.storage, `${this.storageKey}-code-verifier`)
}

private async _removeSession() {
Expand Down
1 change: 1 addition & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export type AuthChangeEvent =
| 'PASSWORD_RECOVERY'
| 'SIGNED_IN'
| 'SIGNED_OUT'
| 'STORAGE_UPDATED'
| 'TOKEN_REFRESHED'
| 'USER_UPDATED'
| AuthChangeEventMFA
Expand Down
Loading