@supabase/gotrue-js 1.18.0 → 1.21.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/README.md +13 -1
  2. package/dist/main/GoTrueApi.d.ts +52 -17
  3. package/dist/main/GoTrueApi.d.ts.map +1 -1
  4. package/dist/main/GoTrueApi.js +118 -74
  5. package/dist/main/GoTrueApi.js.map +1 -1
  6. package/dist/main/GoTrueClient.d.ts +13 -10
  7. package/dist/main/GoTrueClient.d.ts.map +1 -1
  8. package/dist/main/GoTrueClient.js +49 -55
  9. package/dist/main/GoTrueClient.js.map +1 -1
  10. package/dist/main/lib/cookies.d.ts.map +1 -1
  11. package/dist/main/lib/cookies.js +1 -0
  12. package/dist/main/lib/cookies.js.map +1 -1
  13. package/dist/main/lib/fetch.d.ts +5 -4
  14. package/dist/main/lib/fetch.d.ts.map +1 -1
  15. package/dist/main/lib/fetch.js +10 -10
  16. package/dist/main/lib/fetch.js.map +1 -1
  17. package/dist/main/lib/helpers.d.ts.map +1 -1
  18. package/dist/main/lib/helpers.js +7 -4
  19. package/dist/main/lib/helpers.js.map +1 -1
  20. package/dist/main/lib/polyfills.d.ts.map +1 -1
  21. package/dist/main/lib/polyfills.js +3 -1
  22. package/dist/main/lib/polyfills.js.map +1 -1
  23. package/dist/main/lib/types.d.ts +2 -1
  24. package/dist/main/lib/types.d.ts.map +1 -1
  25. package/dist/main/lib/version.d.ts +1 -1
  26. package/dist/main/lib/version.js +1 -1
  27. package/dist/module/GoTrueApi.d.ts +52 -17
  28. package/dist/module/GoTrueApi.d.ts.map +1 -1
  29. package/dist/module/GoTrueApi.js +111 -67
  30. package/dist/module/GoTrueApi.js.map +1 -1
  31. package/dist/module/GoTrueClient.d.ts +13 -10
  32. package/dist/module/GoTrueClient.d.ts.map +1 -1
  33. package/dist/module/GoTrueClient.js +34 -40
  34. package/dist/module/GoTrueClient.js.map +1 -1
  35. package/dist/module/lib/cookies.d.ts.map +1 -1
  36. package/dist/module/lib/cookies.js +1 -0
  37. package/dist/module/lib/cookies.js.map +1 -1
  38. package/dist/module/lib/fetch.d.ts +5 -4
  39. package/dist/module/lib/fetch.d.ts.map +1 -1
  40. package/dist/module/lib/fetch.js +11 -11
  41. package/dist/module/lib/fetch.js.map +1 -1
  42. package/dist/module/lib/helpers.d.ts.map +1 -1
  43. package/dist/module/lib/helpers.js +5 -3
  44. package/dist/module/lib/helpers.js.map +1 -1
  45. package/dist/module/lib/polyfills.d.ts.map +1 -1
  46. package/dist/module/lib/polyfills.js +3 -1
  47. package/dist/module/lib/polyfills.js.map +1 -1
  48. package/dist/module/lib/types.d.ts +2 -1
  49. package/dist/module/lib/types.d.ts.map +1 -1
  50. package/dist/module/lib/version.d.ts +1 -1
  51. package/dist/module/lib/version.js +1 -1
  52. package/package.json +18 -9
  53. package/src/GoTrueApi.ts +159 -77
  54. package/src/GoTrueClient.ts +50 -52
  55. package/src/lib/cookies.ts +1 -0
  56. package/src/lib/fetch.ts +32 -10
  57. package/src/lib/helpers.ts +4 -3
  58. package/src/lib/polyfills.ts +3 -2
  59. package/src/lib/types.ts +3 -0
  60. package/src/lib/version.ts +1 -1
@@ -1,4 +1,4 @@
1
- import GoTrueApi from './GoTrueApi'
1
+ import GoTrueApi, { ApiError } from './GoTrueApi'
2
2
  import { isBrowser, getParameterByName, uuid } from './lib/helpers'
3
3
  import { GOTRUE_URL, DEFAULT_HEADERS, STORAGE_KEY } from './lib/constants'
