@alore/auth-react-sdk 1.1.0-alpha.1 → 1.1.0-alpha.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/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/types.d.ts +0 -2
- package/dist/utils/AloreAuth.d.ts +13 -25
- package/dist/utils/AloreAuth.js +138 -94
- package/dist/utils/AuthError.d.ts +2 -0
- package/dist/utils/AuthError.js +2 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/types.d.ts
CHANGED
|
@@ -20,7 +20,6 @@ export type KeyDerivationFunction = 'argon2d' | 'pbkdf2';
|
|
|
20
20
|
* @property logoUrl - Optional string for logo URL.
|
|
21
21
|
* @property socialProviders - Optional social login providers.
|
|
22
22
|
* @property enableWalletCreation - Optional boolean to enable wallet creation.
|
|
23
|
-
* @property allowedEmailDomains - Optional allowed email domain(s). If provided, authentication will only allow emails from these domains. Accepts a single string (e.g., 'bealore.com' or '@bealore.com') or an array of strings.
|
|
24
23
|
*/
|
|
25
24
|
export interface AuthProviderConfig {
|
|
26
25
|
rpDomain?: string;
|
|
@@ -38,7 +37,6 @@ export interface AuthProviderConfig {
|
|
|
38
37
|
socialProviders?: SocialProvider[];
|
|
39
38
|
enableWalletCreation?: boolean;
|
|
40
39
|
firebaseCompatible?: boolean;
|
|
41
|
-
allowedEmailDomains?: string | string[];
|
|
42
40
|
}
|
|
43
41
|
export interface AuthMachineContext {
|
|
44
42
|
authProviderConfigs?: AuthProviderConfig;
|
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { AuthMachineContext, AuthProviderConfig, FetchWithProgressiveBackoffConfig, ForgeData } from '../types';
|
|
1
|
+
import type { AuthMachineContext, AuthProviderConfig, FetchWithProgressiveBackoffConfig, ForgeData } from '../types';
|
|
2
2
|
export declare class AloreAuth {
|
|
3
3
|
protected readonly aloreBaseUrl: string;
|
|
4
4
|
protected readonly emailTemplate: string;
|
|
5
5
|
protected readonly clientId: string;
|
|
6
6
|
constructor(clientId: string, aloreBaseUrl?: string, config?: AuthProviderConfig);
|
|
7
7
|
services: {
|
|
8
|
-
isEmailAllowed: (context: AuthMachineContext, email?: string | null) => boolean;
|
|
9
8
|
healthCheck: () => Promise<{
|
|
10
9
|
data: boolean;
|
|
11
10
|
}>;
|
|
@@ -16,17 +15,13 @@ export declare class AloreAuth {
|
|
|
16
15
|
};
|
|
17
16
|
}) => Promise<{
|
|
18
17
|
salt: string;
|
|
19
|
-
|
|
20
|
-
} | {
|
|
21
|
-
error: string;
|
|
22
|
-
salt?: undefined;
|
|
23
|
-
}>;
|
|
18
|
+
} | undefined>;
|
|
24
19
|
verifyEmail: (context: AuthMachineContext, event: {
|
|
25
20
|
type: 'VERIFY_EMAIL';
|
|
26
21
|
payload: {
|
|
27
22
|
secureCode: string;
|
|
28
23
|
};
|
|
29
|
-
}) => Promise<{}>;
|
|
24
|
+
}) => Promise<{} | undefined>;
|
|
30
25
|
completeRegistration: (context: AuthMachineContext, event: {
|
|
31
26
|
type: 'COMPLETE_REGISTRATION';
|
|
32
27
|
payload: {
|
|
@@ -39,14 +34,11 @@ export declare class AloreAuth {
|
|
|
39
34
|
confirmPassword: (context: AuthMachineContext, event: {
|
|
40
35
|
type: 'CONFIRM_PASSWORD';
|
|
41
36
|
payload: {
|
|
42
|
-
|
|
37
|
+
salt: string;
|
|
43
38
|
passwordHash: string;
|
|
39
|
+
token: string;
|
|
44
40
|
};
|
|
45
|
-
}) => Promise<{
|
|
46
|
-
error?: undefined;
|
|
47
|
-
} | {
|
|
48
|
-
error: string;
|
|
49
|
-
}>;
|
|
41
|
+
}) => Promise<{}>;
|
|
50
42
|
sendConfirmationEmail: (context: AuthMachineContext, event: {
|
|
51
43
|
type: 'RESEND_CODE';
|
|
52
44
|
payload: {
|
|
@@ -68,22 +60,14 @@ export declare class AloreAuth {
|
|
|
68
60
|
}) => Promise<{
|
|
69
61
|
salt: any;
|
|
70
62
|
sessionId: any;
|
|
71
|
-
|
|
72
|
-
} | {
|
|
73
|
-
error: string;
|
|
74
|
-
salt?: undefined;
|
|
75
|
-
sessionId?: undefined;
|
|
76
|
-
}>;
|
|
63
|
+
} | undefined>;
|
|
77
64
|
sendCode: (context: AuthMachineContext, event: {
|
|
78
65
|
type: 'SEND_CODE';
|
|
79
66
|
payload: {
|
|
80
67
|
email: string;
|
|
68
|
+
locale?: string;
|
|
81
69
|
};
|
|
82
|
-
}) => Promise<{
|
|
83
|
-
error: string;
|
|
84
|
-
} | {
|
|
85
|
-
error?: undefined;
|
|
86
|
-
}>;
|
|
70
|
+
}) => Promise<{}>;
|
|
87
71
|
verifyLogin: (context: AuthMachineContext, event: {
|
|
88
72
|
type: 'VERIFY_LOGIN';
|
|
89
73
|
payload: {
|
|
@@ -264,6 +248,10 @@ export declare class AloreAuth {
|
|
|
264
248
|
}) => Promise<any>;
|
|
265
249
|
};
|
|
266
250
|
private delay;
|
|
251
|
+
private isLocalUrl;
|
|
267
252
|
fetchWithProgressiveBackoff(url: RequestInfo | URL, options?: RequestInit, config?: FetchWithProgressiveBackoffConfig): Promise<Response>;
|
|
253
|
+
private parseErrorResponse;
|
|
254
|
+
private throwParsedResponseError;
|
|
268
255
|
private verifyBackendStatus;
|
|
256
|
+
private extractErrorInfo;
|
|
269
257
|
}
|
package/dist/utils/AloreAuth.js
CHANGED
|
@@ -13,28 +13,6 @@ const DEFAULT_URL = 'https://alpha-api.bealore.com/v1';
|
|
|
13
13
|
export class AloreAuth {
|
|
14
14
|
constructor(clientId, aloreBaseUrl, config) {
|
|
15
15
|
this.services = {
|
|
16
|
-
// Utility to check if an email is allowed based on allowedEmailDomains config
|
|
17
|
-
isEmailAllowed: (context, email) => {
|
|
18
|
-
var _a;
|
|
19
|
-
const allowedConf = (_a = context.authProviderConfigs) === null || _a === void 0 ? void 0 : _a.allowedEmailDomains;
|
|
20
|
-
const allowedList =
|
|
21
|
-
// eslint-disable-next-line no-nested-ternary
|
|
22
|
-
typeof allowedConf === 'string'
|
|
23
|
-
? [allowedConf]
|
|
24
|
-
: Array.isArray(allowedConf)
|
|
25
|
-
? allowedConf
|
|
26
|
-
: [];
|
|
27
|
-
if (allowedList.length === 0)
|
|
28
|
-
return true;
|
|
29
|
-
if (!email)
|
|
30
|
-
return false;
|
|
31
|
-
const domain = (email.split('@')[1] || '').toLowerCase();
|
|
32
|
-
// Allow items like '@bealore.com' or 'bealore.com'
|
|
33
|
-
return allowedList.some((d) => {
|
|
34
|
-
const normalized = d.startsWith('@') ? d.slice(1) : d;
|
|
35
|
-
return domain === normalized.toLowerCase();
|
|
36
|
-
});
|
|
37
|
-
},
|
|
38
16
|
healthCheck: () => __awaiter(this, void 0, void 0, function* () {
|
|
39
17
|
try {
|
|
40
18
|
yield this.verifyBackendStatus();
|
|
@@ -47,9 +25,6 @@ export class AloreAuth {
|
|
|
47
25
|
// Email and password flow
|
|
48
26
|
retrieveSalt: (context, event) => __awaiter(this, void 0, void 0, function* () {
|
|
49
27
|
const { email } = event.payload;
|
|
50
|
-
if (!this.services.isEmailAllowed(context, email)) {
|
|
51
|
-
throw new Error('Email domain not allowed');
|
|
52
|
-
}
|
|
53
28
|
const { credentialEmail } = context;
|
|
54
29
|
const response = yield this.fetchWithProgressiveBackoff(`/auth/v1/salt/${email || credentialEmail}`, {
|
|
55
30
|
headers: {
|
|
@@ -60,7 +35,8 @@ export class AloreAuth {
|
|
|
60
35
|
const salt = yield response.json();
|
|
61
36
|
return { salt };
|
|
62
37
|
}
|
|
63
|
-
|
|
38
|
+
// Propagate parsed error so machine receives full payload as event.data
|
|
39
|
+
yield this.throwParsedResponseError(response);
|
|
64
40
|
}),
|
|
65
41
|
verifyEmail: (context, event) => __awaiter(this, void 0, void 0, function* () {
|
|
66
42
|
const { secureCode } = event.payload;
|
|
@@ -77,14 +53,10 @@ export class AloreAuth {
|
|
|
77
53
|
if (response.ok) {
|
|
78
54
|
return {};
|
|
79
55
|
}
|
|
80
|
-
|
|
81
|
-
throw new Error((data === null || data === void 0 ? void 0 : data.message) || (data === null || data === void 0 ? void 0 : data.error) || data);
|
|
56
|
+
yield this.throwParsedResponseError(response);
|
|
82
57
|
}),
|
|
83
58
|
completeRegistration: (context, event) => __awaiter(this, void 0, void 0, function* () {
|
|
84
59
|
const { email, nickname, passwordHash, device } = event.payload;
|
|
85
|
-
if (!this.services.isEmailAllowed(context, email)) {
|
|
86
|
-
throw new Error('Email domain not allowed');
|
|
87
|
-
}
|
|
88
60
|
const { firebaseCompatible } = context.authProviderConfigs || {};
|
|
89
61
|
const response = yield this.fetchWithProgressiveBackoff(`/auth/v1/account-registration${firebaseCompatible ? `?firebaseCompatibleToken=${firebaseCompatible}` : ''}`, {
|
|
90
62
|
method: 'POST',
|
|
@@ -101,29 +73,27 @@ export class AloreAuth {
|
|
|
101
73
|
const data = yield response.json();
|
|
102
74
|
if (response.ok)
|
|
103
75
|
return data;
|
|
104
|
-
|
|
76
|
+
yield this.throwParsedResponseError(response);
|
|
105
77
|
}),
|
|
106
78
|
confirmPassword: (context, event) => __awaiter(this, void 0, void 0, function* () {
|
|
107
|
-
const {
|
|
108
|
-
const response = yield this.fetchWithProgressiveBackoff(`/auth/v1/password
|
|
79
|
+
const { salt, passwordHash, token } = event.payload;
|
|
80
|
+
const response = yield this.fetchWithProgressiveBackoff(`/auth/v1/reset-password`, {
|
|
109
81
|
method: 'POST',
|
|
110
82
|
headers: {
|
|
111
83
|
'Content-Type': 'application/json',
|
|
112
84
|
},
|
|
113
85
|
body: JSON.stringify({
|
|
114
|
-
|
|
115
|
-
|
|
86
|
+
token,
|
|
87
|
+
salt,
|
|
88
|
+
newPassword: passwordHash,
|
|
116
89
|
}),
|
|
117
90
|
});
|
|
118
91
|
if (!response.ok)
|
|
119
|
-
|
|
120
|
-
return {
|
|
92
|
+
yield this.throwParsedResponseError(response);
|
|
93
|
+
return {};
|
|
121
94
|
}),
|
|
122
95
|
sendConfirmationEmail: (context, event) => __awaiter(this, void 0, void 0, function* () {
|
|
123
96
|
const { email, nickname, locale } = event.payload;
|
|
124
|
-
if (!this.services.isEmailAllowed(context, email)) {
|
|
125
|
-
throw new Error('Email domain not allowed');
|
|
126
|
-
}
|
|
127
97
|
const searchParams = new URLSearchParams();
|
|
128
98
|
const url = '/auth/v1/confirmation-email';
|
|
129
99
|
if (locale) {
|
|
@@ -147,18 +117,17 @@ export class AloreAuth {
|
|
|
147
117
|
const { salt, sessionId } = resJson;
|
|
148
118
|
return { salt, sessionId };
|
|
149
119
|
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
throw new Error(resJson);
|
|
153
|
-
}
|
|
154
|
-
return { error: response === null || response === void 0 ? void 0 : response.statusText };
|
|
120
|
+
// Throw parsed error for machine to consume
|
|
121
|
+
yield this.throwParsedResponseError(response);
|
|
155
122
|
}),
|
|
156
123
|
sendCode: (context, event) => __awaiter(this, void 0, void 0, function* () {
|
|
157
|
-
const { email } = event.payload;
|
|
158
|
-
|
|
159
|
-
|
|
124
|
+
const { email, locale } = event.payload;
|
|
125
|
+
const searchParams = new URLSearchParams();
|
|
126
|
+
const url = '/auth/v1/request-password-reset';
|
|
127
|
+
if (locale) {
|
|
128
|
+
searchParams.append('locale', locale);
|
|
160
129
|
}
|
|
161
|
-
const response = yield this.fetchWithProgressiveBackoff(
|
|
130
|
+
const response = yield this.fetchWithProgressiveBackoff(searchParams.size > 0 ? `${url}?${searchParams.toString()}` : url, {
|
|
162
131
|
method: 'POST',
|
|
163
132
|
headers: {
|
|
164
133
|
'Content-Type': 'application/json',
|
|
@@ -168,15 +137,12 @@ export class AloreAuth {
|
|
|
168
137
|
}),
|
|
169
138
|
});
|
|
170
139
|
if (!response.ok)
|
|
171
|
-
|
|
140
|
+
yield this.throwParsedResponseError(response);
|
|
172
141
|
return {};
|
|
173
142
|
}),
|
|
174
143
|
verifyLogin: (context, event) => __awaiter(this, void 0, void 0, function* () {
|
|
175
144
|
var _a, _b;
|
|
176
145
|
const { email, passwordHash, device, locale } = event.payload;
|
|
177
|
-
if (!this.services.isEmailAllowed(context, email || context.credentialEmail)) {
|
|
178
|
-
throw new Error('Email domain not allowed');
|
|
179
|
-
}
|
|
180
146
|
const { credentialEmail } = context;
|
|
181
147
|
const searchParams = new URLSearchParams();
|
|
182
148
|
const url = '/auth/v1/login-verification';
|
|
@@ -205,7 +171,7 @@ export class AloreAuth {
|
|
|
205
171
|
if (((_a = data === null || data === void 0 ? void 0 : data.error) === null || _a === void 0 ? void 0 : _a.includes('2FA')) || ((_b = data === null || data === void 0 ? void 0 : data.error) === null || _b === void 0 ? void 0 : _b.includes('device'))) {
|
|
206
172
|
return { error: data === null || data === void 0 ? void 0 : data.error };
|
|
207
173
|
}
|
|
208
|
-
|
|
174
|
+
yield this.throwParsedResponseError(response);
|
|
209
175
|
}
|
|
210
176
|
else {
|
|
211
177
|
return data;
|
|
@@ -215,9 +181,6 @@ export class AloreAuth {
|
|
|
215
181
|
startRegisterPasskey: (context, event) => __awaiter(this, void 0, void 0, function* () {
|
|
216
182
|
const { rpDomain } = context.authProviderConfigs || {};
|
|
217
183
|
const { email, nickname, device } = event.payload || context.registerUser;
|
|
218
|
-
if (email && !this.services.isEmailAllowed(context, email)) {
|
|
219
|
-
throw new Error('Email domain not allowed');
|
|
220
|
-
}
|
|
221
184
|
const startPasskeyRegistrationResponse = yield this.fetchWithProgressiveBackoff('/auth/v1/account-registration-passkey', {
|
|
222
185
|
method: 'POST',
|
|
223
186
|
headers: {
|
|
@@ -231,14 +194,13 @@ export class AloreAuth {
|
|
|
231
194
|
}),
|
|
232
195
|
});
|
|
233
196
|
const data = yield startPasskeyRegistrationResponse.json();
|
|
197
|
+
if (!startPasskeyRegistrationResponse.ok)
|
|
198
|
+
yield this.throwParsedResponseError(startPasskeyRegistrationResponse);
|
|
234
199
|
return data;
|
|
235
200
|
}),
|
|
236
201
|
finishRegisterPasskey: (context, event) => __awaiter(this, void 0, void 0, function* () {
|
|
237
202
|
const { rpDomain } = context.authProviderConfigs || {};
|
|
238
203
|
const { email, nickname, device, passkeyRegistration } = event.payload || {};
|
|
239
|
-
if (email && !this.services.isEmailAllowed(context, email)) {
|
|
240
|
-
throw new Error('Email domain not allowed');
|
|
241
|
-
}
|
|
242
204
|
const response = yield this.fetchWithProgressiveBackoff('/auth/v1/account-registration-passkey-finish', {
|
|
243
205
|
method: 'POST',
|
|
244
206
|
headers: {
|
|
@@ -262,9 +224,6 @@ export class AloreAuth {
|
|
|
262
224
|
var _c;
|
|
263
225
|
const { rpDomain } = context.authProviderConfigs || {};
|
|
264
226
|
const email = (_c = event.payload) === null || _c === void 0 ? void 0 : _c.email;
|
|
265
|
-
if (email && !this.services.isEmailAllowed(context, email)) {
|
|
266
|
-
throw new Error('Email domain not allowed');
|
|
267
|
-
}
|
|
268
227
|
const searchParams = new URLSearchParams();
|
|
269
228
|
const url = '/auth/v1/login-passkey';
|
|
270
229
|
if (rpDomain) {
|
|
@@ -297,7 +256,7 @@ export class AloreAuth {
|
|
|
297
256
|
const data = yield response.json();
|
|
298
257
|
if (response.ok)
|
|
299
258
|
return data;
|
|
300
|
-
|
|
259
|
+
yield this.throwParsedResponseError(response);
|
|
301
260
|
}),
|
|
302
261
|
// Hardware 2FA flow
|
|
303
262
|
verify2faCode: (_context, event) => __awaiter(this, void 0, void 0, function* () {
|
|
@@ -317,7 +276,7 @@ export class AloreAuth {
|
|
|
317
276
|
const data = yield response.json();
|
|
318
277
|
if (response.ok)
|
|
319
278
|
return data;
|
|
320
|
-
|
|
279
|
+
yield this.throwParsedResponseError(response);
|
|
321
280
|
}),
|
|
322
281
|
authenticateWebauthn: (context, event) => __awaiter(this, void 0, void 0, function* () {
|
|
323
282
|
const { email, passwordHash, device, authId } = event.payload;
|
|
@@ -355,7 +314,7 @@ export class AloreAuth {
|
|
|
355
314
|
const data = yield response.json();
|
|
356
315
|
if (response.ok)
|
|
357
316
|
return data;
|
|
358
|
-
|
|
317
|
+
yield this.throwParsedResponseError(response);
|
|
359
318
|
}),
|
|
360
319
|
verifyDeviceCode: (context, event) => __awaiter(this, void 0, void 0, function* () {
|
|
361
320
|
const { email, passwordHash, device, secureCode } = event.payload;
|
|
@@ -380,9 +339,6 @@ export class AloreAuth {
|
|
|
380
339
|
verifyEmail2fa: (context, event) => __awaiter(this, void 0, void 0, function* () {
|
|
381
340
|
const { email, passwordHash, secureCode } = event.payload;
|
|
382
341
|
const { credentialEmail, authProviderConfigs } = context;
|
|
383
|
-
if (!this.services.isEmailAllowed(context, email || credentialEmail)) {
|
|
384
|
-
throw new Error('Email domain not allowed');
|
|
385
|
-
}
|
|
386
342
|
const { firebaseCompatible } = authProviderConfigs || {};
|
|
387
343
|
const response = yield this.fetchWithProgressiveBackoff(`/auth/v1/email-2fa-verification${firebaseCompatible ? `?firebaseCompatibleToken=${firebaseCompatible}` : ''}`, {
|
|
388
344
|
method: 'POST',
|
|
@@ -414,7 +370,7 @@ export class AloreAuth {
|
|
|
414
370
|
});
|
|
415
371
|
const data = yield response.json();
|
|
416
372
|
if (!response.ok)
|
|
417
|
-
|
|
373
|
+
yield this.throwParsedResponseError(response);
|
|
418
374
|
return data;
|
|
419
375
|
}),
|
|
420
376
|
verifyClaimNftEmail2fa: (context, event) => __awaiter(this, void 0, void 0, function* () {
|
|
@@ -433,14 +389,13 @@ export class AloreAuth {
|
|
|
433
389
|
const data = yield response.json();
|
|
434
390
|
if (response.ok)
|
|
435
391
|
return data;
|
|
436
|
-
|
|
392
|
+
yield this.throwParsedResponseError(response);
|
|
437
393
|
}),
|
|
438
394
|
fetchForgeData: (_, event) => __awaiter(this, void 0, void 0, function* () {
|
|
439
395
|
const response = yield this.fetchWithProgressiveBackoff(`/forges/${event.forgeId}`);
|
|
440
396
|
const data = (yield response.json());
|
|
441
|
-
if (!response.ok)
|
|
442
|
-
|
|
443
|
-
}
|
|
397
|
+
if (!response.ok)
|
|
398
|
+
yield this.throwParsedResponseError(response);
|
|
444
399
|
return data;
|
|
445
400
|
}),
|
|
446
401
|
// Google login flow
|
|
@@ -460,9 +415,6 @@ export class AloreAuth {
|
|
|
460
415
|
});
|
|
461
416
|
const data = yield response.json();
|
|
462
417
|
if (response.ok) {
|
|
463
|
-
if (!this.services.isEmailAllowed(context, data.email)) {
|
|
464
|
-
throw new Error('Email domain not allowed');
|
|
465
|
-
}
|
|
466
418
|
return {
|
|
467
419
|
googleOtpCode: data.otpCode,
|
|
468
420
|
salt: data.salt,
|
|
@@ -475,9 +427,6 @@ export class AloreAuth {
|
|
|
475
427
|
};
|
|
476
428
|
}
|
|
477
429
|
if (response.status === 404) {
|
|
478
|
-
if (!this.services.isEmailAllowed(context, data.email)) {
|
|
479
|
-
throw new Error('Email domain not allowed');
|
|
480
|
-
}
|
|
481
430
|
return {
|
|
482
431
|
isNewUser: true,
|
|
483
432
|
socialProviderRegisterUser: {
|
|
@@ -488,7 +437,7 @@ export class AloreAuth {
|
|
|
488
437
|
};
|
|
489
438
|
}
|
|
490
439
|
if (!response.ok)
|
|
491
|
-
|
|
440
|
+
yield this.throwParsedResponseError(response);
|
|
492
441
|
return {};
|
|
493
442
|
}),
|
|
494
443
|
verifyGoogleLogin: (context, event) => __awaiter(this, void 0, void 0, function* () {
|
|
@@ -524,6 +473,21 @@ export class AloreAuth {
|
|
|
524
473
|
yield new Promise((resolve) => setTimeout(resolve, ms));
|
|
525
474
|
});
|
|
526
475
|
}
|
|
476
|
+
// eslint-disable-next-line class-methods-use-this
|
|
477
|
+
isLocalUrl(urlString) {
|
|
478
|
+
try {
|
|
479
|
+
const parsed = new URL(urlString);
|
|
480
|
+
const isHttp = parsed.protocol === 'http:';
|
|
481
|
+
const isLoopbackHost = parsed.hostname === 'localhost' ||
|
|
482
|
+
parsed.hostname === '127.0.0.1' ||
|
|
483
|
+
parsed.hostname === '::1';
|
|
484
|
+
const isPort8000 = parsed.port === '8000';
|
|
485
|
+
return isHttp || isLoopbackHost || isPort8000;
|
|
486
|
+
}
|
|
487
|
+
catch (_e) {
|
|
488
|
+
return false;
|
|
489
|
+
}
|
|
490
|
+
}
|
|
527
491
|
fetchWithProgressiveBackoff(
|
|
528
492
|
// eslint-disable-next-line no-undef
|
|
529
493
|
url,
|
|
@@ -533,8 +497,9 @@ export class AloreAuth {
|
|
|
533
497
|
const { maxAttempts = 3, initialDelay = 200 } = config || {};
|
|
534
498
|
let attempt = 0;
|
|
535
499
|
let delayValue = initialDelay;
|
|
500
|
+
const isLocal = this.isLocalUrl(this.aloreBaseUrl);
|
|
536
501
|
// eslint-disable-next-line no-undef
|
|
537
|
-
const init = Object.assign(Object.assign({}, options), { credentials: 'include', headers: Object.assign(Object.assign({}, options === null || options === void 0 ? void 0 : options.headers), { 'X-CLIENT-ID': this.clientId, 'CF-Connecting-IP': '127.0.0.1' }) });
|
|
502
|
+
const init = Object.assign(Object.assign({}, options), { credentials: 'include', headers: Object.assign(Object.assign(Object.assign({}, options === null || options === void 0 ? void 0 : options.headers), { 'X-CLIENT-ID': this.clientId }), (isLocal && { 'CF-Connecting-IP': '127.0.0.1' })) });
|
|
538
503
|
while (attempt < maxAttempts) {
|
|
539
504
|
if (attempt > 0) {
|
|
540
505
|
// eslint-disable-next-line no-await-in-loop, no-promise-executor-return, no-loop-func
|
|
@@ -551,31 +516,110 @@ export class AloreAuth {
|
|
|
551
516
|
}
|
|
552
517
|
catch (error) {
|
|
553
518
|
console.error(error);
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
519
|
+
// Treat any fetch TypeError as a network/CORS-style failure. Browsers localize the message.
|
|
520
|
+
const isFailedToFetch = error instanceof TypeError;
|
|
521
|
+
if (isFailedToFetch && attempt >= maxAttempts) {
|
|
522
|
+
// Determine whether backend is down or the request is likely blocked (e.g., CORS)
|
|
523
|
+
const origin = typeof window !== 'undefined' ? window.location.origin : undefined;
|
|
524
|
+
try {
|
|
525
|
+
// eslint-disable-next-line no-await-in-loop
|
|
526
|
+
const health = yield this.verifyBackendStatus();
|
|
527
|
+
const fullUrl = `${this.aloreBaseUrl}${url}`;
|
|
528
|
+
const message = origin
|
|
529
|
+
? `Request blocked by CORS policy. Endpoint: ${fullUrl}. Please ensure origin ${origin} is allowed.`
|
|
530
|
+
: `Request blocked by CORS policy. Endpoint: ${fullUrl}.`;
|
|
531
|
+
const corsErr = new AloreAuthError(ErrorTypes.CORS_BLOCKED, message);
|
|
532
|
+
// Preserve original fetch error and health-check details for diagnostics
|
|
533
|
+
// @ts-ignore
|
|
534
|
+
corsErr.data = {
|
|
535
|
+
originalError: this.extractErrorInfo(error),
|
|
536
|
+
healthCheck: Object.assign({ reachable: true }, health),
|
|
537
|
+
};
|
|
538
|
+
throw corsErr;
|
|
539
|
+
}
|
|
540
|
+
catch (healthError) {
|
|
541
|
+
// Backend health-check failed → server likely down/unreachable
|
|
542
|
+
const downErr = new AloreAuthError(ErrorTypes.SERVER_DOWN, `Server is unreachable at ${this.aloreBaseUrl}. Please try again later.`);
|
|
543
|
+
// @ts-ignore
|
|
544
|
+
downErr.data = {
|
|
545
|
+
originalError: this.extractErrorInfo(error),
|
|
546
|
+
healthCheck: { reachable: false, error: this.extractErrorInfo(healthError) },
|
|
547
|
+
};
|
|
548
|
+
throw downErr;
|
|
549
|
+
}
|
|
559
550
|
}
|
|
560
|
-
|
|
551
|
+
if (attempt < maxAttempts) {
|
|
561
552
|
console.error(`Attempt ${attempt} failed, retrying in ${delayValue}ms...`);
|
|
562
553
|
}
|
|
563
554
|
}
|
|
564
555
|
}
|
|
565
|
-
|
|
556
|
+
// Final fallback (should rarely hit due to early throws above)
|
|
557
|
+
throw new AloreAuthError(ErrorTypes.FAILED_TO_FETCH, `Max attempts (${maxAttempts}) exceeded while contacting ${this.aloreBaseUrl}${url}`);
|
|
566
558
|
});
|
|
567
559
|
}
|
|
568
|
-
|
|
560
|
+
// eslint-disable-next-line class-methods-use-this
|
|
561
|
+
parseErrorResponse(response) {
|
|
569
562
|
return __awaiter(this, void 0, void 0, function* () {
|
|
570
563
|
try {
|
|
571
|
-
const
|
|
572
|
-
|
|
573
|
-
|
|
564
|
+
const data = yield response.json();
|
|
565
|
+
// Standardize to server payload when present
|
|
566
|
+
if (data && typeof data === 'object' && (data.message || data.status)) {
|
|
567
|
+
return {
|
|
568
|
+
status: data.status || response.status,
|
|
569
|
+
message: data.message || data.error || response.statusText,
|
|
570
|
+
timestamp: data.timestamp,
|
|
571
|
+
};
|
|
574
572
|
}
|
|
573
|
+
return { status: response.status, message: response.statusText };
|
|
574
|
+
}
|
|
575
|
+
catch (_a) {
|
|
576
|
+
return { status: response.status, message: response.statusText };
|
|
577
|
+
}
|
|
578
|
+
});
|
|
579
|
+
}
|
|
580
|
+
// eslint-disable-next-line class-methods-use-this
|
|
581
|
+
throwParsedResponseError(response) {
|
|
582
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
583
|
+
const parsed = yield this.parseErrorResponse(response);
|
|
584
|
+
// Normalize well-known auth errors for consistent UI handling
|
|
585
|
+
let normalizedMessage = parsed.message || 'Request failed';
|
|
586
|
+
if (parsed.status === 401 && parsed.message !== 'INVALID_SESSION') {
|
|
587
|
+
normalizedMessage = 'INVALID_CREDENTIALS';
|
|
588
|
+
}
|
|
589
|
+
else if (parsed.status === 403 && parsed.message === 'EMAIL_NOT_ALLOWED') {
|
|
590
|
+
normalizedMessage = 'EMAIL_NOT_ALLOWED';
|
|
591
|
+
}
|
|
592
|
+
const err = new AloreAuthError(ErrorTypes.FAILED_TO_FETCH, normalizedMessage);
|
|
593
|
+
// @ts-ignore attach server payload for consumers
|
|
594
|
+
err.data = Object.assign(Object.assign({}, parsed), { message: normalizedMessage });
|
|
595
|
+
throw err;
|
|
596
|
+
});
|
|
597
|
+
}
|
|
598
|
+
verifyBackendStatus() {
|
|
599
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
600
|
+
const endpoint = '/health/ready';
|
|
601
|
+
// eslint-disable-next-line no-undef
|
|
602
|
+
const init = { mode: 'no-cors', cache: 'no-store' };
|
|
603
|
+
try {
|
|
604
|
+
yield fetch(`${this.aloreBaseUrl}${endpoint}`, init);
|
|
605
|
+
return { checkedEndpoints: [endpoint] };
|
|
575
606
|
}
|
|
576
607
|
catch (_a) {
|
|
577
608
|
throw new Error('Server down');
|
|
578
609
|
}
|
|
579
610
|
});
|
|
580
611
|
}
|
|
612
|
+
// Capture a concise, serializable snapshot of an unknown error
|
|
613
|
+
// eslint-disable-next-line class-methods-use-this
|
|
614
|
+
extractErrorInfo(error) {
|
|
615
|
+
if (error instanceof Error) {
|
|
616
|
+
return { name: error.name, message: error.message, stack: error.stack };
|
|
617
|
+
}
|
|
618
|
+
try {
|
|
619
|
+
return JSON.parse(JSON.stringify(error));
|
|
620
|
+
}
|
|
621
|
+
catch (_a) {
|
|
622
|
+
return { error };
|
|
623
|
+
}
|
|
624
|
+
}
|
|
581
625
|
}
|
|
@@ -5,10 +5,12 @@ export declare enum ErrorCodes {
|
|
|
5
5
|
}
|
|
6
6
|
export declare enum ErrorTypes {
|
|
7
7
|
CLIENT_ID_REQUIRED = "CLIENT_ID_REQUIRED",
|
|
8
|
+
EMAIL_DOMAIN_NOT_ALLOWED = "EMAIL_DOMAIN_NOT_ALLOWED",
|
|
8
9
|
PASSKEY_NOT_SUPPORTED = "PASSKEY_NOT_SUPPORTED",
|
|
9
10
|
USER_ALREADY_EXISTS = "USER_ALREADY_EXISTS",
|
|
10
11
|
FAILED_TO_FETCH = "FAILED_TO_FETCH",
|
|
11
12
|
SERVER_DOWN = "SERVER_DOWN",
|
|
13
|
+
CORS_BLOCKED = "CORS_BLOCKED",
|
|
12
14
|
BAD_RESPONSE = "BAD_RESPONSE",
|
|
13
15
|
UNKNOWN_ERROR = "UNKNOWN_ERROR",
|
|
14
16
|
INVALID_CREDENTIALS = "INVALID_CREDENTIALS",
|
package/dist/utils/AuthError.js
CHANGED
|
@@ -8,10 +8,12 @@ export var ErrorCodes;
|
|
|
8
8
|
export var ErrorTypes;
|
|
9
9
|
(function (ErrorTypes) {
|
|
10
10
|
ErrorTypes["CLIENT_ID_REQUIRED"] = "CLIENT_ID_REQUIRED";
|
|
11
|
+
ErrorTypes["EMAIL_DOMAIN_NOT_ALLOWED"] = "EMAIL_DOMAIN_NOT_ALLOWED";
|
|
11
12
|
ErrorTypes["PASSKEY_NOT_SUPPORTED"] = "PASSKEY_NOT_SUPPORTED";
|
|
12
13
|
ErrorTypes["USER_ALREADY_EXISTS"] = "USER_ALREADY_EXISTS";
|
|
13
14
|
ErrorTypes["FAILED_TO_FETCH"] = "FAILED_TO_FETCH";
|
|
14
15
|
ErrorTypes["SERVER_DOWN"] = "SERVER_DOWN";
|
|
16
|
+
ErrorTypes["CORS_BLOCKED"] = "CORS_BLOCKED";
|
|
15
17
|
ErrorTypes["BAD_RESPONSE"] = "BAD_RESPONSE";
|
|
16
18
|
ErrorTypes["UNKNOWN_ERROR"] = "UNKNOWN_ERROR";
|
|
17
19
|
ErrorTypes["INVALID_CREDENTIALS"] = "INVALID_CREDENTIALS";
|