• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

supabase / auth-js / 21284248075

23 Jan 2026 11:14AM UTC coverage: 70.669%. Remained the same
21284248075

push

github

web-flow
docs: small readme update

1083 of 1681 branches covered (64.43%)

Branch coverage included in aggregate %.

1495 of 1967 relevant lines covered (76.0%)

149.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.06
/src/lib/helpers.ts
1
import { API_VERSION_HEADER_NAME, BASE64URL_REGEX } from './constants'
14✔
2
import { AuthInvalidJwtError } from './errors'
14✔
3
import { base64UrlToUint8Array, stringFromBase64URL } from './base64url'
14✔
4
import { JwtHeader, JwtPayload, SupportedStorage, User } from './types'
5
import { Uint8Array_ } from './webauthn.dom'
6

7
export function expiresAt(expiresIn: number) {
14✔
8
  const timeNow = Math.round(Date.now() / 1000)
×
9
  return timeNow + expiresIn
×
10
}
11

12
export function uuid() {
14✔
13
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
10✔
14
    const r = (Math.random() * 16) | 0,
310✔
15
      v = c == 'x' ? r : (r & 0x3) | 0x8
310✔
16
    return v.toString(16)
310✔
17
  })
18
}
19

20
export const isBrowser = () => typeof window !== 'undefined' && typeof document !== 'undefined'
884✔
21

22
const localStorageWriteTests = {
14✔
23
  tested: false,
24
  writable: false,
25
}
26

27
/**
28
 * Checks whether localStorage is supported on this browser.
29
 */
30
export const supportsLocalStorage = () => {
14✔
31
  if (!isBrowser()) {
50✔
32
    return false
26✔
33
  }
34

35
  try {
24✔
36
    if (typeof globalThis.localStorage !== 'object') {
24✔
37
      return false
20✔
38
    }
39
  } catch (e) {
40
    // DOM exception when accessing `localStorage`
41
    return false
×
42
  }
43

44
  if (localStorageWriteTests.tested) {
4✔
45
    return localStorageWriteTests.writable
2✔
46
  }
47

48
  const randomKey = `lswt-${Math.random()}${Math.random()}`
2✔
49

50
  try {
2✔
51
    globalThis.localStorage.setItem(randomKey, randomKey)
2✔
52
    globalThis.localStorage.removeItem(randomKey)
2✔
53

54
    localStorageWriteTests.tested = true
2✔
55
    localStorageWriteTests.writable = true
2✔
56
  } catch (e) {
57
    // localStorage can't be written to
58
    // https://www.chromium.org/for-testers/bug-reporting-guidelines/uncaught-securityerror-failed-to-read-the-localstorage-property-from-window-access-is-denied-for-this-document
59

60
    localStorageWriteTests.tested = true
×
61
    localStorageWriteTests.writable = false
×
62
  }
63

64
  return localStorageWriteTests.writable
2✔
65
}
66

67
/**
68
 * Extracts parameters encoded in the URL both in the query and fragment.
69
 */
70
export function parseParametersFromURL(href: string) {
14✔
71
  const result: { [parameter: string]: string } = {}
76✔
72

73
  const url = new URL(href)
76✔
74

75
  if (url.hash && url.hash[0] === '#') {
76✔
76
    try {
8✔
77
      const hashSearchParams = new URLSearchParams(url.hash.substring(1))
8✔
78
      hashSearchParams.forEach((value, key) => {
8✔
79
        result[key] = value
22✔
80
      })
81
    } catch (e: any) {
82
      // hash is not a query string
83
    }
84
  }
85

86
  // search parameters take precedence over hash parameters
87
  url.searchParams.forEach((value, key) => {
76✔
88
    result[key] = value
44✔
89
  })
90

91
  return result
76✔
92
}
93

94
type Fetch = typeof fetch
95

96
export const resolveFetch = (customFetch?: Fetch): Fetch => {
14✔
97
  let _fetch: Fetch
98
  if (customFetch) {
406✔
99
    _fetch = customFetch
38✔
100
  } else if (typeof fetch === 'undefined') {
368✔
101
    _fetch = (...args) =>
54✔
102
      import('@supabase/node-fetch' as any).then(({ default: fetch }) => fetch(...args))
4✔
103
  } else {
104
    _fetch = fetch
314✔
105
  }
106
  return (...args) => _fetch(...args)
464✔
107
}
108

