@supabase/gotrue-js 1.22.21 → 1.23.0-next.10

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 (76) hide show
  1. package/dist/main/GoTrueApi.d.ts +120 -60
  2. package/dist/main/GoTrueApi.d.ts.map +1 -1
  3. package/dist/main/GoTrueApi.js +153 -93
  4. package/dist/main/GoTrueApi.js.map +1 -1
  5. package/dist/main/GoTrueClient.d.ts +80 -73
  6. package/dist/main/GoTrueClient.d.ts.map +1 -1
  7. package/dist/main/GoTrueClient.js +263 -245
  8. package/dist/main/GoTrueClient.js.map +1 -1
  9. package/dist/main/index.js +5 -1
  10. package/dist/main/index.js.map +1 -1
  11. package/dist/main/lib/constants.d.ts +1 -1
  12. package/dist/main/lib/constants.d.ts.map +1 -1
  13. package/dist/main/lib/constants.js +2 -2
  14. package/dist/main/lib/constants.js.map +1 -1
  15. package/dist/main/lib/cookies.d.ts.map +1 -1
  16. package/dist/main/lib/cookies.js +2 -1
  17. package/dist/main/lib/cookies.js.map +1 -1
  18. package/dist/main/lib/errors.d.ts +41 -0
  19. package/dist/main/lib/errors.d.ts.map +1 -0
  20. package/dist/main/lib/errors.js +78 -0
  21. package/dist/main/lib/errors.js.map +1 -0
  22. package/dist/main/lib/fetch.d.ts +8 -5
  23. package/dist/main/lib/fetch.d.ts.map +1 -1
  24. package/dist/main/lib/fetch.js +25 -27
  25. package/dist/main/lib/fetch.js.map +1 -1
  26. package/dist/main/lib/helpers.d.ts +19 -2
  27. package/dist/main/lib/helpers.d.ts.map +1 -1
  28. package/dist/main/lib/helpers.js +32 -15
  29. package/dist/main/lib/helpers.js.map +1 -1
  30. package/dist/main/lib/types.d.ts +56 -6
  31. package/dist/main/lib/types.d.ts.map +1 -1
  32. package/dist/main/lib/version.d.ts +1 -1
  33. package/dist/main/lib/version.js +2 -2
  34. package/dist/main/lib/version.js.map +1 -1
  35. package/dist/module/GoTrueApi.d.ts +120 -60
  36. package/dist/module/GoTrueApi.d.ts.map +1 -1
  37. package/dist/module/GoTrueApi.js +153 -93
  38. package/dist/module/GoTrueApi.js.map +1 -1
  39. package/dist/module/GoTrueClient.d.ts +80 -73
  40. package/dist/module/GoTrueClient.d.ts.map +1 -1
  41. package/dist/module/GoTrueClient.js +265 -247
  42. package/dist/module/GoTrueClient.js.map +1 -1
  43. package/dist/module/lib/constants.d.ts +1 -1
  44. package/dist/module/lib/constants.d.ts.map +1 -1
  45. package/dist/module/lib/constants.js +1 -1
  46. package/dist/module/lib/constants.js.map +1 -1
  47. package/dist/module/lib/cookies.d.ts.map +1 -1
  48. package/dist/module/lib/cookies.js +2 -1
  49. package/dist/module/lib/cookies.js.map +1 -1
  50. package/dist/module/lib/errors.d.ts +41 -0
  51. package/dist/module/lib/errors.d.ts.map +1 -0
  52. package/dist/module/lib/errors.js +66 -0
  53. package/dist/module/lib/errors.js.map +1 -0
  54. package/dist/module/lib/fetch.d.ts +8 -5
  55. package/dist/module/lib/fetch.d.ts.map +1 -1
  56. package/dist/module/lib/fetch.js +25 -27
  57. package/dist/module/lib/fetch.js.map +1 -1
  58. package/dist/module/lib/helpers.d.ts +19 -2
  59. package/dist/module/lib/helpers.d.ts.map +1 -1
  60. package/dist/module/lib/helpers.js +24 -12
  61. package/dist/module/lib/helpers.js.map +1 -1
  62. package/dist/module/lib/types.d.ts +56 -6
  63. package/dist/module/lib/types.d.ts.map +1 -1
  64. package/dist/module/lib/version.d.ts +1 -1
  65. package/dist/module/lib/version.js +2 -2
  66. package/dist/module/lib/version.js.map +1 -1
  67. package/package.json +21 -19
  68. package/src/GoTrueApi.ts +347 -132
  69. package/src/GoTrueClient.ts +375 -302
  70. package/src/lib/constants.ts +1 -1
  71. package/src/lib/cookies.ts +6 -1
  72. package/src/lib/errors.ts +82 -0
  73. package/src/lib/fetch.ts +43 -28
  74. package/src/lib/helpers.ts +33 -12
  75. package/src/lib/types.ts +64 -6
  76. package/src/lib/version.ts +2 -2
@@ -16,9 +16,11 @@ const GoTrueApi_1 = __importDefault(require("./GoTrueApi"));
16
16
  const helpers_1 = require("./lib/helpers");
17
17
  const constants_1 = require("./lib/constants");
18
18
  const polyfills_1 = require("./lib/polyfills");
