Skip to content

Commit

Permalink
fix: return error if missing session or missing custom auth header
Browse files Browse the repository at this point in the history
  • Loading branch information
kangmingtay committed Apr 25, 2024
1 parent a26f771 commit b684558
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/GoTrueClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ const DEFAULT_OPTIONS: Omit<Required<GoTrueClientOptions>, 'fetch' | 'storage' |
headers: DEFAULT_HEADERS,
flowType: 'implicit',
debug: false,
hasCustomAuthorizationHeader: false,
}

/** Current session will be checked for refresh at this interval. */
Expand Down Expand Up @@ -154,6 +155,7 @@ export default class GoTrueClient {
protected headers: {
[key: string]: string
}
protected hasCustomAuthorizationHeader = false
protected fetch: Fetch
protected lock: LockFunc
protected lockAcquired = false
Expand Down Expand Up @@ -202,6 +204,7 @@ export default class GoTrueClient {
this.lock = settings.lock || lockNoOp
this.detectSessionInUrl = settings.detectSessionInUrl
this.flowType = settings.flowType
this.hasCustomAuthorizationHeader = settings.hasCustomAuthorizationHeader

if (settings.lock) {
this.lock = settings.lock
Expand Down Expand Up @@ -1174,6 +1177,11 @@ export default class GoTrueClient {
throw error
}

// returns an error if there is no access_token or custom authorization header
if (!data.session?.access_token && !this.hasCustomAuthorizationHeader) {
return { data: { user: null }, error: new AuthSessionMissingError() }
}

return await _request(this.fetch, 'GET', `${this.url}/user`, {
headers: this.headers,
jwt: data.session?.access_token ?? undefined,
Expand Down
4 changes: 4 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ export type GoTrueClientOptions = {
* @experimental
*/
lock?: LockFunc
/**
* Set to "true" if there is a custom authorization header
*/
hasCustomAuthorizationHeader?: boolean
}

export type WeakPasswordReasons = 'length' | 'characters' | 'pwned' | string
Expand Down

0 comments on commit b684558

Please sign in to comment.