109
export const looksLikeFetchResponse = (maybeResponse: unknown): maybeResponse is Response => {
14✔
110
  return (
135✔
111
    typeof maybeResponse === 'object' &&
810✔
112
    maybeResponse !== null &&
113
    'status' in maybeResponse &&
114
    'ok' in maybeResponse &&
115
    'json' in maybeResponse &&
116
    typeof (maybeResponse as any).json === 'function'
117
  )
118
}
119

120
// Storage helpers
121
export const setItemAsync = async (
14✔
122
  storage: SupportedStorage,
123
  key: string,
124
  data: any
125
): Promise<void> => {
126
  await storage.setItem(key, JSON.stringify(data))
203✔
127
}
128

129
export const getItemAsync = async (storage: SupportedStorage, key: string): Promise<unknown> => {
14✔
130
  const value = await storage.getItem(key)
585✔
131

132
  if (!value) {
581✔
133
    return null
336✔
134
  }
135

136
  try {
245✔
137
    return JSON.parse(value)
245✔
138
  } catch {
139
    return value
10✔
140
  }
141
}
142

143
export const removeItemAsync = async (storage: SupportedStorage, key: string): Promise<void> => {
14✔
144
  await storage.removeItem(key)
1,030✔
145
}
146

147
/**
148
 * A deferred represents some asynchronous work that is not yet finished, which
149
 * may or may not culminate in a value.
150
 * Taken from: https://github.com/mike-north/types/blob/master/src/async.ts
151
 */
152
export class Deferred<T = any> {
14✔
153
  public static promiseConstructor: PromiseConstructor = Promise
14✔
154

155
  public readonly promise!: PromiseLike<T>
156

157
  public readonly resolve!: (value?: T | PromiseLike<T>) => void
158

159
  public readonly reject!: (reason?: any) => any
160

161
  public constructor() {
162
    // eslint-disable-next-line @typescript-eslint/no-extra-semi
163
    ;(this as any).promise = new Deferred.promiseConstructor((res, rej) => {
32✔
164
      // eslint-disable-next-line @typescript-eslint/no-extra-semi
165
      ;(this as any).resolve = res
32✔
166
      // eslint-disable-next-line @typescript-eslint/no-extra-semi
167
      ;(this as any).reject = rej
32✔
168
    })
169
  }
170
}
171

172
export function decodeJWT(token: string): {
14✔
173
  header: JwtHeader
174
  payload: JwtPayload
175
  signature: Uint8Array_
176
  raw: {
177
    header: string
178
    payload: string
179
  }
180
} {
181
  const parts = token.split('.')
29✔
182

183
  if (parts.length !== 3) {
29✔
184
    throw new AuthInvalidJwtError('Invalid JWT structure')
6✔
185
  }
186

187
  // Regex checks for base64url format
188
  for (let i = 0; i < parts.length; i++) {
23✔
189
    if (!BASE64URL_REGEX.test(parts[i] as string)) {
67✔
190
      throw new AuthInvalidJwtError('JWT not in base64url format')
4✔
191
    }
192
  }
193
  const data = {
19✔
194
    // using base64url lib
195
    header: JSON.parse(stringFromBase64URL(parts[0])),
196
    payload: JSON.parse(stringFromBase64URL(parts[1])),
197
    signature: base64UrlToUint8Array(parts[2]),
198
    raw: {
199
      header: parts[0],
200
      payload: parts[1],
201
    },
202
  }
203
  return data
19✔
204
}
205

206
/**
207
 * Creates a promise that resolves to null after some time.
208
 */
209
export async function sleep(time: number): Promise<null> {
14✔
210
  return await new Promise((accept) => {
2✔
211
    setTimeout(() => accept(null), time)
2✔
212
  })
213
}
214

215
/**
216
 * Converts the provided async function into a retryable function. Each result
217
 * or thrown error is sent to the isRetryable function which should return true
218
 * if the function should run again.
219
 */