19
+ const errors_1 = require("./lib/errors");
19
20
  (0, polyfills_1.polyfillGlobalThis)(); // Make "globalThis" available
20
21
  const DEFAULT_OPTIONS = {
21
22
  url: constants_1.GOTRUE_URL,
23
+ storageKey: constants_1.STORAGE_KEY,
22
24
  autoRefreshToken: true,
23
25
  persistSession: true,
24
26
  detectSessionInUrl: true,
@@ -30,9 +32,10 @@ class GoTrueClient {
30
32
  * Create a new client for use in the browser.
31
33
  * @param options.url The URL of the GoTrue server.
32
34
  * @param options.headers Any additional headers to send to the GoTrue server.
35
+ * @param options.storageKey Optional key name used for storing tokens in local storage
33
36
  * @param options.detectSessionInUrl Set to "true" if you want to automatically detects OAuth grants in the URL and signs in the user.
34
37
  * @param options.autoRefreshToken Set to "true" if you want to automatically refresh the token before expiring.
35
- * @param options.persistSession Set to "true" if you want to automatically save the user session into local storage.
38
+ * @param options.persistSession Set to "true" if you want to automatically save the user session into local storage. If set to false, session will just be saved in memory.
36
39
  * @param options.localStorage Provide your own local storage implementation to use instead of the browser's local storage.
37
40
  * @param options.multiTab Set to "false" if you want to disable multi-tab/window events.
38
41
  * @param options.cookieOptions
@@ -40,13 +43,14 @@ class GoTrueClient {
40
43
  */
41
44
  constructor(options) {
42
45
  this.stateChangeEmitters = new Map();
46
+ // eslint-disable-next-line @typescript-eslint/no-inferrable-types
43
47
  this.networkRetries = 0;
48
+ this.refreshingDeferred = null;
44
49
  const settings = Object.assign(Object.assign({}, DEFAULT_OPTIONS), options);
45
- this.currentUser = null;
46
- this.currentSession = null;
50
+ this.inMemorySession = null;
51
+ this.storageKey = settings.storageKey;
47
52
  this.autoRefreshToken = settings.autoRefreshToken;
48
53
  this.persistSession = settings.persistSession;
49
- this.multiTab = settings.multiTab;
50
54
  this.localStorage = settings.localStorage || globalThis.localStorage;
51
55
  this.api = new GoTrueApi_1.default({
52
56
  url: settings.url,
@@ -54,15 +58,13 @@ class GoTrueClient {
54
58
  cookieOptions: settings.cookieOptions,
55
59
  fetch: settings.fetch,
56
60
  });
57
- this._recoverSession();
58
61
  this._recoverAndRefresh();
59
- this._listenForMultiTabEvents();
60
62
  this._handleVisibilityChange();
61
63
  if (settings.detectSessionInUrl && (0, helpers_1.isBrowser)() && !!(0, helpers_1.getParameterByName)('access_token')) {
62
64
  // Handle the OAuth redirect
63
65
  this.getSessionFromUrl({ storeSession: true }).then(({ error }) => {
64
66
  if (error) {
65
- throw new Error('Error getting session from URL.');
67
+ throw new errors_1.AuthUnknownError('Error getting session from URL.', error);
66
68
  }
67
69
  });
68
70
  }
@@ -102,83 +104,109 @@ class GoTrueClient {
102
104
  session = data;
103
105
  user = session.user;
104
106
  this._saveSession(session);
105
- this._notifyAllSubscribers('SIGNED_IN');
107
+ this._notifyAllSubscribers('SIGNED_IN', session);
106
108
  }
107
- if (data.id) {
109
+ else {
108
110
  user = data;
109
111
  }
110
112
  return { user, session, error: null };
111
113
  }
112
- catch (e) {
113
- return { user: null, session: null, error: e };
114
+ catch (error) {
115
+ if ((0, errors_1.isAuthError)(error)) {
116
+ return { user: null, session: null, error };
117
+ }
118
+ throw error;
114
119
  }
115
120
  });
116
121
  }
117
122
  /**
118
123
  * Log in an existing user, or login via a third-party provider.
119
- * @type UserCredentials
124
+ * @type SignInWithPasswordCredentials
120
125
  * @param email The user's email address.
121
126
  * @param phone The user's phone number.
122
127
  * @param password The user's password.
123
- * @param refreshToken A valid refresh token that was returned on login.
128
+ * @param options Valid options for password sign-ins.
129
+ */
130
+ signInWithPassword(credentials) {
131
+ return __awaiter(this, void 0, void 0, function* () {
132
+ try {
133
+ this._removeSession();
134
+ if ('email' in credentials) {
135
+ const { email, password, options } = credentials;
136
+ return this._handleEmailSignIn(email, password, {
137
+ captchaToken: options === null || options === void 0 ? void 0 : options.captchaToken,
138
+ });
139
+ }
140
+ if ('phone' in credentials) {
141
+ const { phone, password, options } = credentials;
142
+ return this._handlePhoneSignIn(phone, password, {
143
+ captchaToken: options === null || options === void 0 ? void 0 : options.captchaToken,
144
+ });
145
+ }
146
+ throw new errors_1.AuthInvalidCredentialsError('You must provide either an email or phone number and a password.');
147
+ }
148
+ catch (error) {
149
+ if ((0, errors_1.isAuthError)(error)) {
150
+ return { user: null, session: null, error };
151
+ }
152
+ throw error;
153
+ }
154
+ });
155
+ }
156
+ /**
157
+ * Log in an existing user via a third-party provider.
158
+ * @type SignInWithOAuthCredentials
124
159
  * @param provider One of the providers supported by GoTrue.
125
160
  * @param redirectTo A URL to send the user to after they are confirmed (OAuth logins only).
126
- * @param shouldCreateUser A boolean flag to indicate whether to automatically create a user on magiclink / otp sign-ins if the user doesn't exist. Defaults to true.
127
161
  * @param scopes A space-separated list of scopes granted to the OAuth application.
162
+ * @param queryParams An object of query params
128
163
  */
129
- signIn({ email, phone, password, refreshToken, provider, oidc }, options = {}) {
164
+ signInWithOAuth(credentials) {
165
+ var _a, _b, _c;
166
+ return __awaiter(this, void 0, void 0, function* () {
167
+ this._removeSession();
168
+ return this._handleProviderSignIn(credentials.provider, {
169
+ redirectTo: (_a = credentials.options) === null || _a === void 0 ? void 0 : _a.redirectTo,
170
+ scopes: (_b = credentials.options) === null || _b === void 0 ? void 0 : _b.scopes,
171
+ queryParams: (_c = credentials.options) === null || _c === void 0 ? void 0 : _c.queryParams,
172
+ });
173
+ });
174
+ }
175
+ /**
176
+ * Passwordless method for logging in an existing user.
177
+ * @type SignInWithPasswordlessCredentials
178
+ * @param email The user's email address.
179
+ * @param phone The user's phone number.
180
+ * @param options Valid options for passwordless sign-ins.
181
+ */
182
+ signInWithOtp(credentials) {
130
183
  return __awaiter(this, void 0, void 0, function* () {
131
184
  try {
132
185
  this._removeSession();
133
- if (email && !password) {
186
+ if ('email' in credentials) {
187
+ const { email, options } = credentials;
134
188
  const { error } = yield this.api.sendMagicLinkEmail(email, {
135
- redirectTo: options.redirectTo,
136
- shouldCreateUser: options.shouldCreateUser,
137
- captchaToken: options.captchaToken,
189
+ redirectTo: options === null || options === void 0 ? void 0 : options.emailRedirectTo,
190
+ shouldCreateUser: options === null || options === void 0 ? void 0 : options.shouldCreateUser,
191
+ captchaToken: options === null || options === void 0 ? void 0 : options.captchaToken,
138
192
  });
139
193
  return { user: null, session: null, error };
140
194
  }
141
- if (email && password) {
142
- return this._handleEmailSignIn(email, password, {
143
- redirectTo: options.redirectTo,
144
- captchaToken: options.captchaToken,
145
- });
146
- }
147
- if (phone && !password) {
195
+ if ('phone' in credentials) {
196
+ const { phone, options } = credentials;
148
197
  const { error } = yield this.api.sendMobileOTP(phone, {
149
- shouldCreateUser: options.shouldCreateUser,
150
- captchaToken: options.captchaToken,
198
+ shouldCreateUser: options === null || options === void 0 ? void 0 : options.shouldCreateUser,
199
+ captchaToken: options === null || options === void 0 ? void 0 : options.captchaToken,
151
200
  });
152
201
  return { user: null, session: null, error };
153
202
  }
154
- if (phone && password) {
155
- return this._handlePhoneSignIn(phone, password);
156
- }
157
- if (refreshToken) {
158
- // currentSession and currentUser will be updated to latest on _callRefreshToken using the passed refreshToken
159
- const { error } = yield this._callRefreshToken(refreshToken);
160
- if (error)
161
- throw error;
162
- return {
163
- user: this.currentUser,
164
- session: this.currentSession,
165
- error: null,
166
- };
167
- }
168
- if (provider) {
169
- return this._handleProviderSignIn(provider, {
170
- redirectTo: options.redirectTo,
171
- scopes: options.scopes,
172
- queryParams: options.queryParams,
173
- });
174
- }
175
- if (oidc) {
176
- return this._handleOpenIDConnectSignIn(oidc);
177
- }
178
- throw new Error(`You must provide either an email, phone number, a third-party provider or OpenID Connect.`);
203
+ throw new errors_1.AuthInvalidCredentialsError('You must provide either an email or phone number.');
179
204
  }
180
- catch (e) {
181
- return { user: null, session: null, error: e };
205
+ catch (error) {
206
+ if ((0, errors_1.isAuthError)(error)) {
207
+ return { user: null, session: null, error };
208
+ }
209
+ throw error;
182
210
  }
183
211
  });
184
212
  }
@@ -207,73 +235,92 @@ class GoTrueClient {
207
235
  session = data;
208
236
  user = session.user;
209
237
  this._saveSession(session);
210
- this._notifyAllSubscribers('SIGNED_IN');
238
+ this._notifyAllSubscribers('SIGNED_IN', session);
211
239
  }
212
240
  if (data.id) {
213
241
  user = data;
214
242
  }
215
243
  return { user, session, error: null };
216
244
  }
217
- catch (e) {
218
- return { user: null, session: null, error: e };
245
+ catch (error) {
246
+ if ((0, errors_1.isAuthError)(error)) {
247
+ return { user: null, session: null, error };
248
+ }
249
+ throw error;
219
250
  }
220
251
  });
221
252
  }
222
253
  /**
223
- * Inside a browser context, `user()` will return the user data, if there is a logged in user.
224
- *
225
- * For server-side management, you can get a user through `auth.api.getUserByCookie()`
226
- */
227
- user() {
228
- return this.currentUser;
229
- }
230
- /**
231
- * Returns the session data, if there is an active session.
254
+ * Returns the session data, refreshing it if necessary.
232
255
  */
233
- session() {
234
- return this.currentSession;
256
+ getSession() {
257
+ var _a;
258
+ return __awaiter(this, void 0, void 0, function* () {
259
+ let currentSession = null;
260
+ if (this.persistSession) {
261
+ const persistedSession = yield (0, helpers_1.getItemAsync)(this.localStorage, this.storageKey);
262
+ currentSession = (_a = persistedSession === null || persistedSession === void 0 ? void 0 : persistedSession.currentSession) !== null && _a !== void 0 ? _a : null;
263
+ }
264
+ else {
265
+ currentSession = this.inMemorySession;
266
+ }
267
+ if (!currentSession) {
268
+ return { session: null, error: null };
269
+ }
270
+ const hasExpired = currentSession.expires_at
271
+ ? currentSession.expires_at <= Date.now() / 1000
272
+ : false;
273
+ if (!hasExpired) {
274
+ return { session: currentSession, error: null };
275
+ }
276
+ const { session, error } = yield this._callRefreshToken(currentSession.refresh_token);
277
+ if (error) {
278
+ return { session: null, error };
279
+ }
280
+ return { session, error: null };
281
+ });
235
282
  }
236
283
  /**
237
- * Force refreshes the session including the user data in case it was updated in a different session.
284
+ * Returns the user data, refreshing the session if necessary.
238
285
  */
239
- refreshSession() {
240
- var _a;
286
+ getUser() {
241
287
  return __awaiter(this, void 0, void 0, function* () {
242
- try {
243
- if (!((_a = this.currentSession) === null || _a === void 0 ? void 0 : _a.access_token))
244
- throw new Error('Not logged in.');
245
- // currentSession and currentUser will be updated to latest on _callRefreshToken
246
- const { error } = yield this._callRefreshToken();
247
- if (error)
248
- throw error;
249
- return { data: this.currentSession, user: this.currentUser, error: null };
288
+ const { session, error } = yield this.getSession();
289
+ if (error) {
290
+ return { user: null, error };
250
291
  }
251
- catch (e) {
252
- return { data: null, user: null, error: e };
292
+ if (!session) {
293
+ return { user: null, error: null };
253
294
  }
295
+ return { user: session.user, error: null };
254
296
  });
255
297
  }
256
298
  /**
257
299
  * Updates user data, if there is a logged in user.
258
300
  */
259
301
  update(attributes) {
260
- var _a;
261
302
  return __awaiter(this, void 0, void 0, function* () {
262
303
  try {
263
- if (!((_a = this.currentSession) === null || _a === void 0 ? void 0 : _a.access_token))
264
- throw new Error('Not logged in.');
265
- const { user, error } = yield this.api.updateUser(this.currentSession.access_token, attributes);
266
- if (error)
267
- throw error;
268
- if (!user)
269
- throw Error('Invalid user data.');
270
- const session = Object.assign(Object.assign({}, this.currentSession), { user });
304
+ const { session, error: sessionError } = yield this.getSession();
305
+ if (sessionError) {
306
+ throw sessionError;
307
+ }
308
+ if (!session) {
309
+ throw new errors_1.AuthSessionMissingError();
310
+ }
311
+ const { user, error: userError } = yield this.api.updateUser(session.access_token, attributes);
312
+ if (userError)
313
+ throw userError;
314
+ session.user = user;
271
315
  this._saveSession(session);
272
- this._notifyAllSubscribers('USER_UPDATED');
273
- return { data: user, user, error: null };
316
+ this._notifyAllSubscribers('USER_UPDATED', session);
317
+ return { user, error: null };
274
318
  }
275
- catch (e) {
276
- return { data: null, user: null, error: e };
319
+ catch (error) {
320
+ if ((0, errors_1.isAuthError)(error)) {
321
+ return { user: null, error };
322
+ }
323
+ throw error;
277
324
  }
278
325
  });
279
326
  }
@@ -285,30 +332,24 @@ class GoTrueClient {
285
332
  return __awaiter(this, void 0, void 0, function* () {
286
333
  try {
287
334
  if (!refresh_token) {
288
- throw new Error('No current session.');
335
+ throw new errors_1.AuthSessionMissingError();
289
336
  }
290
- const { data, error } = yield this.api.refreshAccessToken(refresh_token);
337
+ const { session, error } = yield this.api.refreshAccessToken(refresh_token);
291
338
  if (error) {
292
339
  return { session: null, error: error };
293
340
  }
294
- this._saveSession(data);
295
- this._notifyAllSubscribers('SIGNED_IN');
296
- return { session: data, error: null };
341
+ this._saveSession(session);
342
+ this._notifyAllSubscribers('TOKEN_REFRESHED', session);
343
+ return { session: session, error: null };
297
344
  }
298
- catch (e) {
299
- return { error: e, session: null };
345
+ catch (error) {
346
+ if ((0, errors_1.isAuthError)(error)) {
347
+ return { session: null, error };
348
+ }
349
+ throw error;
300
350
  }
301
351
  });
302
352
  }
303
- /**
304
- * Overrides the JWT on the current client. The JWT will then be sent in all subsequent network requests.
305
- * @param access_token a jwt access token
306
- */
307
- setAuth(access_token) {
308
- this.currentSession = Object.assign(Object.assign({}, this.currentSession), { access_token, token_type: 'bearer', user: this.user() });
309
- this._notifyAllSubscribers('TOKEN_REFRESHED');
310
- return this.currentSession;
311
- }
312
353
  /**
313
354
  * Gets the session data from a URL string
314
355
  * @param options.storeSession Optionally store the session in the browser
@@ -317,23 +358,23 @@ class GoTrueClient {
317
358
  return __awaiter(this, void 0, void 0, function* () {
318
359
  try {
319
360
  if (!(0, helpers_1.isBrowser)())
320
- throw new Error('No browser detected.');
361
+ throw new errors_1.AuthApiError('No browser detected.', 500);
321
362
  const error_description = (0, helpers_1.getParameterByName)('error_description');
322
363
  if (error_description)
323
- throw new Error(error_description);
364
+ throw new errors_1.AuthApiError(error_description, 500);
324
365
  const provider_token = (0, helpers_1.getParameterByName)('provider_token');
325
366
  const access_token = (0, helpers_1.getParameterByName)('access_token');
326
367
  if (!access_token)
327
- throw new Error('No access_token detected.');
368
+ throw new errors_1.AuthApiError('No access_token detected.', 500);
328
369
  const expires_in = (0, helpers_1.getParameterByName)('expires_in');
329
370
  if (!expires_in)
330
- throw new Error('No expires_in detected.');
371
+ throw new errors_1.AuthApiError('No expires_in detected.', 500);
331
372
  const refresh_token = (0, helpers_1.getParameterByName)('refresh_token');
332
373
  if (!refresh_token)
333
- throw new Error('No refresh_token detected.');
374
+ throw new errors_1.AuthApiError('No refresh_token detected.', 500);
334
375
  const token_type = (0, helpers_1.getParameterByName)('token_type');
335
376
  if (!token_type)
336
- throw new Error('No token_type detected.');
377
+ throw new errors_1.AuthApiError('No token_type detected.', 500);
337
378
  const timeNow = Math.round(Date.now() / 1000);
338
379
  const expires_at = timeNow + parseInt(expires_in);
339
380
  const { user, error } = yield this.api.getUser(access_token);
@@ -351,17 +392,20 @@ class GoTrueClient {
351
392
  if (options === null || options === void 0 ? void 0 : options.storeSession) {
352
393
  this._saveSession(session);
353
394
  const recoveryMode = (0, helpers_1.getParameterByName)('type');
354
- this._notifyAllSubscribers('SIGNED_IN');
395
+ this._notifyAllSubscribers('SIGNED_IN', session);
355
396
  if (recoveryMode === 'recovery') {
356
- this._notifyAllSubscribers('PASSWORD_RECOVERY');
397
+ this._notifyAllSubscribers('PASSWORD_RECOVERY', session);
357
398
  }
358
399
  }
359
400
  // Remove tokens from URL
360
401
  window.location.hash = '';
361
- return { data: session, error: null };
402
+ return { session, error: null };
362
403
  }
363
- catch (e) {
364
- return { data: null, error: e };
404
+ catch (error) {
405
+ if ((0, errors_1.isAuthError)(error)) {
406
+ return { session: null, error };
407
+ }
408
+ throw error;
365
409
  }
366
410
  });
367
411
  }
@@ -372,16 +416,19 @@ class GoTrueClient {
372
416
  * For server-side management, you can revoke all refresh tokens for a user by passing a user's JWT through to `auth.api.signOut(JWT: string)`. There is no way to revoke a user's session JWT before it automatically expires
373
417
  */
374
418
  signOut() {
375
- var _a;
376
419
  return __awaiter(this, void 0, void 0, function* () {
377
- const accessToken = (_a = this.currentSession) === null || _a === void 0 ? void 0 : _a.access_token;
378
- this._removeSession();
379
- this._notifyAllSubscribers('SIGNED_OUT');
420
+ const { session, error: sessionError } = yield this.getSession();
421
+ if (sessionError) {
422
+ return { error: sessionError };
423
+ }
424
+ const accessToken = session === null || session === void 0 ? void 0 : session.access_token;
380
425
  if (accessToken) {
381
426
  const { error } = yield this.api.signOut(accessToken);
382
427
  if (error)
383
428
  return { error };
384
429
  }
430
+ this._removeSession();
431
+ this._notifyAllSubscribers('SIGNED_OUT', null);
385
432
  return { error: null };
386
433
  });
387
434
  }
@@ -400,10 +447,13 @@ class GoTrueClient {
400
447
  },
401
448
  };
402
449
  this.stateChangeEmitters.set(id, subscription);
403
- return { data: subscription, error: null };
450
+ return { subscription, error: null };
404
451
  }
405
- catch (e) {
406
- return { data: null, error: e };
452
+ catch (error) {
453
+ if ((0, errors_1.isAuthError)(error)) {
454
+ return { subscription: null, error };
455
+ }
456
+ throw error;
407
457
  }
408
458
  }
409
459
  _handleEmailSignIn(email, password, options = {}) {
@@ -411,19 +461,21 @@ class GoTrueClient {
411
461
  return __awaiter(this, void 0, void 0, function* () {
412
462
  try {
413
463
  const { data, error } = yield this.api.signInWithEmail(email, password, {
414
- redirectTo: options.redirectTo,
415
464
  captchaToken: options.captchaToken,
416
465
  });
417
466
  if (error || !data)
418
- return { data: null, user: null, session: null, error };
467
+ return { user: null, session: null, error };
419
468
  if (((_a = data === null || data === void 0 ? void 0 : data.user) === null || _a === void 0 ? void 0 : _a.confirmed_at) || ((_b = data === null || data === void 0 ? void 0 : data.user) === null || _b === void 0 ? void 0 : _b.email_confirmed_at)) {
420
469
  this._saveSession(data);
421
- this._notifyAllSubscribers('SIGNED_IN');
470
+ this._notifyAllSubscribers('SIGNED_IN', data);
422
471
  }
423
- return { data, user: data.user, session: data, error: null };
472
+ return { user: data.user, session: data, error: null };
424
473
  }
425
- catch (e) {
426
- return { data: null, user: null, session: null, error: e };
474
+ catch (error) {
475
+ if ((0, errors_1.isAuthError)(error)) {
476
+ return { user: null, session: null, error };
477
+ }
478
+ throw error;
427
479
  }
428
480
  });
429
481
  }
@@ -431,17 +483,22 @@ class GoTrueClient {
431
483
  var _a;
432
484
  return __awaiter(this, void 0, void 0, function* () {
433
485
  try {
434
- const { data, error } = yield this.api.signInWithPhone(phone, password, options);
435
- if (error || !data)
436
- return { data: null, user: null, session: null, error };
437
- if ((_a = data === null || data === void 0 ? void 0 : data.user) === null || _a === void 0 ? void 0 : _a.phone_confirmed_at) {
438
- this._saveSession(data);
439
- this._notifyAllSubscribers('SIGNED_IN');
486
+ const { session, error } = yield this.api.signInWithPhone(phone, password, {
487
+ captchaToken: options.captchaToken,
488
+ });
489
+ if (error || !session)
490
+ return { session: null, user: null, error };
491
+ if ((_a = session === null || session === void 0 ? void 0 : session.user) === null || _a === void 0 ? void 0 : _a.phone_confirmed_at) {
492
+ this._saveSession(session);
493
+ this._notifyAllSubscribers('SIGNED_IN', session);
440
494
  }
441
- return { data, user: data.user, session: data, error: null };
495
+ return { session, user: session.user, error: null };
442
496
  }
443
- catch (e) {
444
- return { data: null, user: null, session: null, error: e };
497
+ catch (error) {
498
+ if ((0, errors_1.isAuthError)(error)) {
499
+ return { session: null, user: null, error };
500
+ }
501
+ throw error;
445
502
  }
446
503
  });
447
504
  }
@@ -451,64 +508,39 @@ class GoTrueClient {
451
508
  scopes: options.scopes,
452
509
  queryParams: options.queryParams,
453
510
  });
454
- try {
455
- // try to open on the browser
456
- if ((0, helpers_1.isBrowser)()) {
457
- window.location.href = url;
458
- }
459
- return { provider, url, data: null, session: null, user: null, error: null };
460
- }
461
- catch (e) {
462
- // fallback to returning the URL
463
- if (url)
464
- return { provider, url, data: null, session: null, user: null, error: null };
465
- return { data: null, user: null, session: null, error: e };
511
+ // try to open on the browser
512
+ if ((0, helpers_1.isBrowser)()) {
513
+ window.location.href = url;
466
514
  }
515
+ return { provider, url, error: null };
467
516
  }
468
517
  _handleOpenIDConnectSignIn({ id_token, nonce, client_id, issuer, provider, }) {
469
518
  return __awaiter(this, void 0, void 0, function* () {
470
519
  if (id_token && nonce && ((client_id && issuer) || provider)) {
471
520
  try {
472
- const { data, error } = yield this.api.signInWithOpenIDConnect({
521
+ const { session, error } = yield this.api.signInWithOpenIDConnect({
473
522
  id_token,
474
523
  nonce,
475
524
  client_id,
476
525
  issuer,
477
526
  provider,
478
527
  });
479
- if (error || !data)
528
+ if (error || !session)
480
529
  return { user: null, session: null, error };
481
- this._saveSession(data);
482
- this._notifyAllSubscribers('SIGNED_IN');
483
- return { user: data.user, session: data, error: null };
530
+ this._saveSession(session);
531
+ this._notifyAllSubscribers('SIGNED_IN', session);
532
+ return { user: session.user, session: session, error: null };
484
533
  }
485
- catch (e) {
486
- return { user: null, session: null, error: e };
534
+ catch (error) {
535
+ if ((0, errors_1.isAuthError)(error)) {
536
+ return { user: null, session: null, error };
537
+ }
538
+ throw error;
487
539
  }
488
540
  }
489
- throw new Error(`You must provide a OpenID Connect provider with your id token and nonce.`);
541
+ throw new errors_1.AuthInvalidCredentialsError('You must provide an OpenID Connect provider with your id token and nonce.');
490
542
  });
491
543
  }
492
- /**
493
- * Attempts to get the session from LocalStorage
494
- * Note: this should never be async (even for React Native), as we need it to return immediately in the constructor.
495
- */
496
- _recoverSession() {
497
- try {
498
- const data = (0, helpers_1.getItemSynchronously)(this.localStorage, constants_1.STORAGE_KEY);
499
- if (!data)
500
- return null;
501
- const { currentSession, expiresAt } = data;
502
- const timeNow = Math.round(Date.now() / 1000);
503
- if (expiresAt >= timeNow + constants_1.EXPIRY_MARGIN && (currentSession === null || currentSession === void 0 ? void 0 : currentSession.user)) {
504
- this._saveSession(currentSession);
505
- this._notifyAllSubscribers('SIGNED_IN');
506
- }
507
- }
508
- catch (error) {
509
- console.log('error', error);
510
- }
511
- }
512
544
  /**
513
545
  * Recovers the session from LocalStorage and refreshes
514
546
  * Note: this method is async to accommodate for AsyncStorage e.g. in React native.
@@ -516,7 +548,7 @@ class GoTrueClient {
516
548
  _recoverAndRefresh() {
517
549
  return __awaiter(this, void 0, void 0, function* () {
518
550
  try {
519
- const data = yield (0, helpers_1.getItemAsync)(this.localStorage, constants_1.STORAGE_KEY);
551
+ const data = yield (0, helpers_1.getItemAsync)(this.localStorage, this.storageKey);
520
552
  if (!data)
521
553
  return null;
522
554
  const { currentSession, expiresAt } = data;
@@ -548,10 +580,10 @@ class GoTrueClient {
548
580
  this._removeSession();
549
581
  }
550
582
  else {
551
- // should be handled on _recoverSession method already
552
- // But we still need the code here to accommodate for AsyncStorage e.g. in React native
553
- this._saveSession(currentSession);
554
- this._notifyAllSubscribers('SIGNED_IN');
583
+ if (this.persistSession) {
584
+ this._saveSession(currentSession);
585
+ }
586
+ this._notifyAllSubscribers('SIGNED_IN', currentSession);
555
587
  }
556
588
  }
557
589
  catch (err) {
@@ -560,115 +592,101 @@ class GoTrueClient {
560
592
  }
561
593
  });
562
594
  }
563
- _callRefreshToken(refresh_token) {
564
- var _a;
565
- if (refresh_token === void 0) { refresh_token = (_a = this.currentSession) === null || _a === void 0 ? void 0 : _a.refresh_token; }
595
+ _callRefreshToken(refreshToken) {
566
596
  return __awaiter(this, void 0, void 0, function* () {
567
597
  try {
568
- if (!refresh_token) {
569
- throw new Error('No current session.');
598
+ // refreshing is already in progress
599
+ if (this.refreshingDeferred) {
600
+ return yield this.refreshingDeferred.promise;
601
+ }
602
+ this.refreshingDeferred = new helpers_1.Deferred();
603
+ if (!refreshToken) {
604
+ throw new errors_1.AuthSessionMissingError();
570
605
  }
571
- const { data, error } = yield this.api.refreshAccessToken(refresh_token);
606
+ const { session, error } = yield this.api.refreshAccessToken(refreshToken);
572
607
  if (error)
573
608
  throw error;
574
- if (!data)
575
- throw Error('Invalid session data.');
576
- this._saveSession(data);
577
- this._notifyAllSubscribers('TOKEN_REFRESHED');
578
- this._notifyAllSubscribers('SIGNED_IN');
579
- return { data, error: null };
609
+ if (!session)
610
+ throw new errors_1.AuthSessionMissingError();
611
+ this._saveSession(session);
612
+ this._notifyAllSubscribers('TOKEN_REFRESHED', session);
613
+ const result = { session, error: null };
614
+ this.refreshingDeferred.resolve(result);
615
+ this.refreshingDeferred = null;
616
+ return result;
580
617
  }
581
- catch (e) {
582
- return { data: null, error: e };
618
+ catch (error) {
619
+ if ((0, errors_1.isAuthError)(error)) {
620
+ return { session: null, error };
621
+ }
622
+ throw error;
583
623
  }
584
624
  });
585
625
  }
586
- _notifyAllSubscribers(event) {
587
- this.stateChangeEmitters.forEach((x) => x.callback(event, this.currentSession));
626
+ _notifyAllSubscribers(event, session) {
627
+ this.stateChangeEmitters.forEach((x) => x.callback(event, session));
588
628
  }
589
629
  /**
590
630
  * set currentSession and currentUser
591
631
  * process to _startAutoRefreshToken if possible
592
632
  */
593
633
  _saveSession(session) {
594
- this.currentSession = session;
595
- this.currentUser = session.user;
634
+ if (!this.persistSession) {
635
+ this.inMemorySession = session;
636
+ }
596
637
  const expiresAt = session.expires_at;
597
638
  if (expiresAt) {
598
639
  const timeNow = Math.round(Date.now() / 1000);
599
640
  const expiresIn = expiresAt - timeNow;
600
641
  const refreshDurationBeforeExpires = expiresIn > constants_1.EXPIRY_MARGIN ? constants_1.EXPIRY_MARGIN : 0.5;
601
- this._startAutoRefreshToken((expiresIn - refreshDurationBeforeExpires) * 1000);
642
+ this._startAutoRefreshToken((expiresIn - refreshDurationBeforeExpires) * 1000, session);
602
643
  }
603
644
  // Do we need any extra check before persist session
604
645
  // access_token or user ?
605
646
  if (this.persistSession && session.expires_at) {
606
- this._persistSession(this.currentSession);
647
+ this._persistSession(session);
607
648
  }
608
649
  }
609
650
  _persistSession(currentSession) {
610
651
  const data = { currentSession, expiresAt: currentSession.expires_at };
611
- (0, helpers_1.setItemAsync)(this.localStorage, constants_1.STORAGE_KEY, data);
652
+ (0, helpers_1.setItemAsync)(this.localStorage, this.storageKey, data);
612
653
  }
613
654
  _removeSession() {
614
655
  return __awaiter(this, void 0, void 0, function* () {
615
- this.currentSession = null;
616
- this.currentUser = null;
617
- if (this.refreshTokenTimer)
656
+ if (this.persistSession) {
657
+ (0, helpers_1.removeItemAsync)(this.localStorage, this.storageKey);
658
+ }
659
+ else {
660
+ this.inMemorySession = null;
661
+ }
662
+ if (this.refreshTokenTimer) {
618
663
  clearTimeout(this.refreshTokenTimer);
619
- (0, helpers_1.removeItemAsync)(this.localStorage, constants_1.STORAGE_KEY);
664
+ }
620
665
  });
621
666
  }
622
667
  /**
623
668
  * Clear and re-create refresh token timer
624
669
  * @param value time intervals in milliseconds
625
670
  */
626
- _startAutoRefreshToken(value) {
671
+ _startAutoRefreshToken(value, session) {
627
672
  if (this.refreshTokenTimer)
628
673
  clearTimeout(this.refreshTokenTimer);
629
674
  if (value <= 0 || !this.autoRefreshToken)
630
675
  return;
631
676
  this.refreshTokenTimer = setTimeout(() => __awaiter(this, void 0, void 0, function* () {
632
677
  this.networkRetries++;
633
- const { error } = yield this._callRefreshToken();
678
+ const { error } = yield this._callRefreshToken(session.refresh_token);
634
679
  if (!error)
635
680
  this.networkRetries = 0;
636
681
  if ((error === null || error === void 0 ? void 0 : error.message) === constants_1.NETWORK_FAILURE.ERROR_MESSAGE &&
637
682
  this.networkRetries < constants_1.NETWORK_FAILURE.MAX_RETRIES)
638
- this._startAutoRefreshToken(Math.pow(constants_1.NETWORK_FAILURE.RETRY_INTERVAL, this.networkRetries) * 100); // exponential backoff
683
+ this._startAutoRefreshToken(Math.pow(constants_1.NETWORK_FAILURE.RETRY_INTERVAL, this.networkRetries) * 100, session); // exponential backoff
639
684
  }), value);
640
685
  if (typeof this.refreshTokenTimer.unref === 'function')
641
686
  this.refreshTokenTimer.unref();
642
687
  }
643
- /**
644
- * Listens for changes to LocalStorage and updates the current session.
645
- */
646
- _listenForMultiTabEvents() {
647
- if (!this.multiTab || !(0, helpers_1.isBrowser)() || !(window === null || window === void 0 ? void 0 : window.addEventListener)) {
648
- return false;
649
- }
650
- try {
651
- window === null || window === void 0 ? void 0 : window.addEventListener('storage', (e) => {
652
- var _a;
653
- if (e.key === constants_1.STORAGE_KEY) {
654
- const newSession = JSON.parse(String(e.newValue));
655
- if ((_a = newSession === null || newSession === void 0 ? void 0 : newSession.currentSession) === null || _a === void 0 ? void 0 : _a.access_token) {
656
- this._saveSession(newSession.currentSession);
657
- this._notifyAllSubscribers('SIGNED_IN');
658
- }
659
- else {
660
- this._removeSession();
661
- this._notifyAllSubscribers('SIGNED_OUT');
662
- }
663
- }
664
- });
665
- }
666
- catch (error) {
667
- console.error('_listenForMultiTabEvents', error);
668
- }
669
- }
670
688
  _handleVisibilityChange() {
671
- if (!this.multiTab || !(0, helpers_1.isBrowser)() || !(window === null || window === void 0 ? void 0 : window.addEventListener)) {
689
+ if (!(0, helpers_1.isBrowser)() || !(window === null || window === void 0 ? void 0 : window.addEventListener)) {
672
690
  return false;
673
691
  }
674
692
  try {