-
-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathGoTrueClient.ts
714 lines (636 loc) · 21.7 KB
/
GoTrueClient.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
import GoTrueApi from './GoTrueApi'
import { isBrowser, getParameterByName, uuid } from './lib/helpers'
import { GOTRUE_URL, DEFAULT_HEADERS, STORAGE_KEY } from './lib/constants'
import { polyfillGlobalThis } from './lib/polyfills'
import { Fetch } from './lib/fetch'
import type {
ApiError,
Session,
User,
UserAttributes,
Provider,
Subscription,
AuthChangeEvent,
CookieOptions,
UserCredentials,
VerifyOTPParams,
} from './lib/types'
polyfillGlobalThis() // Make "globalThis" available
const DEFAULT_OPTIONS = {
url: GOTRUE_URL,
autoRefreshToken: true,
persistSession: true,
detectSessionInUrl: true,
multiTab: true,
headers: DEFAULT_HEADERS,
}
type AnyFunction = (...args: any[]) => any
type MaybePromisify<T> = T | Promise<T>
type PromisifyMethods<T> = {
[K in keyof T]: T[K] extends AnyFunction
? (...args: Parameters<T[K]>) => MaybePromisify<ReturnType<T[K]>>
: T[K]
}
type SupportedStorage = PromisifyMethods<Pick<Storage, 'getItem' | 'setItem' | 'removeItem'>>
export default class GoTrueClient {
/**
* Namespace for the GoTrue API methods.
* These can be used for example to get a user from a JWT in a server environment or reset a user's password.
*/
api: GoTrueApi
/**
* The currently logged in user or null.
*/
protected currentUser: User | null
/**
* The session object for the currently logged in user or null.
*/
protected currentSession: Session | null
protected autoRefreshToken: boolean
protected persistSession: boolean
protected localStorage: SupportedStorage
protected multiTab: boolean
protected stateChangeEmitters: Map<string, Subscription> = new Map()
protected refreshTokenTimer?: ReturnType<typeof setTimeout>
/**
* Create a new client for use in the browser.
* @param options.url The URL of the GoTrue server.
* @param options.headers Any additional headers to send to the GoTrue server.
* @param options.detectSessionInUrl Set to "true" if you want to automatically detects OAuth grants in the URL and signs in the user.
* @param options.autoRefreshToken Set to "true" if you want to automatically refresh the token before expiring.
* @param options.persistSession Set to "true" if you want to automatically save the user session into local storage.
* @param options.localStorage Provide your own local storage implementation to use instead of the browser's local storage.
* @param options.multiTab Set to "false" if you want to disable multi-tab/window events.
* @param options.cookieOptions
* @param options.fetch A custom fetch implementation.
*/
constructor(options: {
url?: string
headers?: { [key: string]: string }
detectSessionInUrl?: boolean
autoRefreshToken?: boolean
persistSession?: boolean
localStorage?: SupportedStorage
multiTab?: boolean
cookieOptions?: CookieOptions
fetch?: Fetch
}) {
const settings = { ...DEFAULT_OPTIONS, ...options }
this.currentUser = null
this.currentSession = null
this.autoRefreshToken = settings.autoRefreshToken
this.persistSession = settings.persistSession
this.multiTab = settings.multiTab
this.localStorage = settings.localStorage || globalThis.localStorage
this.api = new GoTrueApi({
url: settings.url,
headers: settings.headers,
cookieOptions: settings.cookieOptions,
fetch: settings.fetch,
})
this._recoverSession()
this._recoverAndRefresh()
this._listenForMultiTabEvents()
if (settings.detectSessionInUrl && isBrowser() && !!getParameterByName('access_token')) {
// Handle the OAuth redirect
this.getSessionFromUrl({ storeSession: true }).then(({ error }) => {
if (error) {
console.error('Error getting session from URL.', error)
}
})
}
}
/**
* Creates a new user.
* @type UserCredentials
* @param email The user's email address.
* @param password The user's password.
* @param phone The user's phone number.
* @param redirectTo A URL or mobile address to send the user to after they are confirmed.
* @param data Optional user metadata.
*/
async signUp(
{ email, password, phone }: UserCredentials,
options: {
redirectTo?: string
data?: object
} = {}
): Promise<{
user: User | null
session: Session | null
error: ApiError | null
}> {
try {
this._removeSession()
const { data, error } =
phone && password
? await this.api.signUpWithPhone(phone!, password!, {
data: options.data,
})
: await this.api.signUpWithEmail(email!, password!, {
redirectTo: options.redirectTo,
data: options.data,
})
if (error) {
throw error
}
if (!data) {
throw 'An error occurred on sign up.'
}
let session: Session | null = null
let user: User | null = null
if ((data as Session).access_token) {
session = data as Session
user = session.user as User
this._saveSession(session)
this._notifyAllSubscribers('SIGNED_IN')
}
if ((data as User).id) {
user = data as User
}
return { user, session, error: null }
} catch (e) {
return { user: null, session: null, error: e as ApiError }
}
}
/**
* Log in an existing user, or login via a third-party provider.
* @type UserCredentials
* @param email The user's email address.
* @param password The user's password.
* @param refreshToken A valid refresh token that was returned on login.
* @param provider One of the providers supported by GoTrue.
* @param redirectTo A URL or mobile address to send the user to after they are confirmed.
* @param scopes A space-separated list of scopes granted to the OAuth application.
*/
async signIn(
{ email, phone, password, refreshToken, provider }: UserCredentials,
options: {
redirectTo?: string
scopes?: string
} = {}
): Promise<{
session: Session | null
user: User | null
provider?: Provider
url?: string | null
error: ApiError | null
}> {
try {
this._removeSession()
if (email && !password) {
const { error } = await this.api.sendMagicLinkEmail(email, {
redirectTo: options.redirectTo,
})
return { user: null, session: null, error }
}
if (email && password) {
return this._handleEmailSignIn(email, password, {
redirectTo: options.redirectTo,
})
}
if (phone && !password) {
const { error } = await this.api.sendMobileOTP(phone)
return { user: null, session: null, error }
}
if (phone && password) {
return this._handlePhoneSignIn(phone, password)
}
if (refreshToken) {
// currentSession and currentUser will be updated to latest on _callRefreshToken using the passed refreshToken
const { error } = await this._callRefreshToken(refreshToken)
if (error) throw error
return {
user: this.currentUser,
session: this.currentSession,
error: null,
}
}
if (provider) {
return this._handleProviderSignIn(provider, {
redirectTo: options.redirectTo,
scopes: options.scopes,
})
}
throw new Error(`You must provide either an email, phone number or a third-party provider.`)
} catch (e) {
return { user: null, session: null, error: e as ApiError }
}
}
/**
* Log in a user given a User supplied OTP received via mobile.
* @param phone The user's phone number.
* @param token The user's password.
* @param redirectTo A URL or mobile address to send the user to after they are confirmed.
*/
async verifyOTP(
{ phone, token }: VerifyOTPParams,
options: {
redirectTo?: string
} = {}
): Promise<{
user: User | null
session: Session | null
error: ApiError | null
}> {
try {
this._removeSession()
const { data, error } = await this.api.verifyMobileOTP(phone, token, options)
if (error) {
throw error
}
if (!data) {
throw 'An error occurred on token verification.'
}
let session: Session | null = null
let user: User | null = null
if ((data as Session).access_token) {
session = data as Session
user = session.user as User
this._saveSession(session)
this._notifyAllSubscribers('SIGNED_IN')
}
if ((data as User).id) {
user = data as User
}
return { user, session, error: null }
} catch (e) {
return { user: null, session: null, error: e as ApiError }
}
}
/**
* Inside a browser context, `user()` will return the user data, if there is a logged in user.
*
* For server-side management, you can get a user through `auth.api.getUserByCookie()`
*/
user(): User | null {
return this.currentUser
}
/**
* Returns the session data, if there is an active session.
*/
session(): Session | null {
return this.currentSession
}
/**
* Force refreshes the session including the user data in case it was updated in a different session.
*/
async refreshSession(): Promise<{
data: Session | null
user: User | null
error: ApiError | null
}> {
try {
if (!this.currentSession?.access_token) throw new Error('Not logged in.')
// currentSession and currentUser will be updated to latest on _callRefreshToken
const { error } = await this._callRefreshToken()
if (error) throw error
return { data: this.currentSession, user: this.currentUser, error: null }
} catch (e) {
return { data: null, user: null, error: e as ApiError }
}
}
/**
* Updates user data, if there is a logged in user.
*/
async update(
attributes: UserAttributes
): Promise<{ data: User | null; user: User | null; error: ApiError | null }> {
try {
if (!this.currentSession?.access_token) throw new Error('Not logged in.')
const { user, error } = await this.api.updateUser(
this.currentSession.access_token,
attributes
)
if (error) throw error
if (!user) throw Error('Invalid user data.')
const session = { ...this.currentSession, user }
this._saveSession(session)
this._notifyAllSubscribers('USER_UPDATED')
return { data: user, user, error: null }
} catch (e) {
return { data: null, user: null, error: e as ApiError }
}
}
/**
* Sets the session data from refresh_token and returns current Session and Error
* @param refresh_token a JWT token
*/
async setSession(
refresh_token: string
): Promise<{ session: Session | null; error: ApiError | null }> {
try {
if (!refresh_token) {
throw new Error('No current session.')
}
const { data, error } = await this.api.refreshAccessToken(refresh_token)
if (error) {
return { session: null, error: error }
}
this._saveSession(data!)
this._notifyAllSubscribers('SIGNED_IN')
return { session: data, error: null }
} catch (e) {
return { error: e as ApiError, session: null }
}
}
/**
* Overrides the JWT on the current client. The JWT will then be sent in all subsequent network requests.
* @param access_token a jwt access token
*/
setAuth(access_token: string): Session {
this.currentSession = {
...this.currentSession,
access_token,
token_type: 'bearer',
user: null,
}
return this.currentSession
}
/**
* Gets the session data from a URL string
* @param options.storeSession Optionally store the session in the browser
*/
async getSessionFromUrl(options?: {
storeSession?: boolean
}): Promise<{ data: Session | null; error: ApiError | null }> {
try {
if (!isBrowser()) throw new Error('No browser detected.')
const error_description = getParameterByName('error_description')
if (error_description) throw new Error(error_description)
const provider_token = getParameterByName('provider_token')
const access_token = getParameterByName('access_token')
if (!access_token) throw new Error('No access_token detected.')
const expires_in = getParameterByName('expires_in')
if (!expires_in) throw new Error('No expires_in detected.')
const refresh_token = getParameterByName('refresh_token')
if (!refresh_token) throw new Error('No refresh_token detected.')
const token_type = getParameterByName('token_type')
if (!token_type) throw new Error('No token_type detected.')
const timeNow = Math.round(Date.now() / 1000)
const expires_at = timeNow + parseInt(expires_in)
const { user, error } = await this.api.getUser(access_token)
if (error) throw error
const session: Session = {
provider_token,
access_token,
expires_in: parseInt(expires_in),
expires_at,
refresh_token,
token_type,
user: user!,
}
if (options?.storeSession) {
this._saveSession(session)
const recoveryMode = getParameterByName('type')
this._notifyAllSubscribers('SIGNED_IN')
if (recoveryMode === 'recovery') {
this._notifyAllSubscribers('PASSWORD_RECOVERY')
}
}
// Remove tokens from URL
window.location.hash = ''
return { data: session, error: null }
} catch (e) {
return { data: null, error: e as ApiError }
}
}
/**
* Inside a browser context, `signOut()` will remove the logged in user from the browser session
* and log them out - removing all items from localstorage and then trigger a "SIGNED_OUT" event.
*
* For server-side management, you can disable sessions by passing a JWT through to `auth.api.signOut(JWT: string)`
*/
async signOut(): Promise<{ error: ApiError | null }> {
const accessToken = this.currentSession?.access_token
this._removeSession()
this._notifyAllSubscribers('SIGNED_OUT')
if (accessToken) {
const { error } = await this.api.signOut(accessToken)
if (error) return { error }
}
return { error: null }
}
/**
* Receive a notification every time an auth event happens.
* @returns {Subscription} A subscription object which can be used to unsubscribe itself.
*/
onAuthStateChange(callback: (event: AuthChangeEvent, session: Session | null) => void): {
data: Subscription | null
error: ApiError | null
} {
try {
const id: string = uuid()
const subscription: Subscription = {
id,
callback,
unsubscribe: () => {
this.stateChangeEmitters.delete(id)
},
}
this.stateChangeEmitters.set(id, subscription)
return { data: subscription, error: null }
} catch (e) {
return { data: null, error: e as ApiError }
}
}
private async _handleEmailSignIn(
email: string,
password: string,
options: {
redirectTo?: string
} = {}
) {
try {
const { data, error } = await this.api.signInWithEmail(email, password, {
redirectTo: options.redirectTo,
})
if (error || !data) return { data: null, user: null, session: null, error }
if (data?.user?.confirmed_at || data?.user?.email_confirmed_at) {
this._saveSession(data)
this._notifyAllSubscribers('SIGNED_IN')
}
return { data, user: data.user, session: data, error: null }
} catch (e) {
return { data: null, user: null, session: null, error: e as ApiError }
}
}
private async _handlePhoneSignIn(phone: string, password: string) {
try {
const { data, error } = await this.api.signInWithPhone(phone, password)
if (error || !data) return { data: null, user: null, session: null, error }
if (data?.user?.phone_confirmed_at) {
this._saveSession(data)
this._notifyAllSubscribers('SIGNED_IN')
}
return { data, user: data.user, session: data, error: null }
} catch (e) {
return { data: null, user: null, session: null, error: e as ApiError }
}
}
private _handleProviderSignIn(
provider: Provider,
options: {
redirectTo?: string
scopes?: string
} = {}
) {
const url: string = this.api.getUrlForProvider(provider, {
redirectTo: options.redirectTo,
scopes: options.scopes,
})
try {
// try to open on the browser
if (isBrowser()) {
window.location.href = url
}
return { provider, url, data: null, session: null, user: null, error: null }
} catch (e) {
// fallback to returning the URL
if (url) return { provider, url, data: null, session: null, user: null, error: null }
return { data: null, user: null, session: null, error: e as ApiError }
}
}
/**
* Attempts to get the session from LocalStorage
* Note: this should never be async (even for React Native), as we need it to return immediately in the constructor.
*/
private _recoverSession() {
try {
const json = isBrowser() && this.localStorage?.getItem(STORAGE_KEY)
if (!json || typeof json !== 'string') {
return null
}
const data = JSON.parse(json)
const { currentSession, expiresAt } = data
const timeNow = Math.round(Date.now() / 1000)
if (expiresAt >= timeNow && currentSession?.user) {
this._saveSession(currentSession)
this._notifyAllSubscribers('SIGNED_IN')
}
} catch (error) {
console.log('error', error)
}
}
/**
* Recovers the session from LocalStorage and refreshes
* Note: this method is async to accommodate for AsyncStorage e.g. in React native.
*/
private async _recoverAndRefresh() {
try {
const json = isBrowser() && (await this.localStorage.getItem(STORAGE_KEY))
if (!json) {
return null
}
const data = JSON.parse(json)
const { currentSession, expiresAt } = data
const timeNow = Math.round(Date.now() / 1000)
if (expiresAt < timeNow) {
if (this.autoRefreshToken && currentSession.refresh_token) {
const { error } = await this._callRefreshToken(currentSession.refresh_token)
if (error) {
console.log(error.message)
await this._removeSession()
}
} else {
this._removeSession()
}
} else if (!currentSession || !currentSession.user) {
console.log('Current session is missing data.')
this._removeSession()
} else {
// should be handled on _recoverSession method already
// But we still need the code here to accommodate for AsyncStorage e.g. in React native
this._saveSession(currentSession)
this._notifyAllSubscribers('SIGNED_IN')
}
} catch (err) {
console.error(err)
return null
}
}
private async _callRefreshToken(refresh_token = this.currentSession?.refresh_token) {
try {
if (!refresh_token) {
throw new Error('No current session.')
}
const { data, error } = await this.api.refreshAccessToken(refresh_token)
if (error) throw error
if (!data) throw Error('Invalid session data.')
this._saveSession(data)
this._notifyAllSubscribers('TOKEN_REFRESHED')
this._notifyAllSubscribers('SIGNED_IN')
return { data, error: null }
} catch (e) {
return { data: null, error: e as ApiError }
}
}
private _notifyAllSubscribers(event: AuthChangeEvent) {
this.stateChangeEmitters.forEach((x) => x.callback(event, this.currentSession))
}
/**
* set currentSession and currentUser
* process to _startAutoRefreshToken if possible
*/
private _saveSession(session: Session) {
this.currentSession = session
this.currentUser = session.user
const expiresAt = session.expires_at
if (expiresAt) {
const timeNow = Math.round(Date.now() / 1000)
const expiresIn = expiresAt - timeNow
const refreshDurationBeforeExpires = expiresIn > 60 ? 60 : 0.5
this._startAutoRefreshToken((expiresIn - refreshDurationBeforeExpires) * 1000)
}
// Do we need any extra check before persist session
// access_token or user ?
if (this.persistSession && session.expires_at) {
this._persistSession(this.currentSession)
}
}
private _persistSession(currentSession: Session) {
const data = { currentSession, expiresAt: currentSession.expires_at }
isBrowser() && this.localStorage.setItem(STORAGE_KEY, JSON.stringify(data))
}
private async _removeSession() {
this.currentSession = null
this.currentUser = null
if (this.refreshTokenTimer) clearTimeout(this.refreshTokenTimer)
isBrowser() && (await this.localStorage.removeItem(STORAGE_KEY))
}
/**
* Clear and re-create refresh token timer
* @param value time intervals in milliseconds
*/
private _startAutoRefreshToken(value: number) {
if (this.refreshTokenTimer) clearTimeout(this.refreshTokenTimer)
if (value <= 0 || !this.autoRefreshToken) return
this.refreshTokenTimer = setTimeout(() => this._callRefreshToken(), value)
if (typeof this.refreshTokenTimer.unref === 'function') this.refreshTokenTimer.unref()
}
/**
* Listens for changes to LocalStorage and updates the current session.
*/
private _listenForMultiTabEvents() {
if (!this.multiTab || !isBrowser() || !window?.addEventListener) {
// console.debug('Auth multi-tab support is disabled.')
return false
}
try {
window?.addEventListener('storage', (e: StorageEvent) => {
if (e.key === STORAGE_KEY) {
const newSession = JSON.parse(String(e.newValue))
if (newSession?.currentSession?.access_token) {
this._recoverAndRefresh()
this._notifyAllSubscribers('SIGNED_IN')
} else {
this._removeSession()
this._notifyAllSubscribers('SIGNED_OUT')
}
}
})
} catch (error) {
console.error('_listenForMultiTabEvents', error)
}
}
}