220
export function retryable<T>(
14✔
221
  fn: (attempt: number) => Promise<T>,
222
  isRetryable: (attempt: number, error: any | null, result?: T) => boolean
223
): Promise<T> {
224
  const promise = new Promise<T>((accept, reject) => {
28✔
225
    // eslint-disable-next-line @typescript-eslint/no-extra-semi
226
    ;(async () => {
28✔
227
      for (let attempt = 0; attempt < Infinity; attempt++) {
28✔
228
        try {
28✔
229
          const result = await fn(attempt)
28✔
230

231
          if (!isRetryable(attempt, null, result)) {
18✔
232
            accept(result)
18✔
233
            return
18✔
234
          }
235
        } catch (e: any) {
236
          if (!isRetryable(attempt, e)) {
10✔
237
            reject(e)
10✔
238
            return
10✔
239
          }
240
        }
241
      }
242
    })()
243
  })
244

245
  return promise
28✔
246
}
247

248
function dec2hex(dec: number) {
249
  return ('0' + dec.toString(16)).substr(-2)
616✔
250
}
251

252
// Functions below taken from: https://stackoverflow.com/questions/63309409/creating-a-code-verifier-and-challenge-for-pkce-auth-on-spotify-api-in-reactjs
253
export function generatePKCEVerifier() {
14✔
254
  const verifierLength = 56
24✔
255
  const array = new Uint32Array(verifierLength)
24✔
256
  if (typeof crypto === 'undefined') {
24✔
257
    const charSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~'
13✔
258
    const charSetLen = charSet.length
13✔
259
    let verifier = ''
13✔
260
    for (let i = 0; i < verifierLength; i++) {
13✔
261
      verifier += charSet.charAt(Math.floor(Math.random() * charSetLen))
728✔
262
    }
263
    return verifier
13✔
264
  }
265
  crypto.getRandomValues(array)
11✔
266
  return Array.from(array, dec2hex).join('')
11✔
267
}
268

269
async function sha256(randomString: string) {
270
  const encoder = new TextEncoder()
11✔
271
  const encodedData = encoder.encode(randomString)
11✔
272
  const hash = await crypto.subtle.digest('SHA-256', encodedData)
11✔
273
  const bytes = new Uint8Array(hash)
11✔
274

275
  return Array.from(bytes)
11✔
276
    .map((c) => String.fromCharCode(c))
352✔
277
    .join('')
278
}
279

280
export async function generatePKCEChallenge(verifier: string) {
14✔
281
  const hasCryptoSupport =
282
    typeof crypto !== 'undefined' &&
24✔
283
    typeof crypto.subtle !== 'undefined' &&
284
    typeof TextEncoder !== 'undefined'
285

286
  if (!hasCryptoSupport) {
24✔
287
    console.warn(
13✔
288
      'WebCrypto API is not supported. Code challenge method will default to use plain instead of sha256.'
289
    )
290
    return verifier
13✔
291
  }
292
  const hashed = await sha256(verifier)
11✔
293
  return btoa(hashed).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '')
11✔
294
}
295

296
export async function getCodeChallengeAndMethod(
14✔
297
  storage: SupportedStorage,
298
  storageKey: string,
299
  isPasswordRecovery = false
20✔
300
) {
301
  const codeVerifier = generatePKCEVerifier()
24✔
302
  let storedCodeVerifier = codeVerifier
24✔
303
  if (isPasswordRecovery) {
24✔
304
    storedCodeVerifier += '/PASSWORD_RECOVERY'
2✔
305
  }
306
  await setItemAsync(storage, `${storageKey}-code-verifier`, storedCodeVerifier)
24✔
307
  const codeChallenge = await generatePKCEChallenge(codeVerifier)
24✔
308
  const codeChallengeMethod = codeVerifier === codeChallenge ? 'plain' : 's256'
24✔
309
  return [codeChallenge, codeChallengeMethod]
24✔
310
}
311

312
/** Parses the API version which is 2YYY-MM-DD. */
313
const API_VERSION_REGEX = /^2[0-9]{3}-(0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-9]|3[0-1])$/i
14✔
314

