@bagelink/auth 1.20.5 → 1.20.11
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/api.d.ts +3 -0
- package/dist/index.cjs +40 -0
- package/dist/index.mjs +40 -0
- package/dist/types.d.ts +1 -1
- package/dist/useAuth.d.ts +60 -0
- package/package.json +1 -1
- package/src/api.mfa.test.ts +21 -0
- package/src/api.ts +11 -0
- package/src/types.ts +2 -0
- package/src/useAuth.ts +33 -0
package/dist/api.d.ts
CHANGED
|
@@ -4,6 +4,9 @@ export declare class AuthApi {
|
|
|
4
4
|
private api;
|
|
5
5
|
private currentTenantId;
|
|
6
6
|
private externalAxiosInstances;
|
|
7
|
+
private sessionToken;
|
|
8
|
+
/** Keep replacement sessions in memory; the server owns the HttpOnly cookie. */
|
|
9
|
+
setSessionToken(token: string | null): void;
|
|
7
10
|
constructor(baseURL?: string);
|
|
8
11
|
/**
|
|
9
12
|
* Register an external axios instance so that tenant headers
|
package/dist/index.cjs
CHANGED
|
@@ -65,15 +65,25 @@ class AuthApi {
|
|
|
65
65
|
__publicField(this, "api");
|
|
66
66
|
__publicField(this, "currentTenantId", null);
|
|
67
67
|
__publicField(this, "externalAxiosInstances", /* @__PURE__ */ new Set());
|
|
68
|
+
__publicField(this, "sessionToken", null);
|
|
68
69
|
this.api = createAxiosInstance(baseURL);
|
|
69
70
|
this.setupInterceptors();
|
|
70
71
|
}
|
|
72
|
+
/** Keep replacement sessions in memory; the server owns the HttpOnly cookie. */
|
|
73
|
+
setSessionToken(token) {
|
|
74
|
+
this.sessionToken = token;
|
|
75
|
+
for (const instance of [this.api, ...this.externalAxiosInstances]) {
|
|
76
|
+
if (token) instance.defaults.headers.common.Authorization = `Bearer ${token}`;
|
|
77
|
+
else delete instance.defaults.headers.common.Authorization;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
71
80
|
/**
|
|
72
81
|
* Register an external axios instance so that tenant headers
|
|
73
82
|
* are automatically applied to it when setTenantId() is called.
|
|
74
83
|
*/
|
|
75
84
|
registerAxios(instance) {
|
|
76
85
|
this.externalAxiosInstances.add(instance);
|
|
86
|
+
if (this.sessionToken) instance.defaults.headers.common.Authorization = `Bearer ${this.sessionToken}`;
|
|
77
87
|
if (this.currentTenantId !== null) {
|
|
78
88
|
instance.defaults.headers.common["X-Tenant-ID"] = this.currentTenantId;
|
|
79
89
|
}
|
|
@@ -1027,6 +1037,7 @@ let redirectConfig = null;
|
|
|
1027
1037
|
let autoRedirectRouter = null;
|
|
1028
1038
|
let cachedAuthGuard = null;
|
|
1029
1039
|
let tenancyEnabled = false;
|
|
1040
|
+
const pendingMfa = vue.ref(null);
|
|
1030
1041
|
const accountInfo = vue.ref(null);
|
|
1031
1042
|
const tenants = vue.ref([]);
|
|
1032
1043
|
const currentTenant = vue.ref(null);
|
|
@@ -1180,6 +1191,8 @@ function useAuth() {
|
|
|
1180
1191
|
},
|
|
1181
1192
|
loginWithSSO: async (provider, params) => {
|
|
1182
1193
|
const { data } = await api.ssoCallback(provider, params);
|
|
1194
|
+
if (captureMfa(data)) return data;
|
|
1195
|
+
if (!data.success) throw new Error(data.message || "SSO login failed");
|
|
1183
1196
|
if (data.success === true && data.requires_verification !== true) {
|
|
1184
1197
|
await checkAuth();
|
|
1185
1198
|
}
|
|
@@ -1224,14 +1237,36 @@ function useAuth() {
|
|
|
1224
1237
|
const logoutPromise = api.logout();
|
|
1225
1238
|
await logoutPromise.catch(() => {
|
|
1226
1239
|
});
|
|
1240
|
+
api.setSessionToken(null);
|
|
1241
|
+
pendingMfa.value = null;
|
|
1227
1242
|
accountInfo.value = null;
|
|
1228
1243
|
tenants.value = [];
|
|
1229
1244
|
currentTenant.value = null;
|
|
1230
1245
|
api.setTenantId(null);
|
|
1231
1246
|
emitter.emit(AuthState.LOGOUT);
|
|
1232
1247
|
}
|
|
1248
|
+
function captureMfa(response) {
|
|
1249
|
+
const metadata = response.metadata;
|
|
1250
|
+
const token = metadata && ("challenge_token" in metadata ? metadata.challenge_token : "token" in metadata ? metadata.token : void 0);
|
|
1251
|
+
if (response.requires_verification && typeof token === "string") {
|
|
1252
|
+
pendingMfa.value = { ...response, metadata: { ...metadata, challenge_token: token } };
|
|
1253
|
+
return true;
|
|
1254
|
+
}
|
|
1255
|
+
pendingMfa.value = null;
|
|
1256
|
+
return false;
|
|
1257
|
+
}
|
|
1258
|
+
async function completeMfa(response) {
|
|
1259
|
+
if (!response.success || response.requires_verification) throw new Error("MFA verification failed");
|
|
1260
|
+
if (!response.session_token) throw new Error("MFA response missing replacement session");
|
|
1261
|
+
api.setSessionToken(response.session_token);
|
|
1262
|
+
pendingMfa.value = null;
|
|
1263
|
+
if (!await checkAuth()) throw new Error("Session could not be established");
|
|
1264
|
+
emitter.emit(AuthState.LOGIN);
|
|
1265
|
+
}
|
|
1233
1266
|
async function login(credentials) {
|
|
1234
1267
|
const { data } = await api.login(credentials);
|
|
1268
|
+
if (captureMfa(data)) return data;
|
|
1269
|
+
if (!data.success) throw new Error(data.message || "Login failed");
|
|
1235
1270
|
if (data.success === true && data.requires_verification !== true) {
|
|
1236
1271
|
await checkAuth();
|
|
1237
1272
|
}
|
|
@@ -1275,6 +1310,7 @@ function useAuth() {
|
|
|
1275
1310
|
async function loginWithToken(token) {
|
|
1276
1311
|
try {
|
|
1277
1312
|
const { data: response } = await api.redeemLoginToken({ token });
|
|
1313
|
+
if (captureMfa(response)) return false;
|
|
1278
1314
|
if (response.success === true && response.requires_verification !== true) {
|
|
1279
1315
|
await checkAuth();
|
|
1280
1316
|
}
|
|
@@ -1470,6 +1506,8 @@ function useAuth() {
|
|
|
1470
1506
|
}
|
|
1471
1507
|
async function loginWithSSO(provider, params) {
|
|
1472
1508
|
const { data } = await api.ssoCallback(provider, params);
|
|
1509
|
+
if (captureMfa(data)) return data;
|
|
1510
|
+
if (!data.success) throw new Error(data.message || "SSO login failed");
|
|
1473
1511
|
if (data.success === true && data.requires_verification !== true) {
|
|
1474
1512
|
await checkAuth();
|
|
1475
1513
|
}
|
|
@@ -1487,6 +1525,8 @@ function useAuth() {
|
|
|
1487
1525
|
return {
|
|
1488
1526
|
// Primary State (use this!)
|
|
1489
1527
|
user,
|
|
1528
|
+
pendingMfa,
|
|
1529
|
+
completeMfa,
|
|
1490
1530
|
// Full account info (for advanced use cases)
|
|
1491
1531
|
accountInfo,
|
|
1492
1532
|
// SSO Providers (ready to use!)
|
package/dist/index.mjs
CHANGED
|
@@ -63,15 +63,25 @@ class AuthApi {
|
|
|
63
63
|
__publicField(this, "api");
|
|
64
64
|
__publicField(this, "currentTenantId", null);
|
|
65
65
|
__publicField(this, "externalAxiosInstances", /* @__PURE__ */ new Set());
|
|
66
|
+
__publicField(this, "sessionToken", null);
|
|
66
67
|
this.api = createAxiosInstance(baseURL);
|
|
67
68
|
this.setupInterceptors();
|
|
68
69
|
}
|
|
70
|
+
/** Keep replacement sessions in memory; the server owns the HttpOnly cookie. */
|
|
71
|
+
setSessionToken(token) {
|
|
72
|
+
this.sessionToken = token;
|
|
73
|
+
for (const instance of [this.api, ...this.externalAxiosInstances]) {
|
|
74
|
+
if (token) instance.defaults.headers.common.Authorization = `Bearer ${token}`;
|
|
75
|
+
else delete instance.defaults.headers.common.Authorization;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
69
78
|
/**
|
|
70
79
|
* Register an external axios instance so that tenant headers
|
|
71
80
|
* are automatically applied to it when setTenantId() is called.
|
|
72
81
|
*/
|
|
73
82
|
registerAxios(instance) {
|
|
74
83
|
this.externalAxiosInstances.add(instance);
|
|
84
|
+
if (this.sessionToken) instance.defaults.headers.common.Authorization = `Bearer ${this.sessionToken}`;
|
|
75
85
|
if (this.currentTenantId !== null) {
|
|
76
86
|
instance.defaults.headers.common["X-Tenant-ID"] = this.currentTenantId;
|
|
77
87
|
}
|
|
@@ -1025,6 +1035,7 @@ let redirectConfig = null;
|
|
|
1025
1035
|
let autoRedirectRouter = null;
|
|
1026
1036
|
let cachedAuthGuard = null;
|
|
1027
1037
|
let tenancyEnabled = false;
|
|
1038
|
+
const pendingMfa = ref(null);
|
|
1028
1039
|
const accountInfo = ref(null);
|
|
1029
1040
|
const tenants = ref([]);
|
|
1030
1041
|
const currentTenant = ref(null);
|
|
@@ -1178,6 +1189,8 @@ function useAuth() {
|
|
|
1178
1189
|
},
|
|
1179
1190
|
loginWithSSO: async (provider, params) => {
|
|
1180
1191
|
const { data } = await api.ssoCallback(provider, params);
|
|
1192
|
+
if (captureMfa(data)) return data;
|
|
1193
|
+
if (!data.success) throw new Error(data.message || "SSO login failed");
|
|
1181
1194
|
if (data.success === true && data.requires_verification !== true) {
|
|
1182
1195
|
await checkAuth();
|
|
1183
1196
|
}
|
|
@@ -1222,14 +1235,36 @@ function useAuth() {
|
|
|
1222
1235
|
const logoutPromise = api.logout();
|
|
1223
1236
|
await logoutPromise.catch(() => {
|
|
1224
1237
|
});
|
|
1238
|
+
api.setSessionToken(null);
|
|
1239
|
+
pendingMfa.value = null;
|
|
1225
1240
|
accountInfo.value = null;
|
|
1226
1241
|
tenants.value = [];
|
|
1227
1242
|
currentTenant.value = null;
|
|
1228
1243
|
api.setTenantId(null);
|
|
1229
1244
|
emitter.emit(AuthState.LOGOUT);
|
|
1230
1245
|
}
|
|
1246
|
+
function captureMfa(response) {
|
|
1247
|
+
const metadata = response.metadata;
|
|
1248
|
+
const token = metadata && ("challenge_token" in metadata ? metadata.challenge_token : "token" in metadata ? metadata.token : void 0);
|
|
1249
|
+
if (response.requires_verification && typeof token === "string") {
|
|
1250
|
+
pendingMfa.value = { ...response, metadata: { ...metadata, challenge_token: token } };
|
|
1251
|
+
return true;
|
|
1252
|
+
}
|
|
1253
|
+
pendingMfa.value = null;
|
|
1254
|
+
return false;
|
|
1255
|
+
}
|
|
1256
|
+
async function completeMfa(response) {
|
|
1257
|
+
if (!response.success || response.requires_verification) throw new Error("MFA verification failed");
|
|
1258
|
+
if (!response.session_token) throw new Error("MFA response missing replacement session");
|
|
1259
|
+
api.setSessionToken(response.session_token);
|
|
1260
|
+
pendingMfa.value = null;
|
|
1261
|
+
if (!await checkAuth()) throw new Error("Session could not be established");
|
|
1262
|
+
emitter.emit(AuthState.LOGIN);
|
|
1263
|
+
}
|
|
1231
1264
|
async function login(credentials) {
|
|
1232
1265
|
const { data } = await api.login(credentials);
|
|
1266
|
+
if (captureMfa(data)) return data;
|
|
1267
|
+
if (!data.success) throw new Error(data.message || "Login failed");
|
|
1233
1268
|
if (data.success === true && data.requires_verification !== true) {
|
|
1234
1269
|
await checkAuth();
|
|
1235
1270
|
}
|
|
@@ -1273,6 +1308,7 @@ function useAuth() {
|
|
|
1273
1308
|
async function loginWithToken(token) {
|
|
1274
1309
|
try {
|
|
1275
1310
|
const { data: response } = await api.redeemLoginToken({ token });
|
|
1311
|
+
if (captureMfa(response)) return false;
|
|
1276
1312
|
if (response.success === true && response.requires_verification !== true) {
|
|
1277
1313
|
await checkAuth();
|
|
1278
1314
|
}
|
|
@@ -1468,6 +1504,8 @@ function useAuth() {
|
|
|
1468
1504
|
}
|
|
1469
1505
|
async function loginWithSSO(provider, params) {
|
|
1470
1506
|
const { data } = await api.ssoCallback(provider, params);
|
|
1507
|
+
if (captureMfa(data)) return data;
|
|
1508
|
+
if (!data.success) throw new Error(data.message || "SSO login failed");
|
|
1471
1509
|
if (data.success === true && data.requires_verification !== true) {
|
|
1472
1510
|
await checkAuth();
|
|
1473
1511
|
}
|
|
@@ -1485,6 +1523,8 @@ function useAuth() {
|
|
|
1485
1523
|
return {
|
|
1486
1524
|
// Primary State (use this!)
|
|
1487
1525
|
user,
|
|
1526
|
+
pendingMfa,
|
|
1527
|
+
completeMfa,
|
|
1488
1528
|
// Full account info (for advanced use cases)
|
|
1489
1529
|
accountInfo,
|
|
1490
1530
|
// SSO Providers (ready to use!)
|
package/dist/types.d.ts
CHANGED
|
@@ -104,7 +104,7 @@ export type GetInvitationResponse = AxiosResponse<{
|
|
|
104
104
|
last_name?: string | null;
|
|
105
105
|
}>;
|
|
106
106
|
export type AcceptInvitationResponse = AxiosResponse<MessageResponse>;
|
|
107
|
-
export type AuthenticationMethodType = 'password' | 'email_token' | 'email' | 'sso' | 'otp' | 'sms' | 'login_token';
|
|
107
|
+
export type AuthenticationMethodType = 'password' | 'email_token' | 'email' | 'sso' | 'otp' | 'sms' | 'login_token' | 'totp' | 'recovery';
|
|
108
108
|
export type SSOProvider = 'google' | 'microsoft' | 'github' | 'okta' | 'apple' | 'facebook' | 'custom';
|
|
109
109
|
export interface AuthenticationAccount {
|
|
110
110
|
created_at?: string;
|
package/dist/useAuth.d.ts
CHANGED
|
@@ -53,6 +53,66 @@ export declare function getRedirectConfig(): NormalizedRedirectConfig;
|
|
|
53
53
|
export declare function createAuth(params: InitParams): AuthInstance;
|
|
54
54
|
export declare function useAuth(): {
|
|
55
55
|
user: import('vue').ComputedRef<User | null>;
|
|
56
|
+
pendingMfa: import('vue').Ref<{
|
|
57
|
+
success: boolean;
|
|
58
|
+
account_id?: string | undefined;
|
|
59
|
+
session_token?: string | undefined;
|
|
60
|
+
requires_verification?: boolean | undefined;
|
|
61
|
+
verification_method?: import('./types').AuthenticationMethodType | undefined;
|
|
62
|
+
message?: string | undefined;
|
|
63
|
+
metadata: {
|
|
64
|
+
nonce: string;
|
|
65
|
+
verification_hash: string;
|
|
66
|
+
timestamp: number;
|
|
67
|
+
expires_in_minutes: number;
|
|
68
|
+
autofill: {
|
|
69
|
+
[key: string]: any;
|
|
70
|
+
};
|
|
71
|
+
challenge_token: string;
|
|
72
|
+
} | {
|
|
73
|
+
provider: string;
|
|
74
|
+
sso_user_info: {
|
|
75
|
+
[key: string]: any;
|
|
76
|
+
};
|
|
77
|
+
can_create_account?: boolean | undefined;
|
|
78
|
+
challenge_token: string;
|
|
79
|
+
} | {
|
|
80
|
+
[x: string]: any;
|
|
81
|
+
challenge_token: string;
|
|
82
|
+
};
|
|
83
|
+
} | null, (AuthenticationResponse & {
|
|
84
|
+
metadata: {
|
|
85
|
+
challenge_token: string;
|
|
86
|
+
};
|
|
87
|
+
}) | {
|
|
88
|
+
success: boolean;
|
|
89
|
+
account_id?: string | undefined;
|
|
90
|
+
session_token?: string | undefined;
|
|
91
|
+
requires_verification?: boolean | undefined;
|
|
92
|
+
verification_method?: import('./types').AuthenticationMethodType | undefined;
|
|
93
|
+
message?: string | undefined;
|
|
94
|
+
metadata: {
|
|
95
|
+
nonce: string;
|
|
96
|
+
verification_hash: string;
|
|
97
|
+
timestamp: number;
|
|
98
|
+
expires_in_minutes: number;
|
|
99
|
+
autofill: {
|
|
100
|
+
[key: string]: any;
|
|
101
|
+
};
|
|
102
|
+
challenge_token: string;
|
|
103
|
+
} | {
|
|
104
|
+
provider: string;
|
|
105
|
+
sso_user_info: {
|
|
106
|
+
[key: string]: any;
|
|
107
|
+
};
|
|
108
|
+
can_create_account?: boolean | undefined;
|
|
109
|
+
challenge_token: string;
|
|
110
|
+
} | {
|
|
111
|
+
[x: string]: any;
|
|
112
|
+
challenge_token: string;
|
|
113
|
+
};
|
|
114
|
+
} | null>;
|
|
115
|
+
completeMfa: (response: AuthenticationResponse) => Promise<void>;
|
|
56
116
|
accountInfo: import('vue').Ref<{
|
|
57
117
|
id: string;
|
|
58
118
|
account_type: string;
|
package/package.json
CHANGED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
2
|
+
import axios from 'axios'
|
|
3
|
+
import { AuthApi } from './api'
|
|
4
|
+
|
|
5
|
+
afterEach(() => vi.unstubAllGlobals())
|
|
6
|
+
describe('MFA replacement session', () => {
|
|
7
|
+
it('updates existing and newly registered transports and clears on logout', () => {
|
|
8
|
+
vi.stubGlobal('window', { location: { search: '' } })
|
|
9
|
+
const api = new AuthApi('http://localhost')
|
|
10
|
+
const first = axios.create()
|
|
11
|
+
api.registerAxios(first)
|
|
12
|
+
api.setSessionToken('replacement-test-session')
|
|
13
|
+
expect(first.defaults.headers.common.Authorization).toBe('Bearer replacement-test-session')
|
|
14
|
+
const second = axios.create()
|
|
15
|
+
api.registerAxios(second)
|
|
16
|
+
expect(second.defaults.headers.common.Authorization).toBe('Bearer replacement-test-session')
|
|
17
|
+
api.setSessionToken(null)
|
|
18
|
+
expect(first.defaults.headers.common.Authorization).toBeUndefined()
|
|
19
|
+
expect(second.defaults.headers.common.Authorization).toBeUndefined()
|
|
20
|
+
})
|
|
21
|
+
})
|
package/src/api.ts
CHANGED
|
@@ -80,6 +80,16 @@ export class AuthApi {
|
|
|
80
80
|
private api: AxiosInstance
|
|
81
81
|
private currentTenantId: string | null = null
|
|
82
82
|
private externalAxiosInstances: Set<AxiosInstance> = new Set()
|
|
83
|
+
private sessionToken: string | null = null
|
|
84
|
+
|
|
85
|
+
/** Keep replacement sessions in memory; the server owns the HttpOnly cookie. */
|
|
86
|
+
setSessionToken(token: string | null) {
|
|
87
|
+
this.sessionToken = token
|
|
88
|
+
for (const instance of [this.api, ...this.externalAxiosInstances]) {
|
|
89
|
+
if (token) instance.defaults.headers.common.Authorization = `Bearer ${token}`
|
|
90
|
+
else delete instance.defaults.headers.common.Authorization
|
|
91
|
+
}
|
|
92
|
+
}
|
|
83
93
|
|
|
84
94
|
constructor(baseURL: string = '') {
|
|
85
95
|
this.api = createAxiosInstance(baseURL)
|
|
@@ -92,6 +102,7 @@ export class AuthApi {
|
|
|
92
102
|
*/
|
|
93
103
|
registerAxios(instance: AxiosInstance) {
|
|
94
104
|
this.externalAxiosInstances.add(instance)
|
|
105
|
+
if (this.sessionToken) instance.defaults.headers.common.Authorization = `Bearer ${this.sessionToken}`
|
|
95
106
|
// Apply current tenant immediately if already set
|
|
96
107
|
if (this.currentTenantId !== null) {
|
|
97
108
|
instance.defaults.headers.common['X-Tenant-ID'] = this.currentTenantId
|
package/src/types.ts
CHANGED
package/src/useAuth.ts
CHANGED
|
@@ -42,6 +42,8 @@ let redirectConfig: NormalizedRedirectConfig | null = null
|
|
|
42
42
|
let autoRedirectRouter: any = null // Router instance for auto-redirect
|
|
43
43
|
let cachedAuthGuard: any = null // Cached router guard
|
|
44
44
|
let tenancyEnabled = false
|
|
45
|
+
// Challenge credentials live only in memory; never persist them in browser storage.
|
|
46
|
+
const pendingMfa = ref<(AuthenticationResponse & { metadata: { challenge_token: string } }) | null>(null)
|
|
45
47
|
const accountInfo = ref<AccountInfo | null>(null)
|
|
46
48
|
const tenants = ref<TenantInfo[]>([])
|
|
47
49
|
const currentTenant = ref<TenantInfo | null>(null)
|
|
@@ -284,6 +286,8 @@ export function useAuth() {
|
|
|
284
286
|
},
|
|
285
287
|
loginWithSSO: async (provider: SSOProvider, params: SSOCallbackRequest) => {
|
|
286
288
|
const { data } = await api.ssoCallback(provider, params)
|
|
289
|
+
if (captureMfa(data)) return data
|
|
290
|
+
if (!data.success) throw new Error(data.message || 'SSO login failed')
|
|
287
291
|
if (data.success === true && data.requires_verification !== true) {
|
|
288
292
|
await checkAuth()
|
|
289
293
|
}
|
|
@@ -340,6 +344,8 @@ export function useAuth() {
|
|
|
340
344
|
// Ignore logout errors
|
|
341
345
|
})
|
|
342
346
|
// Clear local state regardless of API result
|
|
347
|
+
api.setSessionToken(null)
|
|
348
|
+
pendingMfa.value = null
|
|
343
349
|
accountInfo.value = null
|
|
344
350
|
tenants.value = []
|
|
345
351
|
currentTenant.value = null
|
|
@@ -348,8 +354,30 @@ export function useAuth() {
|
|
|
348
354
|
emitter.emit(AuthState.LOGOUT)
|
|
349
355
|
}
|
|
350
356
|
|
|
357
|
+
function captureMfa(response: AuthenticationResponse): boolean {
|
|
358
|
+
const metadata = response.metadata
|
|
359
|
+
const token: unknown = metadata && ('challenge_token' in metadata ? metadata.challenge_token : 'token' in metadata ? metadata.token : undefined)
|
|
360
|
+
if (response.requires_verification && typeof token === 'string') {
|
|
361
|
+
pendingMfa.value = { ...response, metadata: { ...metadata, challenge_token: token } }
|
|
362
|
+
return true
|
|
363
|
+
}
|
|
364
|
+
pendingMfa.value = null
|
|
365
|
+
return false
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
async function completeMfa(response: AuthenticationResponse) {
|
|
369
|
+
if (!response.success || response.requires_verification) throw new Error('MFA verification failed')
|
|
370
|
+
if (!response.session_token) throw new Error('MFA response missing replacement session')
|
|
371
|
+
api.setSessionToken(response.session_token)
|
|
372
|
+
pendingMfa.value = null
|
|
373
|
+
if (!(await checkAuth())) throw new Error('Session could not be established')
|
|
374
|
+
emitter.emit(AuthState.LOGIN)
|
|
375
|
+
}
|
|
376
|
+
|
|
351
377
|
async function login(credentials: { email: string, password: string }) {
|
|
352
378
|
const { data } = await api.login(credentials)
|
|
379
|
+
if (captureMfa(data)) return data
|
|
380
|
+
if (!data.success) throw new Error(data.message || 'Login failed')
|
|
353
381
|
|
|
354
382
|
// If successful and not requiring verification, fetch user data and tenants
|
|
355
383
|
if (data.success === true && data.requires_verification !== true) {
|
|
@@ -433,6 +461,7 @@ export function useAuth() {
|
|
|
433
461
|
async function loginWithToken(token: string): Promise<boolean> {
|
|
434
462
|
try {
|
|
435
463
|
const { data: response } = await api.redeemLoginToken({ token })
|
|
464
|
+
if (captureMfa(response)) return false
|
|
436
465
|
|
|
437
466
|
if (response.success === true && response.requires_verification !== true) {
|
|
438
467
|
await checkAuth()
|
|
@@ -702,6 +731,8 @@ export function useAuth() {
|
|
|
702
731
|
*/
|
|
703
732
|
async function loginWithSSO(provider: SSOProvider, params: SSOCallbackRequest) {
|
|
704
733
|
const { data } = await api.ssoCallback(provider, params)
|
|
734
|
+
if (captureMfa(data)) return data
|
|
735
|
+
if (!data.success) throw new Error(data.message || 'SSO login failed')
|
|
705
736
|
|
|
706
737
|
if (data.success === true && data.requires_verification !== true) {
|
|
707
738
|
await checkAuth()
|
|
@@ -730,6 +761,8 @@ export function useAuth() {
|
|
|
730
761
|
return {
|
|
731
762
|
// Primary State (use this!)
|
|
732
763
|
user,
|
|
764
|
+
pendingMfa,
|
|
765
|
+
completeMfa,
|
|
733
766
|
|
|
734
767
|
// Full account info (for advanced use cases)
|
|
735
768
|
accountInfo,
|