@supabase/gotrue-js 2.40.0 → 2.41.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.
@@ -168,6 +168,19 @@ export default class GoTrueClient {
168
168
  };
169
169
  error: null;
170
170
  }>;
171
+ /**
172
+ * Use instead of {@link #getSession} inside the library. It is
173
+ * semantically usually what you want, as getting a session involves some
174
+ * processing afterwards that requires only one client operating on the
175
+ * session at once across multiple tabs or processes.
176
+ */
177
+ private _useSession;
178
+ /**
179
+ * NEVER USE DIRECTLY!
180
+ *
181
+ * Always use {@link #_useSession}.
182
+ */
183
+ private __loadSession;
171
184
  /**
172
185
  * Gets the current user details if there is an existing session.
173
186
  * @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.
@@ -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,EAaT,MAAM,eAAe,CAAA;AAItB,OAAO,KAAK,EACV,eAAe,EACf,YAAY,EACZ,iBAAiB,EACjB,eAAe,EACf,sBAAsB,EACtB,mBAAmB,EACnB,gBAAgB,EAChB,aAAa,EACb,WAAW,EAEX,OAAO,EACP,4BAA4B,EAC5B,0BAA0B,EAC1B,6BAA6B,EAC7B,iCAAiC,EACjC,6BAA6B,EAC7B,aAAa,EACb,OAAO,EACP,YAAY,EACZ,gBAAgB,EAEhB,cAAc,EACd,YAAY,EACZ,eAAe,EACf,YAAY,EAeZ,YAAY,EACZ,YAAY,EACb,MAAM,aAAa,CAAA;AAsBpB,MAAM,CAAC,OAAO,OAAO,YAAY;IAC/B,OAAO,CAAC,MAAM,CAAC,cAAc,CAAI;IAEjC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,CAAA;QACnB,YAAY,CAAC,EAAE,MAAM,CAAA;KACjB,GACL,OAAO,CACN;QACE,IAAI,EAAE,EAAE,CAAA;QACR,KAAK,EAAE,IAAI,CAAA;KACZ,GACD;QAAE,IAAI,EAAE,IAAI,CAAC;QAAC,KAAK,EAAE,SAAS,CAAA;KAAE,CACnC;IA6BD;;;OAGG;YACW,mBAAmB;IAuCjC,OAAO,CAAC,eAAe;YAWT,qBAAqB;IAyBnC;;;OAGG;YACW,kBAAkB;YA0DlB,iBAAiB;YAgDjB,qBAAqB;IAoCnC;;;OAGG;YACW,YAAY;IAY1B,OAAO,CAAC,eAAe;YAMT,cAAc;IAU5B;;;;;OAKG;IACH,OAAO,CAAC,gCAAgC;IAexC;;;OAGG;YACW,iBAAiB;IA4B/B;;;OAGG;YACW,gBAAgB;IAW9B;;;;;;;;;;;;;;;;;;;;;OAqBG;IACG,gBAAgB;IAKtB;;;;;;;OAOG;IACG,eAAe;IAKrB;;OAEG;YACW,qBAAqB;IAuCnC;;;;OAIG;YACW,uBAAuB;IAyBrC;;OAEG;YACW,oBAAoB;IA2BlC;;;;;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"}
@@ -138,7 +138,6 @@ class GoTrueClient {
138
138
  }
139
139
  const { session, redirectType } = data;
140
140
  this._debug('#_initialize()', 'detected session in URL', session, 'redirect type', redirectType);
141
- await this._saveSession(session);
142
141
  setTimeout(async () => {
143
142
  if (redirectType === 'recovery') {
144
143
  await this._notifyAllSubscribers('PASSWORD_RECOVERY', session);
@@ -526,16 +525,18 @@ class GoTrueClient {
526
525
  */