315
export function parseResponseAPIVersion(response: Response) {
14✔
316
  const apiVersion = response.headers.get(API_VERSION_HEADER_NAME)
141✔
317

318
  if (!apiVersion) {
141✔
319
    return null
16✔
320
  }
321

322
  if (!apiVersion.match(API_VERSION_REGEX)) {
125✔
323
    return null
6✔
324
  }
325

326
  try {
119✔
327
    const date = new Date(`${apiVersion}T00:00:00.0Z`)
119✔
328
    return date
119✔
329
  } catch (e: any) {
330
    return null
×
331
  }
332
}
333

334
export function validateExp(exp: number) {
14✔
335
  if (!exp) {
13✔
336
    throw new Error('Missing exp claim')
2✔
337
  }
338
  const timeNow = Math.floor(Date.now() / 1000)
11✔
339
  if (exp <= timeNow) {
11✔
340
    throw new Error('JWT has expired')
2✔
341
  }
342
}
343

344
export function getAlgorithm(
14✔
345
  alg: 'HS256' | 'RS256' | 'ES256'
346
): RsaHashedImportParams | EcKeyImportParams {
347
  switch (alg) {
8✔
348
    case 'RS256':
349
      return {
4✔
350
        name: 'RSASSA-PKCS1-v1_5',
351
        hash: { name: 'SHA-256' },
352
      }
353
    case 'ES256':
354
      return {
2✔
355
        name: 'ECDSA',
356
        namedCurve: 'P-256',
357
        hash: { name: 'SHA-256' },
358
      }
359
    default:
360
      throw new Error('Invalid alg claim')
2✔
361
  }
362
}
363

364
const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/
14✔
365

366
export function validateUUID(str: string) {
14✔
367
  if (!UUID_REGEX.test(str)) {
42✔
368
    throw new Error('@supabase/auth-js: Expected parameter to be UUID but is not')
6✔
369
  }
370
}
371

372
export function userNotAvailableProxy(): User {
14✔
373
  const proxyTarget = {} as User
16✔
374

375
  return new Proxy(proxyTarget, {
16✔
376
    get: (target: any, prop: string) => {
377
      if (prop === '__isUserNotAvailableProxy') {
8✔
378
        return true
6✔
379
      }
380
      // Preventative check for common problematic symbols during cloning/inspection
381
      // These symbols might be accessed by structuredClone or other internal mechanisms.
382
      if (typeof prop === 'symbol') {
2!
383
        const sProp = (prop as symbol).toString()
×
384
        if (
×
385
          sProp === 'Symbol(Symbol.toPrimitive)' ||
×
386
          sProp === 'Symbol(Symbol.toStringTag)' ||
387
          sProp === 'Symbol(util.inspect.custom)'
388
        ) {
389
          // Node.js util.inspect
390
          return undefined
×
391
        }
392
      }
393
      throw new Error(
2✔
394
        `@supabase/auth-js: client was created with userStorage option and there was no user stored in the user storage. Accessing the "${prop}" property of the session object is not supported. Please use getUser() instead.`
395
      )
396
    },
397
    set: (_target: any, prop: string) => {
398
      throw new Error(
2✔
399
        `@supabase/auth-js: client was created with userStorage option and there was no user stored in the user storage. Setting the "${prop}" property of the session object is not supported. Please use getUser() to fetch a user object you can manipulate.`
400
      )
401
    },
402
    deleteProperty: (_target: any, prop: string) => {
403
      throw new Error(
×
404
        `@supabase/auth-js: client was created with userStorage option and there was no user stored in the user storage. Deleting the "${prop}" property of the session object is not supported. Please use getUser() to fetch a user object you can manipulate.`
405
      )
406
    },
407
  })
408
}
409

410
/**
411
 * Deep clones a JSON-serializable object using JSON.parse(JSON.stringify(obj)).
412
 * Note: Only works for JSON-safe data.
413
 */
414
export function deepClone<T>(obj: T): T {
14✔
415
  return JSON.parse(JSON.stringify(obj))
177✔
416
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc