Skip to content
This repository was archived by the owner on Oct 10, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 2 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
7 changes: 6 additions & 1 deletion src/AuthClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import GoTrueClient from './GoTrueClient'

const AuthClient = GoTrueClient
const AuthClient = (options) => {

Check failure on line 3 in src/AuthClient.ts

View workflow job for this annotation

GitHub Actions / Test / OS ubuntu-latest / Node 18

Parameter 'options' implicitly has an 'any' type.

Check failure on line 3 in src/AuthClient.ts

View workflow job for this annotation

GitHub Actions / Test / OS ubuntu-latest / Node 20

Parameter 'options' implicitly has an 'any' type.
return new GoTrueClient({
...options,
throwOnError: options.throwOnError || false,
})
}

export default AuthClient
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 @@
flowType: 'implicit',
debug: false,
hasCustomAuthorizationHeader: false,
throwOnError: false,

Check failure on line 110 in src/GoTrueClient.ts

View workflow job for this annotation

GitHub Actions / Test / OS ubuntu-latest / Node 18

Type '{ url: string; storageKey: string; autoRefreshToken: true; persistSession: true; detectSessionInUrl: true; headers: { 'X-Client-Info': string; }; flowType: "implicit"; debug: false; hasCustomAuthorizationHeader: false; throwOnError: boolean; }' is not assignable to type 'Omit<Required<GoTrueClientOptions>, "storage" | "fetch" | "lock">'.

Check failure on line 110 in src/GoTrueClient.ts

View workflow job for this annotation

GitHub Actions / Test / OS ubuntu-latest / Node 20

Type '{ url: string; storageKey: string; autoRefreshToken: true; persistSession: true; detectSessionInUrl: true; headers: { 'X-Client-Info': string; }; flowType: "implicit"; debug: false; hasCustomAuthorizationHeader: false; throwOnError: boolean; }' is not assignable to type 'Omit<Required<GoTrueClientOptions>, "storage" | "fetch" | "lock">'.
}

/** Current session will be checked for refresh at this interval. */
Expand Down Expand Up @@ -167,6 +168,7 @@
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 @@
this.detectSessionInUrl = settings.detectSessionInUrl
this.flowType = settings.flowType
this.hasCustomAuthorizationHeader = settings.hasCustomAuthorizationHeader
this.throwOnError = settings.throwOnError

Check failure on line 217 in src/GoTrueClient.ts

View workflow job for this annotation

GitHub Actions / Test / OS ubuntu-latest / Node 18

Property 'throwOnError' does not exist on type '{ url: string; headers: { [key: string]: string; }; storageKey: string; detectSessionInUrl: boolean; autoRefreshToken: boolean; persistSession: boolean; storage?: SupportedStorage | undefined; ... 4 more ...; hasCustomAuthorizationHeader: boolean; }'.

Check failure on line 217 in src/GoTrueClient.ts

View workflow job for this annotation

GitHub Actions / Test / OS ubuntu-latest / Node 20

Property 'throwOnError' does not exist on type '{ url: string; headers: { [key: string]: string; }; storageKey: string; detectSessionInUrl: boolean; autoRefreshToken: boolean; persistSession: boolean; storage?: SupportedStorage | undefined; ... 4 more ...; hasCustomAuthorizationHeader: boolean; }'.

if (settings.lock) {
this.lock = settings.lock
Expand Down Expand Up @@ -399,6 +402,7 @@
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 @@
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 @@
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 @@
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 @@
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 @@
}
} 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 @@

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 @@
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 @@
},
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 @@
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 @@
})

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 @@
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 @@
})
} catch (error) {
if (isAuthError(error)) {
if (this.throwOnError) throw error
return { data: null, error }
}
throw error
Expand Down Expand Up @@ -870,10 +893,12 @@
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 @@
},
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@

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 @@
})
} 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 @@
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 @@
(error.status === 404 || error.status === 401 || error.status === 403)
)
) {
if (this.throwOnError) throw error
return { error }
}
}
Expand Down Expand Up @@ -1717,6 +1756,7 @@
})
} catch (error) {
if (isAuthError(error)) {
if (this.throwOnError) throw error
return { data: null, error }
}

Expand All @@ -1742,6 +1782,7 @@
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 @@
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 @@
})
} catch (error) {
if (isAuthError(error)) {
if (this.throwOnError) throw error
return { data: null, error }
}
throw error
Expand Down Expand Up @@ -1858,6 +1901,7 @@
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 @@
})
} catch (error) {
if (isAuthError(error)) {
if (this.throwOnError) throw error
return { data: null, error }
}
throw error
Expand Down Expand Up @@ -2405,6 +2450,7 @@
})

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

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

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

Expand All @@ -2530,6 +2581,7 @@
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 @@
error: sessionError,
} = result
if (sessionError) {
if (this.throwOnError) throw sessionError
return { data: null, error: sessionError }
}
if (!session) {
Expand Down
Loading
Loading