@supabase/gotrue-js 2.41.0 → 2.42.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 +0 -13
- package/dist/main/GoTrueClient.d.ts.map +1 -1
- package/dist/main/GoTrueClient.js +180 -229
- package/dist/main/GoTrueClient.js.map +1 -1
- package/dist/main/lib/helpers.d.ts +0 -23
- package/dist/main/lib/helpers.d.ts.map +1 -1
- package/dist/main/lib/helpers.js +1 -94
- package/dist/main/lib/helpers.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 +0 -13
- package/dist/module/GoTrueClient.d.ts.map +1 -1
- package/dist/module/GoTrueClient.js +181 -230
- package/dist/module/GoTrueClient.js.map +1 -1
- package/dist/module/lib/helpers.d.ts +0 -23
- package/dist/module/lib/helpers.d.ts.map +1 -1
- package/dist/module/lib/helpers.js +0 -91
- package/dist/module/lib/helpers.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 +205 -280
- package/src/lib/helpers.ts +0 -111
- package/src/lib/version.ts +1 -1
|
@@ -168,19 +168,6 @@ 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;
|
|
184
171
|
/**
|
|
185
172
|
* Gets the current user details if there is an existing session.
|
|
186
173
|
* @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,
|
|
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,OAAO,CAAC,UAAU,CAAQ;IAE1B;;;OAGG;IACH,KAAK,EAAE,cAAc,CAAA;IACrB;;OAEG;IACH,GAAG,EAAE,YAAY,CAAA;IACjB;;OAEG;IACH,SAAS,CAAC,UAAU,EAAE,MA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mB;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;IAqCnC;;;;OAIG;YACW,uBAAuB;IAyBrC;;OAEG;YACW,oBAAoB;IA2BlC;;;;;OAKG;YACW,kBAAkB;YA6ClB,SAAS;IAmBvB;;OAEG;YACW,OAAO;IAkCrB;;OAEG;YACW,OAAO;IAoCrB;;OAEG;YACW,UAAU;IAwBxB;;OAEG;YACW,mBAAmB;IAgBjC;;OAEG;YACW,YAAY;IAuB1B;;OAEG;YACW,+BAA+B;CAoC9C"}
|
|
@@ -138,6 +138,7 @@ 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);
|
|
141
142
|
setTimeout(async () => {
|
|
142
143
|
if (redirectType === 'recovery') {
|
|
143
144
|
await this._notifyAllSubscribers('PASSWORD_RECOVERY', session);
|
|
@@ -525,18 +526,16 @@ class GoTrueClient {
|
|
|
525
526
|
*/
|
|
526
527
|
async reauthenticate() {
|
|
527
528
|
try {
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
jwt: session.access_token,
|
|
537
|
-
});
|
|
538
|
-
return { data: { user: null, session: null }, error };
|
|
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,
|
|
539
537
|
});
|
|
538
|
+
return { data: { user: null, session: null }, error };
|
|
540
539
|
}
|
|
541
540
|
catch (error) {
|
|
542
541
|
if ((0, errors_1.isAuthError)(error)) {
|
|
@@ -593,36 +592,10 @@ class GoTrueClient {
|
|
|
593
592
|
* 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.
|
|
594
593
|
*/
|
|
595
594
|
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
|
-
}
|
|
622
595
|
// make sure we've read the session from the url if there is one
|
|
623
596
|
// save to just await, as long we make sure _initialize() never throws
|
|
624
597
|
await this.initializePromise;
|
|
625
|
-
this._debug('#
|
|
598
|
+
this._debug('#getSession()', 'begin');
|
|
626
599
|
try {
|
|
627
600
|
let currentSession = null;
|
|
628
601
|
if (this.persistSession) {
|
|
@@ -648,7 +621,7 @@ class GoTrueClient {
|
|
|
648
621
|
const hasExpired = currentSession.expires_at
|
|
649
622
|
? currentSession.expires_at <= Date.now() / 1000
|
|
650
623
|
: false;
|
|
651
|
-
this._debug('#
|
|
624
|
+
this._debug('#getSession()', `session has${hasExpired ? '' : ' not'} expired`, 'expires_at', currentSession.expires_at);
|
|
652
625
|
if (!hasExpired) {
|
|
653
626
|
return { data: { session: currentSession }, error: null };
|
|
654
627
|
}
|
|
@@ -659,7 +632,7 @@ class GoTrueClient {
|
|
|
659
632
|
return { data: { session }, error: null };
|
|
660
633
|
}
|
|
661
634
|
finally {
|
|
662
|
-
this._debug('#
|
|
635
|
+
this._debug('#getSession()', 'end');
|
|
663
636
|
}
|
|
664
637
|
}
|
|
665
638
|
/**
|
|
@@ -667,22 +640,20 @@ class GoTrueClient {
|
|
|
667
640
|
* @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.
|
|
668
641
|
*/
|
|
669
642
|
async getUser(jwt) {
|
|
643
|
+
var _a, _b;
|
|
670
644
|
try {
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
if (
|
|
674
|
-
|
|
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;
|
|
645
|
+
if (!jwt) {
|
|
646
|
+
const { data, error } = await this.getSession();
|
|
647
|
+
if (error) {
|
|
648
|
+
throw error;
|
|
680
649
|
}
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
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,
|
|
686
657
|
});
|
|
687
658
|
}
|
|
688
659
|
catch (error) {
|
|
@@ -697,29 +668,27 @@ class GoTrueClient {
|
|
|
697
668
|
*/
|
|
698
669
|
async updateUser(attributes, options = {}) {
|
|
699
670
|
try {
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
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 };
|
|
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,
|
|
722
685
|
});
|
|
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 };
|
|
723
692
|
}
|
|
724
693
|
catch (error) {
|
|
725
694
|
if ((0, errors_1.isAuthError)(error)) {
|
|
@@ -795,28 +764,26 @@ class GoTrueClient {
|
|
|
795
764
|
* @param currentSession The current session. If passed in, it must contain a refresh token.
|
|
796
765
|
*/
|
|
797
766
|
async refreshSession(currentSession) {
|
|
767
|
+
var _a;
|
|
798
768
|
try {
|
|
799
|
-
|
|
800
|
-
|
|
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);
|
|
769
|
+
if (!currentSession) {
|
|
770
|
+
const { data, error } = await this.getSession();
|
|
812
771
|
if (error) {
|
|
813
|
-
|
|
814
|
-
}
|
|
815
|
-
if (!session) {
|
|
816
|
-
return { data: { user: null, session: null }, error: null };
|
|
772
|
+
throw error;
|
|
817
773
|
}
|
|
818
|
-
|
|
819
|
-
}
|
|
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 };
|
|
820
787
|
}
|
|
821
788
|
catch (error) {
|
|
822
789
|
if ((0, errors_1.isAuthError)(error)) {
|
|
@@ -930,30 +897,28 @@ class GoTrueClient {
|
|
|
930
897
|
* If using others scope, no `SIGNED_OUT` event is fired!
|
|
931
898
|
*/
|
|
932
899
|
async signOut({ scope } = { scope: 'global' }) {
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
return { error };
|
|
947
|
-
}
|
|
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 };
|
|
948
913
|
}
|
|
949
914
|
}
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
}
|
|
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 };
|
|
957
922
|
}
|
|
958
923
|
/**
|
|
959
924
|
* Receive a notification every time an auth event happens.
|
|
@@ -975,21 +940,19 @@ class GoTrueClient {
|
|
|
975
940
|
return { data: { subscription } };
|
|
976
941
|
}
|
|
977
942
|
async _emitInitialSession(id) {
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
}
|
|
992
|
-
});
|
|
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
|
+
}
|
|
993
956
|
}
|
|
994
957
|
/**
|
|
995
958
|
* Sends a password reset request to an email address.
|
|
@@ -1329,19 +1292,17 @@ class GoTrueClient {
|
|
|
1329
1292
|
try {
|
|
1330
1293
|
const now = Date.now();
|
|
1331
1294
|
try {
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
}
|
|
1344
|
-
});
|
|
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
|
+
}
|
|
1345
1306
|
}
|
|
1346
1307
|
catch (e) {
|
|
1347
1308
|
console.error('Auto refresh tick failed with error. This is likely a transient error.', e);
|
|
@@ -1433,17 +1394,15 @@ class GoTrueClient {
|
|
|
1433
1394
|
return `${this.url}/authorize?${urlParams.join('&')}`;
|
|
1434
1395
|
}
|
|
1435
1396
|
async _unenroll(params) {
|
|
1397
|
+
var _a;
|
|
1436
1398
|
try {
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
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
|
-
});
|
|
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,
|
|
1447
1406
|
});
|
|
1448
1407
|
}
|
|
1449
1408
|
catch (error) {
|
|
@@ -1457,30 +1416,28 @@ class GoTrueClient {
|
|
|
1457
1416
|
* {@see GoTrueMFAApi#enroll}
|
|
1458
1417
|
*/
|
|
1459
1418
|
async _enroll(params) {
|
|
1419
|
+
var _a, _b;
|
|
1460
1420
|
try {
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
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 };
|
|
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,
|
|
1483
1433
|
});
|
|
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 };
|
|
1484
1441
|
}
|
|
1485
1442
|
catch (error) {
|
|
1486
1443
|
if ((0, errors_1.isAuthError)(error)) {
|
|
@@ -1493,25 +1450,23 @@ class GoTrueClient {
|
|
|
1493
1450
|
* {@see GoTrueMFAApi#verify}
|
|
1494
1451
|
*/
|
|
1495
1452
|
async _verify(params) {
|
|
1453
|
+
var _a;
|
|
1496
1454
|
try {
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
}
|
|
1503
|
-
|
|
1504
|
-
|
|
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 };
|
|
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,
|
|
1514
1463
|
});
|
|
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 };
|
|
1515
1470
|
}
|
|
1516
1471
|
catch (error) {
|
|
1517
1472
|
if ((0, errors_1.isAuthError)(error)) {
|
|
@@ -1524,17 +1479,15 @@ class GoTrueClient {
|
|
|
1524
1479
|
* {@see GoTrueMFAApi#challenge}
|
|
1525
1480
|
*/
|
|
1526
1481
|
async _challenge(params) {
|
|
1482
|
+
var _a;
|
|
1527
1483
|
try {
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
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
|
-
});
|
|
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,
|
|
1538
1491
|
});
|
|
1539
1492
|
}
|
|
1540
1493
|
catch (error) {
|
|
@@ -1582,31 +1535,29 @@ class GoTrueClient {
|
|
|
1582
1535
|
* {@see GoTrueMFAApi#getAuthenticatorAssuranceLevel}
|
|
1583
1536
|
*/
|
|
1584
1537
|
async _getAuthenticatorAssuranceLevel() {
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
return { data: { currentLevel, nextLevel, currentAuthenticationMethods }, error: null };
|
|
1609
|
-
});
|
|
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 };
|
|
1610
1561
|
}
|
|
1611
1562
|
}
|
|
1612
1563
|
exports.default = GoTrueClient;
|