527
526
  async reauthenticate() {
528
527
  try {
529
- const { data: { session }, error: sessionError, } = await this.getSession();
530
- if (sessionError)
531
- throw sessionError;
532
- if (!session)
533
- throw new errors_1.AuthSessionMissingError();
534
- const { error } = await (0, fetch_1._request)(this.fetch, 'GET', `${this.url}/reauthenticate`, {
535
- headers: this.headers,
536
- jwt: session.access_token,
528
+ return await this._useSession(async (result) => {
529
+ const { data: { session }, error: sessionError, } = result;
530
+ if (sessionError)
531
+ throw sessionError;
532
+ if (!session)
533
+ throw new errors_1.AuthSessionMissingError();
534
+ const { error } = await (0, fetch_1._request)(this.fetch, 'GET', `${this.url}/reauthenticate`, {
535
+ headers: this.headers,
536
+ jwt: session.access_token,
537
+ });
538
+ return { data: { user: null, session: null }, error };
537
539
  });
538
- return { data: { user: null, session: null }, error };
539
540
  }
540
541
  catch (error) {
541
542
  if ((0, errors_1.isAuthError)(error)) {
@@ -592,10 +593,36 @@ class GoTrueClient {
592
593
  * 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.
593
594
  */
594
595
  async getSession() {
596
+ return this._useSession(async (result) => {
597
+ return result;
598
+ });
599
+ }
600
+ /**
601
+ * Use instead of {@link #getSession} inside the library. It is
602
+ * semantically usually what you want, as getting a session involves some
603
+ * processing afterwards that requires only one client operating on the
604
+ * session at once across multiple tabs or processes.
605
+ */
606
+ async _useSession(fn) {
607
+ return await (0, helpers_1.stackGuard)('_useSession', async () => {
608
+ // the use of __loadSession here is the only correct use of the function!
609
+ const result = await this.__loadSession();
610
+ return await fn(result);
611
+ });
612
+ }
613
+ /**
614
+ * NEVER USE DIRECTLY!
615
+ *
616
+ * Always use {@link #_useSession}.
617
+ */
618
+ async __loadSession() {
619
+ if (this.logDebugMessages && !(0, helpers_1.isInStackGuard)('_useSession')) {
620
+ throw new Error('Please use #_useSession()');
621
+ }
595
622
  // make sure we've read the session from the url if there is one
596
623
  // save to just await, as long we make sure _initialize() never throws
597
624
  await this.initializePromise;
598
- this._debug('#getSession()', 'begin');
625
+ this._debug('#__loadSession()', 'begin');
599
626
  try {
600
627
  let currentSession = null;
601
628
  if (this.persistSession) {
@@ -621,7 +648,7 @@ class GoTrueClient {
621
648
  const hasExpired = currentSession.expires_at
622
649
  ? currentSession.expires_at <= Date.now() / 1000
623
650
  : false;
624
- this._debug('#getSession()', `session has${hasExpired ? '' : ' not'} expired`, 'expires_at', currentSession.expires_at);
651
+ this._debug('#__loadSession()', `session has${hasExpired ? '' : ' not'} expired`, 'expires_at', currentSession.expires_at);
625
652
  if (!hasExpired) {
626
653
  return { data: { session: currentSession }, error: null };
627
654
  }
@@ -632,7 +659,7 @@ class GoTrueClient {
632
659
  return { data: { session }, error: null };
633
660
  }
634
661
  finally {
635
- this._debug('#getSession()', 'end');
662
+ this._debug('#__loadSession()', 'end');
636
663
  }
637
664
  }
638
665
  /**
@@ -640,20 +667,22 @@ class GoTrueClient {
640
667
  * @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.
641
668
  */
642
669
  async getUser(jwt) {
643
- var _a, _b;
644
670
  try {
645
- if (!jwt) {
646
- const { data, error } = await this.getSession();
647
- if (error) {
648
- throw error;
671
+ return await this._useSession(async (result) => {
672
+ var _a, _b;
673
+ if (!jwt) {
674
+ const { data, error } = result;
675
+ if (error) {
676
+ throw error;
677
+ }
678
+ // Default to Authorization header if there is no existing session
679
+ jwt = (_b = (_a = data.session) === null || _a === void 0 ? void 0 : _a.access_token) !== null && _b !== void 0 ? _b : undefined;
649
680
  }
650
- // Default to Authorization header if there is no existing session
651
- jwt = (_b = (_a = data.session) === null || _a === void 0 ? void 0 : _a.access_token) !== null && _b !== void 0 ? _b : undefined;
652
- }
653
- return await (0, fetch_1._request)(this.fetch, 'GET', `${this.url}/user`, {
654
- headers: this.headers,
655
- jwt: jwt,
656
- xform: fetch_1._userResponse,
681
+ return await (0, fetch_1._request)(this.fetch, 'GET', `${this.url}/user`, {
682
+ headers: this.headers,
683
+ jwt: jwt,
684
+ xform: fetch_1._userResponse,
685
+ });
657
686
  });
658
687
  }
659
688
  catch (error) {
@@ -668,27 +697,29 @@ class GoTrueClient {
668
697
  */
669
698
  async updateUser(attributes, options = {}) {
670
699
  try {
671
- const { data: sessionData, error: sessionError } = await this.getSession();
672
- if (sessionError) {
673
- throw sessionError;
674
- }
675
- if (!sessionData.session) {
676
- throw new errors_1.AuthSessionMissingError();
677
- }
678
- const session = sessionData.session;
679
- const { data, error: userError } = await (0, fetch_1._request)(this.fetch, 'PUT', `${this.url}/user`, {
680
- headers: this.headers,
681
- redirectTo: options === null || options === void 0 ? void 0 : options.emailRedirectTo,
682
- body: attributes,
683
- jwt: session.access_token,
684
- xform: fetch_1._userResponse,
700
+ return await this._useSession(async (result) => {
701
+ const { data: sessionData, error: sessionError } = result;
702
+ if (sessionError) {
703
+ throw sessionError;
704
+ }
705
+ if (!sessionData.session) {
706
+ throw new errors_1.AuthSessionMissingError();
707
+ }
708
+ const session = sessionData.session;
709
+ const { data, error: userError } = await (0, fetch_1._request)(this.fetch, 'PUT', `${this.url}/user`, {
710
+ headers: this.headers,
711
+ redirectTo: options === null || options === void 0 ? void 0 : options.emailRedirectTo,
712
+ body: attributes,
713
+ jwt: session.access_token,
714
+ xform: fetch_1._userResponse,
715
+ });
716
+ if (userError)
717
+ throw userError;
718
+ session.user = data.user;
719
+ await this._saveSession(session);
720
+ await this._notifyAllSubscribers('USER_UPDATED', session);
721
+ return { data: { user: session.user }, error: null };
685
722
  });
686
- if (userError)
687
- throw userError;
688
- session.user = data.user;
689
- await this._saveSession(session);
690
- await this._notifyAllSubscribers('USER_UPDATED', session);
691
- return { data: { user: session.user }, error: null };
692
723
  }
693
724
  catch (error) {
694
725
  if ((0, errors_1.isAuthError)(error)) {
@@ -764,26 +795,28 @@ class GoTrueClient {
764
795
  * @param currentSession The current session. If passed in, it must contain a refresh token.
765
796
  */
766
797
  async refreshSession(currentSession) {
767
- var _a;
768
798
  try {
769
- if (!currentSession) {
770
- const { data, error } = await this.getSession();
799
+ return await this._useSession(async (result) => {
800
+ var _a;
801
+ if (!currentSession) {
802
+ const { data, error } = result;
803
+ if (error) {
804
+ throw error;
805
+ }
806
+ currentSession = (_a = data.session) !== null && _a !== void 0 ? _a : undefined;
807
+ }
808
+ if (!(currentSession === null || currentSession === void 0 ? void 0 : currentSession.refresh_token)) {
809
+ throw new errors_1.AuthSessionMissingError();
810
+ }
811
+ const { session, error } = await this._callRefreshToken(currentSession.refresh_token);
771
812
  if (error) {
772
- throw error;
813
+ return { data: { user: null, session: null }, error: error };
773
814
  }
774
- currentSession = (_a = data.session) !== null && _a !== void 0 ? _a : undefined;
775
- }
776
- if (!(currentSession === null || currentSession === void 0 ? void 0 : currentSession.refresh_token)) {
777
- throw new errors_1.AuthSessionMissingError();
778
- }
779
- const { session, error } = await this._callRefreshToken(currentSession.refresh_token);
780
- if (error) {
781
- return { data: { user: null, session: null }, error: error };
782
- }
783
- if (!session) {
784
- return { data: { user: null, session: null }, error: null };
785
- }
786
- return { data: { user: session.user, session }, error: null };
815
+ if (!session) {
816
+ return { data: { user: null, session: null }, error: null };
817
+ }
818
+ return { data: { user: session.user, session }, error: null };
819
+ });
787
820
  }
788
821
  catch (error) {
789
822
  if ((0, errors_1.isAuthError)(error)) {
@@ -897,28 +930,30 @@ class GoTrueClient {
897
930
  * If using others scope, no `SIGNED_OUT` event is fired!
898
931
  */
899
932
  async signOut({ scope } = { scope: 'global' }) {
900
- var _a;
901
- const { data, error: sessionError } = await this.getSession();
902
- if (sessionError) {
903
- return { error: sessionError };
904
- }
905
- const accessToken = (_a = data.session) === null || _a === void 0 ? void 0 : _a.access_token;
906
- if (accessToken) {
907
- const { error } = await this.admin.signOut(accessToken, scope);
908
- if (error) {
909
- // ignore 404s since user might not exist anymore
910
- // ignore 401s since an invalid or expired JWT should sign out the current session
911
- if (!((0, errors_1.isAuthApiError)(error) && (error.status === 404 || error.status === 401))) {
912
- return { error };
933
+ return await this._useSession(async (result) => {
934
+ var _a;
935
+ const { data, error: sessionError } = result;
936
+ if (sessionError) {
937
+ return { error: sessionError };
938
+ }
939
+ const accessToken = (_a = data.session) === null || _a === void 0 ? void 0 : _a.access_token;
940
+ if (accessToken) {
941
+ const { error } = await this.admin.signOut(accessToken, scope);
942
+ if (error) {
943
+ // ignore 404s since user might not exist anymore
944
+ // ignore 401s since an invalid or expired JWT should sign out the current session
945
+ if (!((0, errors_1.isAuthApiError)(error) && (error.status === 404 || error.status === 401))) {
946
+ return { error };
947
+ }
913
948
  }
914
949
  }
915
- }
916
- if (scope !== 'others') {
917
- await this._removeSession();
918
- await (0, helpers_1.removeItemAsync)(this.storage, `${this.storageKey}-code-verifier`);
919
- await this._notifyAllSubscribers('SIGNED_OUT', null);
920
- }
921
- return { error: null };
950
+ if (scope !== 'others') {
951
+ await this._removeSession();
952
+ await (0, helpers_1.removeItemAsync)(this.storage, `${this.storageKey}-code-verifier`);
953
+ await this._notifyAllSubscribers('SIGNED_OUT', null);
954
+ }
955
+ return { error: null };
956
+ });
922
957
  }
923
958
  /**
924
959
  * Receive a notification every time an auth event happens.
@@ -940,19 +975,21 @@ class GoTrueClient {
940
975
  return { data: { subscription } };
941
976
  }
942
977
  async _emitInitialSession(id) {
943
- var _a, _b;
944
- try {
945
- const { data: { session }, error, } = await this.getSession();
946
- if (error)
947
- throw error;
948
- await ((_a = this.stateChangeEmitters.get(id)) === null || _a === void 0 ? void 0 : _a.callback('INITIAL_SESSION', session));
949
- this._debug('INITIAL_SESSION', 'callback id', id, 'session', session);
950
- }
951
- catch (err) {
952
- await ((_b = this.stateChangeEmitters.get(id)) === null || _b === void 0 ? void 0 : _b.callback('INITIAL_SESSION', null));
953
- this._debug('INITIAL_SESSION', 'callback id', id, 'error', err);
954
- console.error(err);
955
- }
978
+ return await this._useSession(async (result) => {
979
+ var _a, _b;
980
+ try {
981
+ const { data: { session }, error, } = result;
982
+ if (error)
983
+ throw error;
984
+ await ((_a = this.stateChangeEmitters.get(id)) === null || _a === void 0 ? void 0 : _a.callback('INITIAL_SESSION', session));
985
+ this._debug('INITIAL_SESSION', 'callback id', id, 'session', session);
986
+ }
987
+ catch (err) {
988
+ await ((_b = this.stateChangeEmitters.get(id)) === null || _b === void 0 ? void 0 : _b.callback('INITIAL_SESSION', null));
989
+ this._debug('INITIAL_SESSION', 'callback id', id, 'error', err);
990
+ console.error(err);
991
+ }
992
+ });
956
993
  }
957
994
  /**
958
995
  * Sends a password reset request to an email address.
@@ -1292,17 +1329,19 @@ class GoTrueClient {
1292
1329
  try {
1293
1330
  const now = Date.now();
1294
1331
  try {
1295
- const { data: { session }, } = await this.getSession();
1296
- if (!session || !session.refresh_token || !session.expires_at) {
1297
- this._debug('#_autoRefreshTokenTick()', 'no session');
1298
- return;
1299
- }
1300
- // session will expire in this many ticks (or has already expired if <= 0)
1301
- const expiresInTicks = Math.floor((session.expires_at * 1000 - now) / AUTO_REFRESH_TICK_DURATION);
1302
- 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`);
1303
- if (expiresInTicks <= AUTO_REFRESH_TICK_THRESHOLD) {
1304
- await this._callRefreshToken(session.refresh_token);
1305
- }
1332
+ return await this._useSession(async (result) => {
1333
+ const { data: { session }, } = result;
1334
+ if (!session || !session.refresh_token || !session.expires_at) {
1335
+ this._debug('#_autoRefreshTokenTick()', 'no session');
1336
+ return;
1337
+ }
1338
+ // session will expire in this many ticks (or has already expired if <= 0)
1339
+ const expiresInTicks = Math.floor((session.expires_at * 1000 - now) / AUTO_REFRESH_TICK_DURATION);
1340
+ 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`);
1341
+ if (expiresInTicks <= AUTO_REFRESH_TICK_THRESHOLD) {
1342
+ await this._callRefreshToken(session.refresh_token);
1343
+ }
1344
+ });
1306
1345
  }
1307
1346
  catch (e) {
1308
1347
  console.error('Auto refresh tick failed with error. This is likely a transient error.', e);
@@ -1394,15 +1433,17 @@ class GoTrueClient {
1394
1433
  return `${this.url}/authorize?${urlParams.join('&')}`;
1395
1434
  }
1396
1435
  async _unenroll(params) {
1397
- var _a;
1398
1436
  try {
1399
- const { data: sessionData, error: sessionError } = await this.getSession();
1400
- if (sessionError) {
1401
- return { data: null, error: sessionError };
1402
- }
1403
- return await (0, fetch_1._request)(this.fetch, 'DELETE', `${this.url}/factors/${params.factorId}`, {
1404
- headers: this.headers,
1405
- jwt: (_a = sessionData === null || sessionData === void 0 ? void 0 : sessionData.session) === null || _a === void 0 ? void 0 : _a.access_token,
1437
+ return await this._useSession(async (result) => {
1438
+ var _a;
1439
+ const { data: sessionData, error: sessionError } = result;
1440
+ if (sessionError) {
1441
+ return { data: null, error: sessionError };
1442
+ }
1443
+ return await (0, fetch_1._request)(this.fetch, 'DELETE', `${this.url}/factors/${params.factorId}`, {
1444
+ headers: this.headers,
1445
+ jwt: (_a = sessionData === null || sessionData === void 0 ? void 0 : sessionData.session) === null || _a === void 0 ? void 0 : _a.access_token,
1446
+ });
1406
1447
  });
1407
1448
  }
1408
1449
  catch (error) {
@@ -1416,28 +1457,30 @@ class GoTrueClient {
1416
1457
  * {@see GoTrueMFAApi#enroll}
1417
1458
  */
1418
1459
  async _enroll(params) {
1419
- var _a, _b;
1420
1460
  try {
1421
- const { data: sessionData, error: sessionError } = await this.getSession();
1422
- if (sessionError) {
1423
- return { data: null, error: sessionError };
1424
- }
1425
- const { data, error } = await (0, fetch_1._request)(this.fetch, 'POST', `${this.url}/factors`, {
1426
- body: {
1427
- friendly_name: params.friendlyName,
1428
- factor_type: params.factorType,
1429
- issuer: params.issuer,
1430
- },
1431
- headers: this.headers,
1432
- jwt: (_a = sessionData === null || sessionData === void 0 ? void 0 : sessionData.session) === null || _a === void 0 ? void 0 : _a.access_token,
1461
+ return await this._useSession(async (result) => {
1462
+ var _a, _b;
1463
+ const { data: sessionData, error: sessionError } = result;
1464
+ if (sessionError) {
1465
+ return { data: null, error: sessionError };
1466
+ }
1467
+ const { data, error } = await (0, fetch_1._request)(this.fetch, 'POST', `${this.url}/factors`, {
1468
+ body: {
1469
+ friendly_name: params.friendlyName,
1470
+ factor_type: params.factorType,
1471
+ issuer: params.issuer,
1472
+ },
1473
+ headers: this.headers,
1474
+ jwt: (_a = sessionData === null || sessionData === void 0 ? void 0 : sessionData.session) === null || _a === void 0 ? void 0 : _a.access_token,
1475
+ });
1476
+ if (error) {
1477
+ return { data: null, error };
1478
+ }
1479
+ if ((_b = data === null || data === void 0 ? void 0 : data.totp) === null || _b === void 0 ? void 0 : _b.qr_code) {
1480
+ data.totp.qr_code = `data:image/svg+xml;utf-8,${data.totp.qr_code}`;
1481
+ }
1482
+ return { data, error: null };
1433
1483
  });
1434
- if (error) {
1435
- return { data: null, error };
1436
- }
1437
- if ((_b = data === null || data === void 0 ? void 0 : data.totp) === null || _b === void 0 ? void 0 : _b.qr_code) {
1438
- data.totp.qr_code = `data:image/svg+xml;utf-8,${data.totp.qr_code}`;
1439
- }
1440
- return { data, error: null };
1441
1484
  }
1442
1485
  catch (error) {
1443
1486
  if ((0, errors_1.isAuthError)(error)) {
@@ -1450,23 +1493,25 @@ class GoTrueClient {
1450
1493
  * {@see GoTrueMFAApi#verify}
1451
1494
  */
1452
1495
  async _verify(params) {
1453
- var _a;
1454
1496
  try {
1455
- const { data: sessionData, error: sessionError } = await this.getSession();
1456
- if (sessionError) {
1457
- return { data: null, error: sessionError };
1458
- }
1459
- const { data, error } = await (0, fetch_1._request)(this.fetch, 'POST', `${this.url}/factors/${params.factorId}/verify`, {
1460
- body: { code: params.code, challenge_id: params.challengeId },
1461
- headers: this.headers,
1462
- jwt: (_a = sessionData === null || sessionData === void 0 ? void 0 : sessionData.session) === null || _a === void 0 ? void 0 : _a.access_token,
1497
+ return await this._useSession(async (result) => {
1498
+ var _a;
1499
+ const { data: sessionData, error: sessionError } = result;
1500
+ if (sessionError) {
1501
+ return { data: null, error: sessionError };
1502
+ }
1503
+ const { data, error } = await (0, fetch_1._request)(this.fetch, 'POST', `${this.url}/factors/${params.factorId}/verify`, {
1504
+ body: { code: params.code, challenge_id: params.challengeId },
1505
+ headers: this.headers,
1506
+ jwt: (_a = sessionData === null || sessionData === void 0 ? void 0 : sessionData.session) === null || _a === void 0 ? void 0 : _a.access_token,
1507
+ });
1508
+ if (error) {
1509
+ return { data: null, error };
1510
+ }
1511
+ await this._saveSession(Object.assign({ expires_at: Math.round(Date.now() / 1000) + data.expires_in }, data));
1512
+ await this._notifyAllSubscribers('MFA_CHALLENGE_VERIFIED', data);
1513
+ return { data, error };
1463
1514
  });
1464
- if (error) {
1465
- return { data: null, error };
1466
- }
1467
- await this._saveSession(Object.assign({ expires_at: Math.round(Date.now() / 1000) + data.expires_in }, data));
1468
- await this._notifyAllSubscribers('MFA_CHALLENGE_VERIFIED', data);
1469
- return { data, error };
1470
1515
  }
1471
1516
  catch (error) {
1472
1517
  if ((0, errors_1.isAuthError)(error)) {
@@ -1479,15 +1524,17 @@ class GoTrueClient {
1479
1524
  * {@see GoTrueMFAApi#challenge}
1480
1525
  */
1481
1526
  async _challenge(params) {
1482
- var _a;
1483
1527
  try {
1484
- const { data: sessionData, error: sessionError } = await this.getSession();
1485
- if (sessionError) {
1486
- return { data: null, error: sessionError };
1487
- }
1488
- return await (0, fetch_1._request)(this.fetch, 'POST', `${this.url}/factors/${params.factorId}/challenge`, {
1489
- headers: this.headers,
1490
- jwt: (_a = sessionData === null || sessionData === void 0 ? void 0 : sessionData.session) === null || _a === void 0 ? void 0 : _a.access_token,
1528
+ return await this._useSession(async (result) => {
1529
+ var _a;
1530
+ const { data: sessionData, error: sessionError } = result;
1531
+ if (sessionError) {
1532
+ return { data: null, error: sessionError };
1533
+ }
1534
+ return await (0, fetch_1._request)(this.fetch, 'POST', `${this.url}/factors/${params.factorId}/challenge`, {
1535
+ headers: this.headers,
1536
+ jwt: (_a = sessionData === null || sessionData === void 0 ? void 0 : sessionData.session) === null || _a === void 0 ? void 0 : _a.access_token,
1537
+ });
1491
1538
  });
1492
1539
  }
1493
1540
  catch (error) {
@@ -1535,29 +1582,31 @@ class GoTrueClient {
1535
1582
  * {@see GoTrueMFAApi#getAuthenticatorAssuranceLevel}
1536
1583
  */
1537
1584
  async _getAuthenticatorAssuranceLevel() {
1538
- var _a, _b;
1539
- const { data: { session }, error: sessionError, } = await this.getSession();
1540
- if (sessionError) {
1541
- return { data: null, error: sessionError };
1542
- }
1543
- if (!session) {
1544
- return {
1545
- data: { currentLevel: null, nextLevel: null, currentAuthenticationMethods: [] },
1546
- error: null,
1547
- };
1548
- }
1549
- const payload = this._decodeJWT(session.access_token);
1550
- let currentLevel = null;
1551
- if (payload.aal) {
1552
- currentLevel = payload.aal;
1553
- }
1554
- let nextLevel = currentLevel;
1555
- const verifiedFactors = (_b = (_a = session.user.factors) === null || _a === void 0 ? void 0 : _a.filter((factor) => factor.status === 'verified')) !== null && _b !== void 0 ? _b : [];
1556
- if (verifiedFactors.length > 0) {
1557
- nextLevel = 'aal2';
1558
- }
1559
- const currentAuthenticationMethods = payload.amr || [];
1560
- return { data: { currentLevel, nextLevel, currentAuthenticationMethods }, error: null };
1585
+ return await this._useSession(async (result) => {
1586
+ var _a, _b;
1587
+ const { data: { session }, error: sessionError, } = result;
1588
+ if (sessionError) {
1589
+ return { data: null, error: sessionError };
1590
+ }
1591
+ if (!session) {
1592
+ return {
1593
+ data: { currentLevel: null, nextLevel: null, currentAuthenticationMethods: [] },
1594
+ error: null,
1595
+ };
1596
+ }
1597
+ const payload = this._decodeJWT(session.access_token);
1598
+ let currentLevel = null;
1599
+ if (payload.aal) {
1600
+ currentLevel = payload.aal;
1601
+ }
1602
+ let nextLevel = currentLevel;
1603
+ const verifiedFactors = (_b = (_a = session.user.factors) === null || _a === void 0 ? void 0 : _a.filter((factor) => factor.status === 'verified')) !== null && _b !== void 0 ? _b : [];
1604
+ if (verifiedFactors.length > 0) {
1605
+ nextLevel = 'aal2';
1606
+ }
1607
+ const currentAuthenticationMethods = payload.amr || [];
1608
+ return { data: { currentLevel, nextLevel, currentAuthenticationMethods }, error: null };
1609
+ });
1561
1610
  }
1562
1611
  }
1563
1612
  exports.default = GoTrueClient;