4
4
  import {
@@ -13,6 +13,7 @@ import {
13
13
  VerifyOTPParams,
14
14
  } from './lib/types'
15
15
  import { polyfillGlobalThis } from './lib/polyfills'
16
+ import { Fetch } from './lib/fetch'
16
17
 
17
18
  polyfillGlobalThis() // Make "globalThis" available
18
19
 
@@ -65,6 +66,7 @@ export default class GoTrueClient {
65
66
  * @param options.persistSession Set to "true" if you want to automatically save the user session into local storage.
66
67
  * @param options.localStorage
67
68
  * @param options.cookieOptions
69
+ * @param options.fetch A custom fetch implementation.
68
70
  */
69
71
  constructor(options: {
70
72
  url?: string
@@ -74,6 +76,7 @@ export default class GoTrueClient {
74
76
  persistSession?: boolean
75
77
  localStorage?: SupportedStorage
76
78
  cookieOptions?: CookieOptions
79
+ fetch?: Fetch
77
80
  }) {
78
81
  const settings = { ...DEFAULT_OPTIONS, ...options }
79
82
  this.currentUser = null
@@ -85,17 +88,18 @@ export default class GoTrueClient {
85
88
  url: settings.url,
86
89
  headers: settings.headers,
87
90
  cookieOptions: settings.cookieOptions,
91
+ fetch: settings.fetch,
88
92
  })
89
93
  this._recoverSession()
90
94
  this._recoverAndRefresh()
91
95
 
92
96
  // Handle the OAuth redirect
93
- try {
94
- if (settings.detectSessionInUrl && isBrowser() && !!getParameterByName('access_token')) {
95
- this.getSessionFromUrl({ storeSession: true })
96
- }
97
- } catch (error) {
98
- console.log('Error getting session from URL.')
97
+ if (settings.detectSessionInUrl && isBrowser() && !!getParameterByName('access_token')) {
98
+ this.getSessionFromUrl({ storeSession: true }).then(({ error }) => {
99
+ if (error) {
100
+ console.error('Error getting session from URL.', error)
101
+ }
102
+ })
99
103
  }
100
104
  }
101
105
 
@@ -117,7 +121,7 @@ export default class GoTrueClient {
117
121
  ): Promise<{
118
122
  user: User | null
119
123
  session: Session | null
120
- error: Error | null
124
+ error: ApiError | null
121
125
  data: Session | User | null // Deprecated
122
126
  }> {
123
127
  try {
@@ -156,8 +160,8 @@ export default class GoTrueClient {
156
160
  }
157
161
 
158
162
  return { data, user, session, error: null }
159
- } catch (error) {
160
- return { data: null, user: null, session: null, error }
163
+ } catch (e) {
164
+ return { data: null, user: null, session: null, error: e as ApiError }
161
165
  }
162
166
  }
163
167
 
@@ -182,7 +186,7 @@ export default class GoTrueClient {
182
186
  user: User | null
183
187
  provider?: Provider
184
188
  url?: string | null
185
- error: Error | null
189
+ error: ApiError | null
186
190
  data: Session | null // Deprecated
187
191
  }> {
188
192
  try {
@@ -225,8 +229,8 @@ export default class GoTrueClient {
225
229
  })
226
230
  }
227
231
  throw new Error(`You must provide either an email, phone number or a third-party provider.`)
228
- } catch (error) {
229
- return { data: null, user: null, session: null, error }
232
+ } catch (e) {
233
+ return { data: null, user: null, session: null, error: e as ApiError }
230
234
  }
231
235
  }
232
236
 
@@ -244,7 +248,7 @@ export default class GoTrueClient {
244
248
  ): Promise<{
245
249
  user: User | null
246
250
  session: Session | null
247
- error: Error | null
251
+ error: ApiError | null
248
252
  data: Session | User | null // Deprecated
249
253
  }> {
250
254
  try {
@@ -275,8 +279,8 @@ export default class GoTrueClient {
275
279
  }
276
280
 
277
281
  return { data, user, session, error: null }
278
- } catch (error) {
279
- return { data: null, user: null, session: null, error }
282
+ } catch (e) {
283
+ return { data: null, user: null, session: null, error: e as ApiError }
280
284
  }
281
285
  }
282
286
 
@@ -302,7 +306,7 @@ export default class GoTrueClient {
302
306
  async refreshSession(): Promise<{
303
307
  data: Session | null
304
308
  user: User | null
305
- error: Error | null
309
+ error: ApiError | null
306
310
  }> {
307
311
  try {
308
312
  if (!this.currentSession?.access_token) throw new Error('Not logged in.')
@@ -312,8 +316,8 @@ export default class GoTrueClient {
312
316
  if (error) throw error
313
317
 
314
318
  return { data: this.currentSession, user: this.currentUser, error: null }
315
- } catch (error) {
316
- return { data: null, user: null, error }
319
+ } catch (e) {
320
+ return { data: null, user: null, error: e as ApiError }
317
321
  }
318
322
  }
319
323
 
@@ -322,7 +326,7 @@ export default class GoTrueClient {
322
326
  */
323
327
  async update(
324
328
  attributes: UserAttributes
325
- ): Promise<{ data: User | null; user: User | null; error: Error | null }> {
329
+ ): Promise<{ data: User | null; user: User | null; error: ApiError | null }> {
326
330
  try {
327
331
  if (!this.currentSession?.access_token) throw new Error('Not logged in.')
328
332
 
@@ -338,8 +342,8 @@ export default class GoTrueClient {
338
342
  this._notifyAllSubscribers('USER_UPDATED')
339
343
 
340
344
  return { data: user, user, error: null }
341
- } catch (error) {
342
- return { data: null, user: null, error }
345
+ } catch (e) {
346
+ return { data: null, user: null, error: e as ApiError }
343
347
  }
344
348
  }
345
349
 
@@ -349,7 +353,7 @@ export default class GoTrueClient {
349
353
  */
350
354
  async setSession(
351
355
  refresh_token: string
352
- ): Promise<{ session: Session | null; error: Error | null }> {
356
+ ): Promise<{ session: Session | null; error: ApiError | null }> {
353
357
  try {
354
358
  if (!refresh_token) {
355
359
  throw new Error('No current session.')
@@ -358,18 +362,12 @@ export default class GoTrueClient {
358
362
  if (error) {
359
363
  return { session: null, error: error }
360
364
  }
361
- if (!data) {
362
- return {
363
- session: null,
364
- error: { name: 'Invalid refresh_token', message: 'JWT token provided is Invalid' },
365
- }
366
- }
367
365
 
368
- this._saveSession(data)
366
+ this._saveSession(data!)
369
367
  this._notifyAllSubscribers('SIGNED_IN')
370
368
  return { session: data, error: null }
371
- } catch (error) {
372
- return { error, session: null }
369
+ } catch (e) {
370
+ return { error: e as ApiError, session: null }
373
371
  }
374
372
  }
375
373
 
@@ -394,7 +392,7 @@ export default class GoTrueClient {
394
392
  */
395
393
  async getSessionFromUrl(options?: {
396
394
  storeSession?: boolean
397
- }): Promise<{ data: Session | null; error: Error | null }> {
395
+ }): Promise<{ data: Session | null; error: ApiError | null }> {
398
396
  try {
399
397
  if (!isBrowser()) throw new Error('No browser detected.')
400
398
 
@@ -437,8 +435,8 @@ export default class GoTrueClient {
437
435
  window.location.hash = ''
438
436
 
439
437
  return { data: session, error: null }
440
- } catch (error) {
441
- return { data: null, error }
438
+ } catch (e) {
439
+ return { data: null, error: e as ApiError }
442
440
  }
443
441
  }
444
442
 
@@ -448,7 +446,7 @@ export default class GoTrueClient {
448
446
  *
449
447
  * For server-side management, you can disable sessions by passing a JWT through to `auth.api.signOut(JWT: string)`
450
448
  */
451
- async signOut(): Promise<{ error: Error | null }> {
449
+ async signOut(): Promise<{ error: ApiError | null }> {
452
450
  const accessToken = this.currentSession?.access_token
453
451
  this._removeSession()
454
452
  this._notifyAllSubscribers('SIGNED_OUT')
@@ -463,23 +461,23 @@ export default class GoTrueClient {
463
461
  * Receive a notification every time an auth event happens.
464
462
  * @returns {Subscription} A subscription object which can be used to unsubscribe itself.
465
463
  */
466
- onAuthStateChange(
467
- callback: (event: AuthChangeEvent, session: Session | null) => void
468
- ): { data: Subscription | null; error: Error | null } {
464
+ onAuthStateChange(callback: (event: AuthChangeEvent, session: Session | null) => void): {
465
+ data: Subscription | null
466
+ error: ApiError | null
467
+ } {
469
468
  try {
470
469
  const id: string = uuid()
471
- const self = this
472
470
  const subscription: Subscription = {
473
471
  id,
474
472
  callback,
475
473
  unsubscribe: () => {
476
- self.stateChangeEmitters.delete(id)
474
+ this.stateChangeEmitters.delete(id)
477
475
  },
478
476
  }
479
477
  this.stateChangeEmitters.set(id, subscription)
480
478
  return { data: subscription, error: null }
481
- } catch (error) {
482
- return { data: null, error }
479
+ } catch (e) {
480
+ return { data: null, error: e as ApiError }
483
481
  }
484
482
  }
485
483
 
@@ -502,8 +500,8 @@ export default class GoTrueClient {
502
500
  }
503
501
 
504
502
  return { data, user: data.user, session: data, error: null }
505
- } catch (error) {
506
- return { data: null, user: null, session: null, error }
503
+ } catch (e) {
504
+ return { data: null, user: null, session: null, error: e as ApiError }
507
505
  }
508
506
  }
509
507
 
@@ -518,8 +516,8 @@ export default class GoTrueClient {
518
516
  }
519
517
 
520
518
  return { data, user: data.user, session: data, error: null }
521
- } catch (error) {
522
- return { data: null, user: null, session: null, error }
519
+ } catch (e) {
520
+ return { data: null, user: null, session: null, error: e as ApiError }
523
521
  }
524
522
  }
525
523
 
@@ -541,10 +539,10 @@ export default class GoTrueClient {
541
539
  window.location.href = url
542
540
  }
543
541
  return { provider, url, data: null, session: null, user: null, error: null }
544
- } catch (error) {
542
+ } catch (e) {
545
543
  // fallback to returning the URL
546
- if (!!url) return { provider, url, data: null, session: null, user: null, error: null }
547
- return { data: null, user: null, session: null, error }
544
+ if (url) return { provider, url, data: null, session: null, user: null, error: null }
545
+ return { data: null, user: null, session: null, error: e as ApiError }
548
546
  }
549
547
  }
550
548
 
@@ -625,8 +623,8 @@ export default class GoTrueClient {
625
623
  this._notifyAllSubscribers('SIGNED_IN')
626
624
 
627
625
  return { data, error: null }
628
- } catch (error) {
629
- return { data: null, error }
626
+ } catch (e) {
627
+ return { data: null, error: e as ApiError }
630
628
  }
631
629
  }
632
630
 
@@ -25,6 +25,7 @@ function serialize(
25
25
  ) {
26
26
  const opt = options || {}
27
27
  const enc = encodeURIComponent
28
+ /* eslint-disable-next-line no-control-regex */
28
29
  const fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/
29
30
 
30
31
  if (typeof enc !== 'function') {
package/src/lib/fetch.ts CHANGED
@@ -1,4 +1,6 @@
1
- import fetch from 'cross-fetch'
1
+ import crossFetch from 'cross-fetch'
2
+
3
+ export type Fetch = typeof fetch
2
4
 
3
5
  export interface FetchOptions {
4
6
  headers?: {
@@ -38,13 +40,14 @@ const _getRequestParams = (method: RequestMethodType, options?: FetchOptions, bo
38
40
  }
39
41
 
40
42
  async function _handleRequest(
43
+ fetcher: Fetch = crossFetch,
41
44
  method: RequestMethodType,
42
45
  url: string,
43
46
  options?: FetchOptions,
44
47
  body?: object
45
48
  ): Promise<any> {
46
49
  return new Promise((resolve, reject) => {
47
- fetch(url, _getRequestParams(method, options, body))
50
+ fetcher(url, _getRequestParams(method, options, body))
48
51
  .then((result) => {
49
52
  if (!result.ok) throw result
50
53
  if (options?.noResolveJson) return resolve
@@ -55,18 +58,37 @@ async function _handleRequest(
55
58
  })
56
59
  }
57
60
 
58
- export async function get(url: string, options?: FetchOptions): Promise<any> {
59
- return _handleRequest('GET', url, options)
61
+ export async function get(
62
+ fetcher: Fetch | undefined,
63
+ url: string,
64
+ options?: FetchOptions
65
+ ): Promise<any> {
66
+ return _handleRequest(fetcher, 'GET', url, options)
60
67
  }
61
68
 
62
- export async function post(url: string, body: object, options?: FetchOptions): Promise<any> {
63
- return _handleRequest('POST', url, options, body)
69
+ export async function post(
70
+ fetcher: Fetch | undefined,
71
+ url: string,
72
+ body: object,
73
+ options?: FetchOptions
74
+ ): Promise<any> {
75
+ return _handleRequest(fetcher, 'POST', url, options, body)
64
76
  }
65
77
 
66
- export async function put(url: string, body: object, options?: FetchOptions): Promise<any> {
67
- return _handleRequest('PUT', url, options, body)
78
+ export async function put(
79
+ fetcher: Fetch | undefined,
80
+ url: string,
81
+ body: object,
82
+ options?: FetchOptions
83
+ ): Promise<any> {
84
+ return _handleRequest(fetcher, 'PUT', url, options, body)
68
85
  }
69
86
 
70
- export async function remove(url: string, body: object, options?: FetchOptions): Promise<any> {
71
- return _handleRequest('DELETE', url, options, body)
87
+ export async function remove(
88
+ fetcher: Fetch | undefined,
89
+ url: string,
90
+ body: object,
91
+ options?: FetchOptions
92
+ ): Promise<any> {
93
+ return _handleRequest(fetcher, 'DELETE', url, options, body)
72
94
  }
@@ -5,7 +5,7 @@ export function expiresAt(expiresIn: number) {
5
5
 
6
6
  export function uuid() {
7
7
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
8
- var r = (Math.random() * 16) | 0,
8
+ const r = (Math.random() * 16) | 0,
9
9
  v = c == 'x' ? r : (r & 0x3) | 0x8
10
10
  return v.toString(16)
11
11
  })
@@ -14,9 +14,10 @@ export function uuid() {
14
14
  export const isBrowser = () => typeof window !== 'undefined'
15
15
 
16
16
  export function getParameterByName(name: string, url?: string) {
17
- if (!url) url = window.location.href
17
+ if (!url) url = window?.location?.href || ''
18
+ // eslint-disable-next-line no-useless-escape
18
19
  name = name.replace(/[\[\]]/g, '\\$&')
19
- var regex = new RegExp('[?&#]' + name + '(=([^&#]*)|&|#|$)'),
20
+ const regex = new RegExp('[?&#]' + name + '(=([^&#]*)|&|#|$)'),
20
21
  results = regex.exec(url)
21
22
  if (!results) return null
22
23
  if (!results[2]) return ''
@@ -1,5 +1,3 @@
1
- // @ts-nocheck
2
-
3
1
  /**
4
2
  * https://mathiasbynens.be/notes/globalthis
5
3
  */
@@ -12,10 +10,13 @@ export function polyfillGlobalThis() {
12
10
  },
13
11
  configurable: true,
14
12
  })
13
+ // @ts-expect-error 'Allow access to magic'
15
14
  __magic__.globalThis = __magic__
15
+ // @ts-expect-error 'Allow access to magic'
16
16
  delete Object.prototype.__magic__
17
17
  } catch (e) {
18
18
  if (typeof self !== 'undefined') {
19
+ // @ts-expect-error 'Allow access to globals'
19
20
  self.globalThis = self
20
21
  }
21
22
  }
package/src/lib/types.ts CHANGED
@@ -9,6 +9,8 @@ export type Provider =
9
9
  | 'apple'
10
10
  | 'discord'
11
11
  | 'twitch'
12
+ | 'spotify'
13
+ | 'slack'
12
14
 
13
15
  export type AuthChangeEvent =
14
16
  | 'SIGNED_IN'
@@ -44,6 +46,7 @@ export interface User {
44
46
  aud: string
45
47
  confirmation_sent_at?: string
46
48
  recovery_sent_at?: string
49
+ invited_at?: string
47
50
  action_link?: string
48
51
  email?: string
49
52
  phone?: string
@@ -1,2 +1,2 @@
1
1
  // generated by genversion
2
- export const version = '1.18.0'
2
+ export const version = '1.21.1'