@supabase/gotrue-js 2.46.1 → 2.47.0
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.
- package/dist/main/GoTrueClient.d.ts +19 -1
- package/dist/main/GoTrueClient.d.ts.map +1 -1
- package/dist/main/GoTrueClient.js +179 -103
- package/dist/main/GoTrueClient.js.map +1 -1
- package/dist/main/lib/helpers.d.ts +0 -29
- package/dist/main/lib/helpers.d.ts.map +1 -1
- package/dist/main/lib/helpers.js +1 -117
- package/dist/main/lib/helpers.js.map +1 -1
- package/dist/main/lib/locks.d.ts.map +1 -1
- package/dist/main/lib/locks.js +18 -58
- package/dist/main/lib/locks.js.map +1 -1
- package/dist/main/lib/version.d.ts +1 -1
- package/dist/main/lib/version.js +1 -1
- package/dist/module/GoTrueClient.d.ts +19 -1
- package/dist/module/GoTrueClient.d.ts.map +1 -1
- package/dist/module/GoTrueClient.js +180 -104
- package/dist/module/GoTrueClient.js.map +1 -1
- package/dist/module/lib/helpers.d.ts +0 -29
- package/dist/module/lib/helpers.d.ts.map +1 -1
- package/dist/module/lib/helpers.js +0 -113
- package/dist/module/lib/helpers.js.map +1 -1
- package/dist/module/lib/locks.d.ts.map +1 -1
- package/dist/module/lib/locks.js +18 -58
- package/dist/module/lib/locks.js.map +1 -1
- package/dist/module/lib/version.d.ts +1 -1
- package/dist/module/lib/version.js +1 -1
- package/package.json +1 -1
- package/src/GoTrueClient.ts +235 -135
- package/src/lib/helpers.ts +0 -140
- package/src/lib/locks.ts +20 -73
- package/src/lib/version.ts +1 -1
|
@@ -46,6 +46,8 @@ export default class GoTrueClient {
|
|
|
46
46
|
};
|
|
47
47
|
protected fetch: Fetch;
|
|
48
48
|
protected lock: LockFunc;
|
|
49
|
+
protected lockAcquired: boolean;
|
|
50
|
+
protected pendingInLock: Promise<any>[];
|
|
49
51
|
/**
|
|
50
52
|
* Used to broadcast state change events to other tabs listening.
|
|
51
53
|
*/
|
|
@@ -98,6 +100,7 @@ export default class GoTrueClient {
|
|
|
98
100
|
* Log in an existing user by exchanging an Auth Code issued during the PKCE flow.
|
|
99
101
|
*/
|
|
100
102
|
exchangeCodeForSession(authCode: string): Promise<AuthTokenResponse>;
|
|
103
|
+
private _exchangeCodeForSession;
|
|
101
104
|
/**
|
|
102
105
|
* Allows signing in with an OIDC ID token. The authentication provider used
|
|
103
106
|
* should be enabled and configured.
|
|
@@ -145,6 +148,7 @@ export default class GoTrueClient {
|
|
|
145
148
|
* Requires the user to be signed-in.
|
|
146
149
|
*/
|
|
147
150
|
reauthenticate(): Promise<AuthResponse>;
|
|
151
|
+
private _reauthenticate;
|
|
148
152
|
/**
|
|
149
153
|
* Resends an existing signup confirmation email, email change email, SMS OTP or phone change OTP.
|
|
150
154
|
*/
|
|
@@ -191,12 +195,16 @@ export default class GoTrueClient {
|
|
|
191
195
|
* @param jwt Takes in an optional access token jwt. If no jwt is provided, getUser() will attempt to get the jwt from the current session.
|
|
192
196
|
*/
|
|
193
197
|
getUser(jwt?: string): Promise<UserResponse>;
|
|
198
|
+
private _getUser;
|
|
194
199
|
/**
|
|
195
200
|
* Updates user data for a logged in user.
|
|
196
201
|
*/
|
|
197
202
|
updateUser(attributes: UserAttributes, options?: {
|
|
198
203
|
emailRedirectTo?: string | undefined;
|
|
199
204
|
}): Promise<UserResponse>;
|
|
205
|
+
protected _updateUser(attributes: UserAttributes, options?: {
|
|
206
|
+
emailRedirectTo?: string | undefined;
|
|
207
|
+
}): Promise<UserResponse>;
|
|
200
208
|
/**
|
|
201
209
|
* Decodes a JWT (without performing any validation).
|
|
202
210
|
*/
|
|
@@ -210,6 +218,10 @@ export default class GoTrueClient {
|
|
|
210
218
|
access_token: string;
|
|
211
219
|
refresh_token: string;
|
|
212
220
|
}): Promise<AuthResponse>;
|
|
221
|
+
protected _setSession(currentSession: {
|
|
222
|
+
access_token: string;
|
|
223
|
+
refresh_token: string;
|
|
224
|
+
}): Promise<AuthResponse>;
|
|
213
225
|
/**
|
|
214
226
|
* Returns a new session, regardless of expiry status.
|
|
215
227
|
* Takes in an optional current session. If not passed in, then refreshSession() will attempt to retrieve it from getSession().
|
|
@@ -219,6 +231,9 @@ export default class GoTrueClient {
|
|
|
219
231
|
refreshSession(currentSession?: {
|
|
220
232
|
refresh_token: string;
|
|
221
233
|
}): Promise<AuthResponse>;
|
|
234
|
+
protected _refreshSession(currentSession?: {
|
|
235
|
+
refresh_token: string;
|
|
236
|
+
}): Promise<AuthResponse>;
|
|
222
237
|
/**
|
|
223
238
|
* Gets the session data from a URL string
|
|
224
239
|
*/
|
|
@@ -240,7 +255,10 @@ export default class GoTrueClient {
|
|
|
240
255
|
*
|
|
241
256
|
* If using others scope, no `SIGNED_OUT` event is fired!
|
|
242
257
|
*/
|
|
243
|
-
signOut(
|
|
258
|
+
signOut(options?: SignOut): Promise<{
|
|
259
|
+
error: AuthError | null;
|
|
260
|
+
}>;
|
|
261
|
+
protected _signOut({ scope }?: SignOut): Promise<{
|
|
244
262
|
error: AuthError | null;
|
|
245
263
|
}>;
|
|
246
264
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GoTrueClient.d.ts","sourceRoot":"","sources":["../../src/GoTrueClient.ts"],"names":[],"mappings":"AAAA,OAAO,cAAc,MAAM,kBAAkB,CAAA;AAE7C,OAAO,EACL,SAAS,EAWV,MAAM,cAAc,CAAA;AACrB,OAAO,EAAE,KAAK,EAA2D,MAAM,aAAa,CAAA;AAC5F,OAAO,EAEL,QAAQ,
|
|
1
|
+
{"version":3,"file":"GoTrueClient.d.ts","sourceRoot":"","sources":["../../src/GoTrueClient.ts"],"names":[],"mappings":"AAAA,OAAO,cAAc,MAAM,kBAAkB,CAAA;AAE7C,OAAO,EACL,SAAS,EAWV,MAAM,cAAc,CAAA;AACrB,OAAO,EAAE,KAAK,EAA2D,MAAM,aAAa,CAAA;AAC5F,OAAO,EAEL,QAAQ,EAaT,MAAM,eAAe,CAAA;AAKtB,OAAO,KAAK,EACV,eAA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U5B;;;;;OAKG;IACH,OAAO,CAAC,gCAAgC;IAexC;;;OAGG;YACW,iBAAiB;IAiC/B;;;OAGG;YACW,gBAAgB;IAW9B;;;;;;;;;;;;;;;;;;;;;OAqBG;IACG,gBAAgB;IAKtB;;;;;;;OAOG;IACG,eAAe;IAKrB;;OAEG;YACW,qBAAqB;IAoDnC;;;;OAIG;YACW,uBAAuB;IAyBrC;;OAEG;YACW,oBAAoB;IA+BlC;;;;;OAKG;YACW,kBAAkB;YA6ClB,SAAS;IAqBvB;;OAEG;YACW,OAAO;IAoCrB;;OAEG;YACW,OAAO;IAsCrB;;OAEG;YACW,UAAU;IA0BxB;;OAEG;YACW,mBAAmB;IAgBjC;;OAEG;YACW,YAAY;IAuB1B;;OAEG;YACW,+BAA+B;CAsC9C"}
|
|
@@ -48,6 +48,8 @@ class GoTrueClient {
|
|
|
48
48
|
*/
|
|
49
49
|
this.initializePromise = null;
|
|
50
50
|
this.detectSessionInUrl = true;
|
|
51
|
+
this.lockAcquired = false;
|
|
52
|
+
this.pendingInLock = [];
|
|
51
53
|
/**
|
|
52
54
|
* Used to broadcast state change events to other tabs listening.
|
|
53
55
|
*/
|
|
@@ -113,11 +115,16 @@ class GoTrueClient {
|
|
|
113
115
|
* This method is automatically called when instantiating the client, but should also be called
|
|
114
116
|
* manually when checking for an error from an auth redirect (oauth, magiclink, password recovery, etc).
|
|
115
117
|
*/
|
|
116
|
-
initialize() {
|
|
118
|
+
async initialize() {
|
|
117
119
|
if (this.initializePromise) {
|
|
118
|
-
return this.initializePromise;
|
|
120
|
+
return await this.initializePromise;
|
|
119
121
|
}
|
|
120
|
-
|
|
122
|
+
this.initializePromise = (async () => {
|
|
123
|
+
return await this._acquireLock(-1, async () => {
|
|
124
|
+
return await this._initialize();
|
|
125
|
+
});
|
|
126
|
+
})();
|
|
127
|
+
return await this.initializePromise;
|
|
121
128
|
}
|
|
122
129
|
/**
|
|
123
130
|
* IMPORTANT:
|
|
@@ -126,53 +133,47 @@ class GoTrueClient {
|
|
|
126
133
|
* the whole lifetime of the client
|
|
127
134
|
*/
|
|
128
135
|
async _initialize() {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
this._debug('#_initialize()', 'error detecting session from URL', error);
|
|
140
|
-
// failed login attempt via url,
|
|
141
|
-
// remove old session as in verifyOtp, signUp and signInWith*
|
|
142
|
-
await this._removeSession();
|
|
143
|
-
return { error };
|
|
144
|
-
}
|
|
145
|
-
const { session, redirectType } = data;
|
|
146
|
-
this._debug('#_initialize()', 'detected session in URL', session, 'redirect type', redirectType);
|
|
147
|
-
await this._saveSession(session);
|
|
148
|
-
setTimeout(async () => {
|
|
149
|
-
if (redirectType === 'recovery') {
|
|
150
|
-
await this._notifyAllSubscribers('PASSWORD_RECOVERY', session);
|
|
151
|
-
}
|
|
152
|
-
else {
|
|
153
|
-
await this._notifyAllSubscribers('SIGNED_IN', session);
|
|
154
|
-
}
|
|
155
|
-
}, 0);
|
|
156
|
-
return { error: null };
|
|
157
|
-
}
|
|
158
|
-
// no login attempt via callback url try to recover session from storage
|
|
159
|
-
await this._recoverAndRefresh();
|
|
160
|
-
return { error: null };
|
|
161
|
-
}
|
|
162
|
-
catch (error) {
|
|
163
|
-
if ((0, errors_1.isAuthError)(error)) {
|
|
136
|
+
try {
|
|
137
|
+
const isPKCEFlow = (0, helpers_1.isBrowser)() ? await this._isPKCEFlow() : false;
|
|
138
|
+
this._debug('#_initialize()', 'begin', 'is PKCE flow', isPKCEFlow);
|
|
139
|
+
if (isPKCEFlow || (this.detectSessionInUrl && this._isImplicitGrantFlow())) {
|
|
140
|
+
const { data, error } = await this._getSessionFromURL(isPKCEFlow);
|
|
141
|
+
if (error) {
|
|
142
|
+
this._debug('#_initialize()', 'error detecting session from URL', error);
|
|
143
|
+
// failed login attempt via url,
|
|
144
|
+
// remove old session as in verifyOtp, signUp and signInWith*
|
|
145
|
+
await this._removeSession();
|
|
164
146
|
return { error };
|
|
165
147
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
148
|
+
const { session, redirectType } = data;
|
|
149
|
+
this._debug('#_initialize()', 'detected session in URL', session, 'redirect type', redirectType);
|
|
150
|
+
await this._saveSession(session);
|
|
151
|
+
setTimeout(async () => {
|
|
152
|
+
if (redirectType === 'recovery') {
|
|
153
|
+
await this._notifyAllSubscribers('PASSWORD_RECOVERY', session);
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
await this._notifyAllSubscribers('SIGNED_IN', session);
|
|
157
|
+
}
|
|
158
|
+
}, 0);
|
|
159
|
+
return { error: null };
|
|
169
160
|
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
161
|
+
// no login attempt via callback url try to recover session from storage
|
|
162
|
+
await this._recoverAndRefresh();
|
|
163
|
+
return { error: null };
|
|
164
|
+
}
|
|
165
|
+
catch (error) {
|
|
166
|
+
if ((0, errors_1.isAuthError)(error)) {
|
|
167
|
+
return { error };
|
|
173
168
|
}
|
|
174
|
-
|
|
175
|
-
|
|
169
|
+
return {
|
|
170
|
+
error: new errors_1.AuthUnknownError('Unexpected error during initialization', error),
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
finally {
|
|
174
|
+
await this._handleVisibilityChange();
|
|
175
|
+
this._debug('#_initialize()', 'end');
|
|
176
|
+
}
|
|
176
177
|
}
|
|
177
178
|
/**
|
|
178
179
|
* Creates a new user.
|
|
@@ -326,6 +327,12 @@ class GoTrueClient {
|
|
|
326
327
|
* Log in an existing user by exchanging an Auth Code issued during the PKCE flow.
|
|
327
328
|
*/
|
|
328
329
|
async exchangeCodeForSession(authCode) {
|
|
330
|
+
await this.initializePromise;
|
|
331
|
+
return this._acquireLock(-1, async () => {
|
|
332
|
+
return this._exchangeCodeForSession(authCode);
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
async _exchangeCodeForSession(authCode) {
|
|
329
336
|
const codeVerifier = await (0, helpers_1.getItemAsync)(this.storage, `${this.storageKey}-code-verifier`);
|
|
330
337
|
const { data, error } = await (0, fetch_1._request)(this.fetch, 'POST', `${this.url}/token?grant_type=pkce`, {
|
|
331
338
|
headers: this.headers,
|
|
@@ -539,6 +546,12 @@ class GoTrueClient {
|
|
|
539
546
|
* Requires the user to be signed-in.
|
|
540
547
|
*/
|
|
541
548
|
async reauthenticate() {
|
|
549
|
+
await this.initializePromise;
|
|
550
|
+
return await this._acquireLock(-1, async () => {
|
|
551
|
+
return await this._reauthenticate();
|
|
552
|
+
});
|
|
553
|
+
}
|
|
554
|
+
async _reauthenticate() {
|
|
542
555
|
try {
|
|
543
556
|
return await this._useSession(async (result) => {
|
|
544
557
|
const { data: { session }, error: sessionError, } = result;
|
|
@@ -608,8 +621,11 @@ class GoTrueClient {
|
|
|
608
621
|
* The session returned can be null if the session is not detected which can happen in the event a user is not signed-in or has logged out.
|
|
609
622
|
*/
|
|
610
623
|
async getSession() {
|
|
611
|
-
|
|
612
|
-
|
|
624
|
+
await this.initializePromise;
|
|
625
|
+
return this._acquireLock(-1, async () => {
|
|
626
|
+
return this._useSession(async (result) => {
|
|
627
|
+
return result;
|
|
628
|
+
});
|
|
613
629
|
});
|
|
614
630
|
}
|
|
615
631
|
/**
|
|
@@ -618,23 +634,49 @@ class GoTrueClient {
|
|
|
618
634
|
async _acquireLock(acquireTimeout, fn) {
|
|
619
635
|
this._debug('#_acquireLock', 'begin', acquireTimeout);
|
|
620
636
|
try {
|
|
621
|
-
if (
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
637
|
+
if (this.lockAcquired) {
|
|
638
|
+
const last = this.pendingInLock.length
|
|
639
|
+
? this.pendingInLock[this.pendingInLock.length - 1]
|
|
640
|
+
: Promise.resolve();
|
|
641
|
+
const result = (async () => {
|
|
642
|
+
await last;
|
|
643
|
+
return await fn();
|
|
644
|
+
})();
|
|
645
|
+
this.pendingInLock.push((async () => {
|
|
646
|
+
try {
|
|
647
|
+
await result;
|
|
648
|
+
}
|
|
649
|
+
catch (e) {
|
|
650
|
+
// we jsut care if it finished
|
|
651
|
+
}
|
|
652
|
+
})());
|
|
653
|
+
return result;
|
|
628
654
|
}
|
|
629
655
|
return await this.lock(`lock:${this.storageKey}`, acquireTimeout, async () => {
|
|
630
656
|
this._debug('#_acquireLock', 'lock acquired for storage key', this.storageKey);
|
|
631
657
|
try {
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
658
|
+
this.lockAcquired = true;
|
|
659
|
+
const result = fn();
|
|
660
|
+
this.pendingInLock.push((async () => {
|
|
661
|
+
try {
|
|
662
|
+
await result;
|
|
663
|
+
}
|
|
664
|
+
catch (e) {
|
|
665
|
+
// we just care if it finished
|
|
666
|
+
}
|
|
667
|
+
})());
|
|
668
|
+
await result;
|
|
669
|
+
// keep draining the queue until there's nothing to wait on
|
|
670
|
+
while (this.pendingInLock.length) {
|
|
671
|
+
const waitOn = [...this.pendingInLock];
|
|
672
|
+
await Promise.all(waitOn);
|
|
673
|
+
this.pendingInLock.splice(0, waitOn.length);
|
|
674
|
+
}
|
|
675
|
+
return await result;
|
|
635
676
|
}
|
|
636
677
|
finally {
|
|
637
678
|
this._debug('#_acquireLock', 'lock released for storage key', this.storageKey);
|
|
679
|
+
this.lockAcquired = false;
|
|
638
680
|
}
|
|
639
681
|
});
|
|
640
682
|
}
|
|
@@ -651,19 +693,9 @@ class GoTrueClient {
|
|
|
651
693
|
async _useSession(fn) {
|
|
652
694
|
this._debug('#_useSession', 'begin');
|
|
653
695
|
try {
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
const result = await this.__loadSession();
|
|
658
|
-
return await fn(result);
|
|
659
|
-
}
|
|
660
|
-
return await this._acquireLock(-1, async () => {
|
|
661
|
-
return await (0, helpers_1.stackGuard)('_useSession', async () => {
|
|
662
|
-
// the use of __loadSession here is the only correct use of the function!
|
|
663
|
-
const result = await this.__loadSession();
|
|
664
|
-
return await fn(result);
|
|
665
|
-
});
|
|
666
|
-
});
|
|
696
|
+
// the use of __loadSession here is the only correct use of the function!
|
|
697
|
+
const result = await this.__loadSession();
|
|
698
|
+
return await fn(result);
|
|
667
699
|
}
|
|
668
700
|
finally {
|
|
669
701
|
this._debug('#_useSession', 'end');
|
|
@@ -676,15 +708,9 @@ class GoTrueClient {
|
|
|
676
708
|
*/
|
|
677
709
|
async __loadSession() {
|
|
678
710
|
this._debug('#__loadSession()', 'begin');
|
|
679
|
-
if (this.
|
|
680
|
-
|
|
711
|
+
if (!this.lockAcquired) {
|
|
712
|
+
this._debug('#__loadSession()', 'used outside of an acquired lock!', new Error().stack);
|
|
681
713
|
}
|
|
682
|
-
if ((0, helpers_1.isInStackGuard)('_initialize')) {
|
|
683
|
-
this._debug('#__loadSession', '#_initialize recursion detected', new Error().stack);
|
|
684
|
-
}
|
|
685
|
-
// always wait for #_initialize() to finish before loading anything from
|
|
686
|
-
// storage
|
|
687
|
-
await this.initializePromise;
|
|
688
714
|
try {
|
|
689
715
|
let currentSession = null;
|
|
690
716
|
if (this.persistSession) {
|
|
@@ -729,6 +755,15 @@ class GoTrueClient {
|
|
|
729
755
|
* @param jwt Takes in an optional access token jwt. If no jwt is provided, getUser() will attempt to get the jwt from the current session.
|
|
730
756
|
*/
|
|
731
757
|
async getUser(jwt) {
|
|
758
|
+
if (jwt) {
|
|
759
|
+
return await this._getUser(jwt);
|
|
760
|
+
}
|
|
761
|
+
await this.initializePromise;
|
|
762
|
+
return this._acquireLock(-1, async () => {
|
|
763
|
+
return await this._getUser();
|
|
764
|
+
});
|
|
765
|
+
}
|
|
766
|
+
async _getUser(jwt) {
|
|
732
767
|
try {
|
|
733
768
|
if (jwt) {
|
|
734
769
|
return await (0, fetch_1._request)(this.fetch, 'GET', `${this.url}/user`, {
|
|
@@ -761,6 +796,12 @@ class GoTrueClient {
|
|
|
761
796
|
* Updates user data for a logged in user.
|
|
762
797
|
*/
|
|
763
798
|
async updateUser(attributes, options = {}) {
|
|
799
|
+
await this.initializePromise;
|
|
800
|
+
return await this._acquireLock(-1, async () => {
|
|
801
|
+
return await this._updateUser(attributes, options);
|
|
802
|
+
});
|
|
803
|
+
}
|
|
804
|
+
async _updateUser(attributes, options = {}) {
|
|
764
805
|
try {
|
|
765
806
|
return await this._useSession(async (result) => {
|
|
766
807
|
const { data: sessionData, error: sessionError } = result;
|
|
@@ -805,6 +846,12 @@ class GoTrueClient {
|
|
|
805
846
|
* @param currentSession The current session that minimally contains an access token and refresh token.
|
|
806
847
|
*/
|
|
807
848
|
async setSession(currentSession) {
|
|
849
|
+
await this.initializePromise;
|
|
850
|
+
return await this._acquireLock(-1, async () => {
|
|
851
|
+
return await this._setSession(currentSession);
|
|
852
|
+
});
|
|
853
|
+
}
|
|
854
|
+
async _setSession(currentSession) {
|
|
808
855
|
try {
|
|
809
856
|
if (!currentSession.access_token || !currentSession.refresh_token) {
|
|
810
857
|
throw new errors_1.AuthSessionMissingError();
|
|
@@ -829,7 +876,7 @@ class GoTrueClient {
|
|
|
829
876
|
session = refreshedSession;
|
|
830
877
|
}
|
|
831
878
|
else {
|
|
832
|
-
const { data, error } = await this.
|
|
879
|
+
const { data, error } = await this._getUser(currentSession.access_token);
|
|
833
880
|
if (error) {
|
|
834
881
|
throw error;
|
|
835
882
|
}
|
|
@@ -860,6 +907,12 @@ class GoTrueClient {
|
|
|
860
907
|
* @param currentSession The current session. If passed in, it must contain a refresh token.
|
|
861
908
|
*/
|
|
862
909
|
async refreshSession(currentSession) {
|
|
910
|
+
await this.initializePromise;
|
|
911
|
+
return await this._acquireLock(-1, async () => {
|
|
912
|
+
return await this._refreshSession(currentSession);
|
|
913
|
+
});
|
|
914
|
+
}
|
|
915
|
+
async _refreshSession(currentSession) {
|
|
863
916
|
try {
|
|
864
917
|
return await this._useSession(async (result) => {
|
|
865
918
|
var _a;
|
|
@@ -907,7 +960,7 @@ class GoTrueClient {
|
|
|
907
960
|
if (isPKCEFlow) {
|
|
908
961
|
if (!params.code)
|
|
909
962
|
throw new errors_1.AuthPKCEGrantCodeExchangeError('No code detected.');
|
|
910
|
-
const { data, error } = await this.
|
|
963
|
+
const { data, error } = await this._exchangeCodeForSession(params.code);
|
|
911
964
|
if (error)
|
|
912
965
|
throw error;
|
|
913
966
|
const url = new URL(window.location.href);
|
|
@@ -928,7 +981,7 @@ class GoTrueClient {
|
|
|
928
981
|
const timeNow = Math.round(Date.now() / 1000);
|
|
929
982
|
const expiresIn = parseInt(expires_in);
|
|
930
983
|
const expires_at = timeNow + expiresIn;
|
|
931
|
-
const { data, error } = await this.
|
|
984
|
+
const { data, error } = await this._getUser(access_token);
|
|
932
985
|
if (error)
|
|
933
986
|
throw error;
|
|
934
987
|
const session = {
|
|
@@ -977,7 +1030,13 @@ class GoTrueClient {
|
|
|
977
1030
|
*
|
|
978
1031
|
* If using others scope, no `SIGNED_OUT` event is fired!
|
|
979
1032
|
*/
|
|
980
|
-
async signOut(
|
|
1033
|
+
async signOut(options = { scope: 'global' }) {
|
|
1034
|
+
await this.initializePromise;
|
|
1035
|
+
return await this._acquireLock(-1, async () => {
|
|
1036
|
+
return await this._signOut(options);
|
|
1037
|
+
});
|
|
1038
|
+
}
|
|
1039
|
+
async _signOut({ scope } = { scope: 'global' }) {
|
|
981
1040
|
return await this._useSession(async (result) => {
|
|
982
1041
|
var _a;
|
|
983
1042
|
const { data, error: sessionError } = result;
|
|
@@ -1019,7 +1078,12 @@ class GoTrueClient {
|
|
|
1019
1078
|
};
|
|
1020
1079
|
this._debug('#onAuthStateChange()', 'registered callback with id', id);
|
|
1021
1080
|
this.stateChangeEmitters.set(id, subscription);
|
|
1022
|
-
|
|
1081
|
+
(async () => {
|
|
1082
|
+
await this.initializePromise;
|
|
1083
|
+
await this._acquireLock(-1, async () => {
|
|
1084
|
+
this._emitInitialSession(id);
|
|
1085
|
+
});
|
|
1086
|
+
})();
|
|
1023
1087
|
return { data: { subscription } };
|
|
1024
1088
|
}
|
|
1025
1089
|
async _emitInitialSession(id) {
|
|
@@ -1380,29 +1444,41 @@ class GoTrueClient {
|
|
|
1380
1444
|
async _autoRefreshTokenTick() {
|
|
1381
1445
|
this._debug('#_autoRefreshTokenTick()', 'begin');
|
|
1382
1446
|
try {
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1447
|
+
await this._acquireLock(0, async () => {
|
|
1448
|
+
try {
|
|
1449
|
+
const now = Date.now();
|
|
1450
|
+
try {
|
|
1451
|
+
return await this._useSession(async (result) => {
|
|
1452
|
+
const { data: { session }, } = result;
|
|
1453
|
+
if (!session || !session.refresh_token || !session.expires_at) {
|
|
1454
|
+
this._debug('#_autoRefreshTokenTick()', 'no session');
|
|
1455
|
+
return;
|
|
1456
|
+
}
|
|
1457
|
+
// session will expire in this many ticks (or has already expired if <= 0)
|
|
1458
|
+
const expiresInTicks = Math.floor((session.expires_at * 1000 - now) / AUTO_REFRESH_TICK_DURATION);
|
|
1459
|
+
this._debug('#_autoRefreshTokenTick()', `access token expires in ${expiresInTicks} ticks, a tick lasts ${AUTO_REFRESH_TICK_DURATION}ms, refresh threshold is ${AUTO_REFRESH_TICK_THRESHOLD} ticks`);
|
|
1460
|
+
if (expiresInTicks <= AUTO_REFRESH_TICK_THRESHOLD) {
|
|
1461
|
+
await this._callRefreshToken(session.refresh_token);
|
|
1462
|
+
}
|
|
1463
|
+
});
|
|
1390
1464
|
}
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
this._debug('#_autoRefreshTokenTick()', `access token expires in ${expiresInTicks} ticks, a tick lasts ${AUTO_REFRESH_TICK_DURATION}ms, refresh threshold is ${AUTO_REFRESH_TICK_THRESHOLD} ticks`);
|
|
1394
|
-
if (expiresInTicks <= AUTO_REFRESH_TICK_THRESHOLD) {
|
|
1395
|
-
await this._callRefreshToken(session.refresh_token);
|
|
1465
|
+
catch (e) {
|
|
1466
|
+
console.error('Auto refresh tick failed with error. This is likely a transient error.', e);
|
|
1396
1467
|
}
|
|
1397
|
-
}
|
|
1468
|
+
}
|
|
1469
|
+
finally {
|
|
1470
|
+
this._debug('#_autoRefreshTokenTick()', 'end');
|
|
1471
|
+
}
|
|
1472
|
+
});
|
|
1473
|
+
}
|
|
1474
|
+
catch (e) {
|
|
1475
|
+
if (e.isAcquireTimeout) {
|
|
1476
|
+
this._debug('auto refresh token tick lock not available');
|
|
1398
1477
|
}
|
|
1399
|
-
|
|
1400
|
-
|
|
1478
|
+
else {
|
|
1479
|
+
throw e;
|
|
1401
1480
|
}
|
|
1402
1481
|
}
|
|
1403
|
-
finally {
|
|
1404
|
-
this._debug('#_autoRefreshTokenTick()', 'end');
|
|
1405
|
-
}
|
|
1406
1482
|
}
|
|
1407
1483
|
/**
|
|
1408
1484
|
* Registers callbacks on the browser / platform, which in-turn run
|
|
@@ -1621,7 +1697,7 @@ class GoTrueClient {
|
|
|
1621
1697
|
* {@see GoTrueMFAApi#listFactors}
|
|
1622
1698
|
*/
|
|
1623
1699
|
async _listFactors() {
|
|
1624
|
-
const { data: { user }, error: userError, } = await this.
|
|
1700
|
+
const { data: { user }, error: userError, } = await this._getUser();
|
|
1625
1701
|
if (userError) {
|
|
1626
1702
|
return { data: null, error: userError };
|
|
1627
1703
|
}
|