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: support throwing errors instead of returning them #1022

Open
wants to merge 8 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
61 changes: 57 additions & 4 deletions src/GoTrueClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ const DEFAULT_OPTIONS: Omit<Required<GoTrueClientOptions>, 'fetch' | 'storage' |
flowType: 'implicit',
debug: false,
hasCustomAuthorizationHeader: false,
throwOnError: false,
}

/** Current session will be checked for refresh at this interval. */
Expand Down Expand Up @@ -167,6 +168,7 @@ export default class GoTrueClient {
protected lock: LockFunc
protected lockAcquired = false
protected pendingInLock: Promise<any>[] = []
protected throwOnError: boolean

/**
* Used to broadcast state change events to other tabs listening.
Expand Down Expand Up @@ -212,6 +214,7 @@ export default class GoTrueClient {
this.detectSessionInUrl = settings.detectSessionInUrl
this.flowType = settings.flowType
this.hasCustomAuthorizationHeader = settings.hasCustomAuthorizationHeader
this.throwOnError = settings.throwOnError

if (settings.lock) {
this.lock = settings.lock
Expand Down Expand Up @@ -399,6 +402,7 @@ export default class GoTrueClient {
const { data, error } = res

if (error || !data) {
if (this.throwOnError) throw error
return { data: { user: null, session: null }, error: error }
}
const session: Session | null = data.session
Expand All @@ -412,6 +416,7 @@ export default class GoTrueClient {
return { data: { user, session }, error: null }
} catch (error) {
if (isAuthError(error)) {
if (this.throwOnError) throw error
return { data: { user: null, session: null }, error }
}

Expand Down Expand Up @@ -477,6 +482,7 @@ export default class GoTrueClient {
const { data, error } = res

if (error || !data) {
if (this.throwOnError) throw error
return { data: { user: null, session: null }, error: error }
}

Expand All @@ -491,6 +497,7 @@ export default class GoTrueClient {
return { data: { user, session }, error: null }
} catch (error) {
if (isAuthError(error)) {
if (this.throwOnError) throw error
return { data: { user: null, session: null }, error }
}

Expand Down Expand Up @@ -541,9 +548,12 @@ export default class GoTrueClient {
const { data, error } = res

if (error) {
if (this.throwOnError) throw error
return { data: { user: null, session: null }, error }
} else if (!data || !data.session || !data.user) {
return { data: { user: null, session: null }, error: new AuthInvalidTokenResponseError() }
const invalidTokenError = new AuthInvalidTokenResponseError()
if (this.throwOnError) throw invalidTokenError
return { data: { user: null, session: null }, error: invalidTokenError }
}
if (data.session) {
await this._saveSession(data.session)
Expand All @@ -559,6 +569,7 @@ export default class GoTrueClient {
}
} catch (error) {
if (isAuthError(error)) {
if (this.throwOnError) throw error
return { data: { user: null, session: null }, error }
}
throw error
Expand Down Expand Up @@ -659,11 +670,14 @@ export default class GoTrueClient {

const { data, error } = res
if (error) {
if (this.throwOnError) throw error
return { data: { user: null, session: null }, error }
} else if (!data || !data.session || !data.user) {
const invalidTokenError = new AuthInvalidTokenResponseError()
if (this.throwOnError) throw invalidTokenError
return {
data: { user: null, session: null },
error: new AuthInvalidTokenResponseError(),
error: invalidTokenError,
}
}
if (data.session) {
Expand All @@ -673,6 +687,7 @@ export default class GoTrueClient {
return { data, error }
} catch (error) {
if (isAuthError(error)) {
if (this.throwOnError) throw error
return { data: { user: null, session: null }, error }
}
throw error
Expand Down Expand Up @@ -720,6 +735,7 @@ export default class GoTrueClient {
},
redirectTo: options?.emailRedirectTo,
})
if (this.throwOnError && error) throw error
return { data: { user: null, session: null }, error }
}
if ('phone' in credentials) {
Expand All @@ -734,11 +750,13 @@ export default class GoTrueClient {
channel: options?.channel ?? 'sms',
},
})
if (this.throwOnError && error) throw error
return { data: { user: null, session: null, messageId: data?.message_id }, error }
}
throw new AuthInvalidCredentialsError('You must provide either an email or phone number.')
} catch (error) {
if (isAuthError(error)) {
if (this.throwOnError) throw error
return { data: { user: null, session: null }, error }
}

Expand Down Expand Up @@ -768,11 +786,14 @@ export default class GoTrueClient {
})

if (error) {
if (this.throwOnError) throw error
throw error
}

if (!data) {
throw new Error('An error occurred on token verification.')
const tokenVerificationError = new Error('An error occurred on token verification.')
if (this.throwOnError) throw tokenVerificationError
throw tokenVerificationError
}

const session: Session | null = data.session
Expand All @@ -789,6 +810,7 @@ export default class GoTrueClient {
return { data: { user, session }, error: null }
} catch (error) {
if (isAuthError(error)) {
if (this.throwOnError) throw error
return { data: { user: null, session: null }, error }
}

Expand Down Expand Up @@ -838,6 +860,7 @@ export default class GoTrueClient {
})
} catch (error) {
if (isAuthError(error)) {
if (this.throwOnError) throw error
return { data: null, error }
}
throw error
Expand Down Expand Up @@ -870,10 +893,12 @@ export default class GoTrueClient {
headers: this.headers,
jwt: session.access_token,
})
if (this.throwOnError && error) throw error
return { data: { user: null, session: null }, error }
})
} catch (error) {
if (isAuthError(error)) {
if (this.throwOnError) throw error
return { data: { user: null, session: null }, error }
}
throw error
Expand All @@ -897,6 +922,7 @@ export default class GoTrueClient {
},
redirectTo: options?.emailRedirectTo,
})
if (this.throwOnError && error) throw error
return { data: { user: null, session: null }, error }
} else if ('phone' in credentials) {
const { phone, type, options } = credentials
Expand All @@ -908,13 +934,15 @@ export default class GoTrueClient {
gotrue_meta_security: { captcha_token: options?.captchaToken },
},
})
if (this.throwOnError && error) throw error
return { data: { user: null, session: null, messageId: data?.message_id }, error }
}
throw new AuthInvalidCredentialsError(
'You must provide either an email or phone number and a type'
)
} catch (error) {
if (isAuthError(error)) {
if (this.throwOnError) throw error
return { data: { user: null, session: null }, error }
}
throw error
Expand Down Expand Up @@ -1209,6 +1237,7 @@ export default class GoTrueClient {
await removeItemAsync(this.storage, `${this.storageKey}-code-verifier`)
}

if (this.throwOnError) throw error
return { data: { user: null }, error }
}

Expand Down Expand Up @@ -1268,14 +1297,18 @@ export default class GoTrueClient {
jwt: session.access_token,
xform: _userResponse,
})
if (userError) throw userError
if (userError) {
if (this.throwOnError) throw userError
throw userError
}
session.user = data.user as User
await this._saveSession(session)
await this._notifyAllSubscribers('USER_UPDATED', session)
return { data: { user: session.user }, error: null }
})
} catch (error) {
if (isAuthError(error)) {
if (this.throwOnError) throw error
return { data: { user: null }, error }
}

Expand Down Expand Up @@ -1334,6 +1367,7 @@ export default class GoTrueClient {
currentSession.refresh_token
)
if (error) {
if (this.throwOnError) throw error
return { data: { user: null, session: null }, error: error }
}

Expand Down Expand Up @@ -1361,6 +1395,7 @@ export default class GoTrueClient {
return { data: { user: session.user, session }, error: null }
} catch (error) {
if (isAuthError(error)) {
if (this.throwOnError) throw error
return { data: { session: null, user: null }, error }
}

Expand Down Expand Up @@ -1402,6 +1437,7 @@ export default class GoTrueClient {

const { session, error } = await this._callRefreshToken(currentSession.refresh_token)
if (error) {
if (this.throwOnError) throw error
return { data: { user: null, session: null }, error: error }
}

Expand All @@ -1413,6 +1449,7 @@ export default class GoTrueClient {
})
} catch (error) {
if (isAuthError(error)) {
if (this.throwOnError) throw error
return { data: { user: null, session: null }, error }
}

Expand Down Expand Up @@ -1547,6 +1584,7 @@ export default class GoTrueClient {
return { data: { session, redirectType: params.type }, error: null }
} catch (error) {
if (isAuthError(error)) {
if (this.throwOnError) throw error
return { data: { session: null, redirectType: null }, error }
}

Expand Down Expand Up @@ -1609,6 +1647,7 @@ export default class GoTrueClient {
(error.status === 404 || error.status === 401 || error.status === 403)
)
) {
if (this.throwOnError) throw error
return { error }
}
}
Expand Down Expand Up @@ -1717,6 +1756,7 @@ export default class GoTrueClient {
})
} catch (error) {
if (isAuthError(error)) {
if (this.throwOnError) throw error
return { data: null, error }
}

Expand All @@ -1742,6 +1782,7 @@ export default class GoTrueClient {
return { data: { identities: data.user.identities ?? [] }, error: null }
} catch (error) {
if (isAuthError(error)) {
if (this.throwOnError) throw error
return { data: null, error }
}
throw error
Expand Down Expand Up @@ -1778,6 +1819,7 @@ export default class GoTrueClient {
return { data: { provider: credentials.provider, url: data?.url }, error: null }
} catch (error) {
if (isAuthError(error)) {
if (this.throwOnError) throw error
return { data: { provider: credentials.provider, url: null }, error }
}
throw error
Expand Down Expand Up @@ -1812,6 +1854,7 @@ export default class GoTrueClient {
})
} catch (error) {
if (isAuthError(error)) {
if (this.throwOnError) throw error
return { data: null, error }
}
throw error
Expand Down Expand Up @@ -1858,6 +1901,7 @@ export default class GoTrueClient {
this._debug(debugName, 'error', error)

if (isAuthError(error)) {
if (this.throwOnError) throw error
return { data: { session: null, user: null }, error }
}
throw error
Expand Down Expand Up @@ -2373,6 +2417,7 @@ export default class GoTrueClient {
})
} catch (error) {
if (isAuthError(error)) {
if (this.throwOnError) throw error
return { data: null, error }
}
throw error
Expand Down Expand Up @@ -2405,6 +2450,7 @@ export default class GoTrueClient {
})

if (error) {
if (this.throwOnError) throw error
return { data: null, error }
}

Expand All @@ -2416,6 +2462,7 @@ export default class GoTrueClient {
})
} catch (error) {
if (isAuthError(error)) {
if (this.throwOnError) throw error
return { data: null, error }
}
throw error
Expand Down Expand Up @@ -2445,6 +2492,7 @@ export default class GoTrueClient {
}
)
if (error) {
if (this.throwOnError) throw error
return { data: null, error }
}

Expand All @@ -2458,6 +2506,7 @@ export default class GoTrueClient {
})
} catch (error) {
if (isAuthError(error)) {
if (this.throwOnError) throw error
return { data: null, error }
}
throw error
Expand Down Expand Up @@ -2490,6 +2539,7 @@ export default class GoTrueClient {
})
} catch (error) {
if (isAuthError(error)) {
if (this.throwOnError) throw error
return { data: null, error }
}
throw error
Expand All @@ -2510,6 +2560,7 @@ export default class GoTrueClient {
factorId: params.factorId,
})
if (challengeError) {
if (this.throwOnError) throw challengeError
return { data: null, error: challengeError }
}

Expand All @@ -2530,6 +2581,7 @@ export default class GoTrueClient {
error: userError,
} = await this.getUser()
if (userError) {
if (this.throwOnError) throw userError
return { data: null, error: userError }
}

Expand Down Expand Up @@ -2562,6 +2614,7 @@ export default class GoTrueClient {
error: sessionError,
} = result
if (sessionError) {
if (this.throwOnError) throw sessionError
return { data: null, error: sessionError }
}
if (!session) {
Expand Down
5 changes: 5 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ export type GoTrueClientOptions = {
* @experimental
*/
hasCustomAuthorizationHeader?: boolean
/**
* If there is an error with the query, throwOnError will reject the promise by
* throwing the error instead of returning it as part of a successful response.
*/
throwOnError?: boolean
}

export type WeakPasswordReasons = 'length' | 'characters' | 'pwned' | (string & {})
Expand Down
Loading
Loading