@alore/auth-react-sdk 1.1.0-alpha.5 → 1.1.0-alpha.7
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/types.d.ts +0 -2
- package/dist/utils/AloreAuth.d.ts +9 -24
- package/dist/utils/AloreAuth.js +110 -88
- package/dist/utils/AuthError.d.ts +1 -0
- package/dist/utils/AuthError.js +1 -0
- package/package.json +1 -1
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: {
|
|
@@ -42,11 +37,7 @@ export declare class AloreAuth {
|
|
|
42
37
|
email: string;
|
|
43
38
|
passwordHash: string;
|
|
44
39
|
};
|
|
45
|
-
}) => Promise<{
|
|
46
|
-
error?: undefined;
|
|
47
|
-
} | {
|
|
48
|
-
error: string;
|
|
49
|
-
}>;
|
|
40
|
+
}) => Promise<{}>;
|
|
50
41
|
sendConfirmationEmail: (context: AuthMachineContext, event: {
|
|
51
42
|
type: 'RESEND_CODE';
|
|
52
43
|
payload: {
|
|
@@ -68,22 +59,13 @@ export declare class AloreAuth {
|
|
|
68
59
|
}) => Promise<{
|
|
69
60
|
salt: any;
|
|
70
61
|
sessionId: any;
|
|
71
|
-
|
|
72
|
-
} | {
|
|
73
|
-
error: string;
|
|
74
|
-
salt?: undefined;
|
|
75
|
-
sessionId?: undefined;
|
|
76
|
-
}>;
|
|
62
|
+
} | undefined>;
|
|
77
63
|
sendCode: (context: AuthMachineContext, event: {
|
|
78
64
|
type: 'SEND_CODE';
|
|
79
65
|
payload: {
|
|
80
66
|
email: string;
|
|
81
67
|
};
|
|
82
|
-
}) => Promise<{
|
|
83
|
-
error: string;
|
|
84
|
-
} | {
|
|
85
|
-
error?: undefined;
|
|
86
|
-
}>;
|
|
68
|
+
}) => Promise<{}>;
|
|
87
69
|
verifyLogin: (context: AuthMachineContext, event: {
|
|
88
70
|
type: 'VERIFY_LOGIN';
|
|
89
71
|
payload: {
|
|
@@ -265,5 +247,8 @@ export declare class AloreAuth {
|
|
|
265
247
|
};
|
|
266
248
|
private delay;
|
|
267
249
|
fetchWithProgressiveBackoff(url: RequestInfo | URL, options?: RequestInit, config?: FetchWithProgressiveBackoffConfig): Promise<Response>;
|
|
250
|
+
private parseErrorResponse;
|
|
251
|
+
private throwParsedResponseError;
|
|
268
252
|
private verifyBackendStatus;
|
|
253
|
+
private extractErrorInfo;
|
|
269
254
|
}
|
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 AloreAuthError(ErrorTypes.EMAIL_DOMAIN_NOT_ALLOWED, 'Email domain not allowed', 400);
|
|
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 AloreAuthError(ErrorTypes.EMAIL_DOMAIN_NOT_ALLOWED, 'Email domain not allowed', 400);
|
|
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,7 +73,7 @@ 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
79
|
const { email, passwordHash } = event.payload;
|
|
@@ -116,14 +88,11 @@ export class AloreAuth {
|
|
|
116
88
|
}),
|
|
117
89
|
});
|
|
118
90
|
if (!response.ok)
|
|
119
|
-
|
|
120
|
-
return {
|
|
91
|
+
yield this.throwParsedResponseError(response);
|
|
92
|
+
return {};
|
|
121
93
|
}),
|
|
122
94
|
sendConfirmationEmail: (context, event) => __awaiter(this, void 0, void 0, function* () {
|
|
123
95
|
const { email, nickname, locale } = event.payload;
|
|
124
|
-
if (!this.services.isEmailAllowed(context, email)) {
|
|
125
|
-
throw new AloreAuthError(ErrorTypes.EMAIL_DOMAIN_NOT_ALLOWED, 'Email domain not allowed', 400);
|
|
126
|
-
}
|
|
127
96
|
const searchParams = new URLSearchParams();
|
|
128
97
|
const url = '/auth/v1/confirmation-email';
|
|
129
98
|
if (locale) {
|
|
@@ -147,17 +116,11 @@ export class AloreAuth {
|
|
|
147
116
|
const { salt, sessionId } = resJson;
|
|
148
117
|
return { salt, sessionId };
|
|
149
118
|
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
throw new Error(resJson);
|
|
153
|
-
}
|
|
154
|
-
return { error: response === null || response === void 0 ? void 0 : response.statusText };
|
|
119
|
+
// Throw parsed error for machine to consume
|
|
120
|
+
yield this.throwParsedResponseError(response);
|
|
155
121
|
}),
|
|
156
122
|
sendCode: (context, event) => __awaiter(this, void 0, void 0, function* () {
|
|
157
123
|
const { email } = event.payload;
|
|
158
|
-
if (!this.services.isEmailAllowed(context, email)) {
|
|
159
|
-
throw new AloreAuthError(ErrorTypes.EMAIL_DOMAIN_NOT_ALLOWED, 'Email domain not allowed', 400);
|
|
160
|
-
}
|
|
161
124
|
const response = yield this.fetchWithProgressiveBackoff(`/reset-password`, {
|
|
162
125
|
method: 'POST',
|
|
163
126
|
headers: {
|
|
@@ -168,15 +131,12 @@ export class AloreAuth {
|
|
|
168
131
|
}),
|
|
169
132
|
});
|
|
170
133
|
if (!response.ok)
|
|
171
|
-
|
|
134
|
+
yield this.throwParsedResponseError(response);
|
|
172
135
|
return {};
|
|
173
136
|
}),
|
|
174
137
|
verifyLogin: (context, event) => __awaiter(this, void 0, void 0, function* () {
|
|
175
138
|
var _a, _b;
|
|
176
139
|
const { email, passwordHash, device, locale } = event.payload;
|
|
177
|
-
if (!this.services.isEmailAllowed(context, email || context.credentialEmail)) {
|
|
178
|
-
throw new AloreAuthError(ErrorTypes.EMAIL_DOMAIN_NOT_ALLOWED, 'Email domain not allowed', 400);
|
|
179
|
-
}
|
|
180
140
|
const { credentialEmail } = context;
|
|
181
141
|
const searchParams = new URLSearchParams();
|
|
182
142
|
const url = '/auth/v1/login-verification';
|
|
@@ -205,7 +165,7 @@ export class AloreAuth {
|
|
|
205
165
|
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
166
|
return { error: data === null || data === void 0 ? void 0 : data.error };
|
|
207
167
|
}
|
|
208
|
-
|
|
168
|
+
yield this.throwParsedResponseError(response);
|
|
209
169
|
}
|
|
210
170
|
else {
|
|
211
171
|
return data;
|
|
@@ -215,9 +175,6 @@ export class AloreAuth {
|
|
|
215
175
|
startRegisterPasskey: (context, event) => __awaiter(this, void 0, void 0, function* () {
|
|
216
176
|
const { rpDomain } = context.authProviderConfigs || {};
|
|
217
177
|
const { email, nickname, device } = event.payload || context.registerUser;
|
|
218
|
-
if (email && !this.services.isEmailAllowed(context, email)) {
|
|
219
|
-
throw new AloreAuthError(ErrorTypes.EMAIL_DOMAIN_NOT_ALLOWED, 'Email domain not allowed', 400);
|
|
220
|
-
}
|
|
221
178
|
const startPasskeyRegistrationResponse = yield this.fetchWithProgressiveBackoff('/auth/v1/account-registration-passkey', {
|
|
222
179
|
method: 'POST',
|
|
223
180
|
headers: {
|
|
@@ -231,14 +188,13 @@ export class AloreAuth {
|
|
|
231
188
|
}),
|
|
232
189
|
});
|
|
233
190
|
const data = yield startPasskeyRegistrationResponse.json();
|
|
191
|
+
if (!startPasskeyRegistrationResponse.ok)
|
|
192
|
+
yield this.throwParsedResponseError(startPasskeyRegistrationResponse);
|
|
234
193
|
return data;
|
|
235
194
|
}),
|
|
236
195
|
finishRegisterPasskey: (context, event) => __awaiter(this, void 0, void 0, function* () {
|
|
237
196
|
const { rpDomain } = context.authProviderConfigs || {};
|
|
238
197
|
const { email, nickname, device, passkeyRegistration } = event.payload || {};
|
|
239
|
-
if (email && !this.services.isEmailAllowed(context, email)) {
|
|
240
|
-
throw new AloreAuthError(ErrorTypes.EMAIL_DOMAIN_NOT_ALLOWED, 'Email domain not allowed', 400);
|
|
241
|
-
}
|
|
242
198
|
const response = yield this.fetchWithProgressiveBackoff('/auth/v1/account-registration-passkey-finish', {
|
|
243
199
|
method: 'POST',
|
|
244
200
|
headers: {
|
|
@@ -262,9 +218,6 @@ export class AloreAuth {
|
|
|
262
218
|
var _c;
|
|
263
219
|
const { rpDomain } = context.authProviderConfigs || {};
|
|
264
220
|
const email = (_c = event.payload) === null || _c === void 0 ? void 0 : _c.email;
|
|
265
|
-
if (email && !this.services.isEmailAllowed(context, email)) {
|
|
266
|
-
throw new AloreAuthError(ErrorTypes.EMAIL_DOMAIN_NOT_ALLOWED, 'Email domain not allowed', 400);
|
|
267
|
-
}
|
|
268
221
|
const searchParams = new URLSearchParams();
|
|
269
222
|
const url = '/auth/v1/login-passkey';
|
|
270
223
|
if (rpDomain) {
|
|
@@ -297,7 +250,7 @@ export class AloreAuth {
|
|
|
297
250
|
const data = yield response.json();
|
|
298
251
|
if (response.ok)
|
|
299
252
|
return data;
|
|
300
|
-
|
|
253
|
+
yield this.throwParsedResponseError(response);
|
|
301
254
|
}),
|
|
302
255
|
// Hardware 2FA flow
|
|
303
256
|
verify2faCode: (_context, event) => __awaiter(this, void 0, void 0, function* () {
|
|
@@ -317,7 +270,7 @@ export class AloreAuth {
|
|
|
317
270
|
const data = yield response.json();
|
|
318
271
|
if (response.ok)
|
|
319
272
|
return data;
|
|
320
|
-
|
|
273
|
+
yield this.throwParsedResponseError(response);
|
|
321
274
|
}),
|
|
322
275
|
authenticateWebauthn: (context, event) => __awaiter(this, void 0, void 0, function* () {
|
|
323
276
|
const { email, passwordHash, device, authId } = event.payload;
|
|
@@ -355,7 +308,7 @@ export class AloreAuth {
|
|
|
355
308
|
const data = yield response.json();
|
|
356
309
|
if (response.ok)
|
|
357
310
|
return data;
|
|
358
|
-
|
|
311
|
+
yield this.throwParsedResponseError(response);
|
|
359
312
|
}),
|
|
360
313
|
verifyDeviceCode: (context, event) => __awaiter(this, void 0, void 0, function* () {
|
|
361
314
|
const { email, passwordHash, device, secureCode } = event.payload;
|
|
@@ -380,9 +333,6 @@ export class AloreAuth {
|
|
|
380
333
|
verifyEmail2fa: (context, event) => __awaiter(this, void 0, void 0, function* () {
|
|
381
334
|
const { email, passwordHash, secureCode } = event.payload;
|
|
382
335
|
const { credentialEmail, authProviderConfigs } = context;
|
|
383
|
-
if (!this.services.isEmailAllowed(context, email || credentialEmail)) {
|
|
384
|
-
throw new AloreAuthError(ErrorTypes.EMAIL_DOMAIN_NOT_ALLOWED, 'Email domain not allowed', 400);
|
|
385
|
-
}
|
|
386
336
|
const { firebaseCompatible } = authProviderConfigs || {};
|
|
387
337
|
const response = yield this.fetchWithProgressiveBackoff(`/auth/v1/email-2fa-verification${firebaseCompatible ? `?firebaseCompatibleToken=${firebaseCompatible}` : ''}`, {
|
|
388
338
|
method: 'POST',
|
|
@@ -414,7 +364,7 @@ export class AloreAuth {
|
|
|
414
364
|
});
|
|
415
365
|
const data = yield response.json();
|
|
416
366
|
if (!response.ok)
|
|
417
|
-
|
|
367
|
+
yield this.throwParsedResponseError(response);
|
|
418
368
|
return data;
|
|
419
369
|
}),
|
|
420
370
|
verifyClaimNftEmail2fa: (context, event) => __awaiter(this, void 0, void 0, function* () {
|
|
@@ -433,14 +383,13 @@ export class AloreAuth {
|
|
|
433
383
|
const data = yield response.json();
|
|
434
384
|
if (response.ok)
|
|
435
385
|
return data;
|
|
436
|
-
|
|
386
|
+
yield this.throwParsedResponseError(response);
|
|
437
387
|
}),
|
|
438
388
|
fetchForgeData: (_, event) => __awaiter(this, void 0, void 0, function* () {
|
|
439
389
|
const response = yield this.fetchWithProgressiveBackoff(`/forges/${event.forgeId}`);
|
|
440
390
|
const data = (yield response.json());
|
|
441
|
-
if (!response.ok)
|
|
442
|
-
|
|
443
|
-
}
|
|
391
|
+
if (!response.ok)
|
|
392
|
+
yield this.throwParsedResponseError(response);
|
|
444
393
|
return data;
|
|
445
394
|
}),
|
|
446
395
|
// Google login flow
|
|
@@ -460,9 +409,6 @@ export class AloreAuth {
|
|
|
460
409
|
});
|
|
461
410
|
const data = yield response.json();
|
|
462
411
|
if (response.ok) {
|
|
463
|
-
if (!this.services.isEmailAllowed(context, data.email)) {
|
|
464
|
-
throw new AloreAuthError(ErrorTypes.EMAIL_DOMAIN_NOT_ALLOWED, 'Email domain not allowed', 400);
|
|
465
|
-
}
|
|
466
412
|
return {
|
|
467
413
|
googleOtpCode: data.otpCode,
|
|
468
414
|
salt: data.salt,
|
|
@@ -475,9 +421,6 @@ export class AloreAuth {
|
|
|
475
421
|
};
|
|
476
422
|
}
|
|
477
423
|
if (response.status === 404) {
|
|
478
|
-
if (!this.services.isEmailAllowed(context, data.email)) {
|
|
479
|
-
throw new AloreAuthError(ErrorTypes.EMAIL_DOMAIN_NOT_ALLOWED, 'Email domain not allowed', 400);
|
|
480
|
-
}
|
|
481
424
|
return {
|
|
482
425
|
isNewUser: true,
|
|
483
426
|
socialProviderRegisterUser: {
|
|
@@ -488,7 +431,7 @@ export class AloreAuth {
|
|
|
488
431
|
};
|
|
489
432
|
}
|
|
490
433
|
if (!response.ok)
|
|
491
|
-
|
|
434
|
+
yield this.throwParsedResponseError(response);
|
|
492
435
|
return {};
|
|
493
436
|
}),
|
|
494
437
|
verifyGoogleLogin: (context, event) => __awaiter(this, void 0, void 0, function* () {
|
|
@@ -552,31 +495,110 @@ export class AloreAuth {
|
|
|
552
495
|
}
|
|
553
496
|
catch (error) {
|
|
554
497
|
console.error(error);
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
498
|
+
// Treat any fetch TypeError as a network/CORS-style failure. Browsers localize the message.
|
|
499
|
+
const isFailedToFetch = error instanceof TypeError;
|
|
500
|
+
if (isFailedToFetch && attempt >= maxAttempts) {
|
|
501
|
+
// Determine whether backend is down or the request is likely blocked (e.g., CORS)
|
|
502
|
+
const origin = typeof window !== 'undefined' ? window.location.origin : undefined;
|
|
503
|
+
try {
|
|
504
|
+
// eslint-disable-next-line no-await-in-loop
|
|
505
|
+
const health = yield this.verifyBackendStatus();
|
|
506
|
+
const fullUrl = `${this.aloreBaseUrl}${url}`;
|
|
507
|
+
const message = origin
|
|
508
|
+
? `Request blocked by CORS policy. Endpoint: ${fullUrl}. Please ensure origin ${origin} is allowed.`
|
|
509
|
+
: `Request blocked by CORS policy. Endpoint: ${fullUrl}.`;
|
|
510
|
+
const corsErr = new AloreAuthError(ErrorTypes.CORS_BLOCKED, message);
|
|
511
|
+
// Preserve original fetch error and health-check details for diagnostics
|
|
512
|
+
// @ts-ignore
|
|
513
|
+
corsErr.data = {
|
|
514
|
+
originalError: this.extractErrorInfo(error),
|
|
515
|
+
healthCheck: Object.assign({ reachable: true }, health),
|
|
516
|
+
};
|
|
517
|
+
throw corsErr;
|
|
518
|
+
}
|
|
519
|
+
catch (healthError) {
|
|
520
|
+
// Backend health-check failed → server likely down/unreachable
|
|
521
|
+
const downErr = new AloreAuthError(ErrorTypes.SERVER_DOWN, `Server is unreachable at ${this.aloreBaseUrl}. Please try again later.`);
|
|
522
|
+
// @ts-ignore
|
|
523
|
+
downErr.data = {
|
|
524
|
+
originalError: this.extractErrorInfo(error),
|
|
525
|
+
healthCheck: { reachable: false, error: this.extractErrorInfo(healthError) },
|
|
526
|
+
};
|
|
527
|
+
throw downErr;
|
|
528
|
+
}
|
|
560
529
|
}
|
|
561
|
-
|
|
530
|
+
if (attempt < maxAttempts) {
|
|
562
531
|
console.error(`Attempt ${attempt} failed, retrying in ${delayValue}ms...`);
|
|
563
532
|
}
|
|
564
533
|
}
|
|
565
534
|
}
|
|
566
|
-
|
|
535
|
+
// Final fallback (should rarely hit due to early throws above)
|
|
536
|
+
throw new AloreAuthError(ErrorTypes.FAILED_TO_FETCH, `Max attempts (${maxAttempts}) exceeded while contacting ${this.aloreBaseUrl}${url}`);
|
|
567
537
|
});
|
|
568
538
|
}
|
|
569
|
-
|
|
539
|
+
// eslint-disable-next-line class-methods-use-this
|
|
540
|
+
parseErrorResponse(response) {
|
|
570
541
|
return __awaiter(this, void 0, void 0, function* () {
|
|
571
542
|
try {
|
|
572
|
-
const
|
|
573
|
-
|
|
574
|
-
|
|
543
|
+
const data = yield response.json();
|
|
544
|
+
// Standardize to server payload when present
|
|
545
|
+
if (data && typeof data === 'object' && (data.message || data.status)) {
|
|
546
|
+
return {
|
|
547
|
+
status: data.status || response.status,
|
|
548
|
+
message: data.message || data.error || response.statusText,
|
|
549
|
+
timestamp: data.timestamp,
|
|
550
|
+
};
|
|
575
551
|
}
|
|
552
|
+
return { status: response.status, message: response.statusText };
|
|
553
|
+
}
|
|
554
|
+
catch (_a) {
|
|
555
|
+
return { status: response.status, message: response.statusText };
|
|
556
|
+
}
|
|
557
|
+
});
|
|
558
|
+
}
|
|
559
|
+
// eslint-disable-next-line class-methods-use-this
|
|
560
|
+
throwParsedResponseError(response) {
|
|
561
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
562
|
+
const parsed = yield this.parseErrorResponse(response);
|
|
563
|
+
// Normalize well-known auth errors for consistent UI handling
|
|
564
|
+
let normalizedMessage = parsed.message || 'Request failed';
|
|
565
|
+
if (parsed.status === 401) {
|
|
566
|
+
normalizedMessage = 'INVALID_CREDENTIALS';
|
|
567
|
+
}
|
|
568
|
+
else if (parsed.status === 403 && parsed.message === 'EMAIL_NOT_ALLOWED') {
|
|
569
|
+
normalizedMessage = 'EMAIL_NOT_ALLOWED';
|
|
570
|
+
}
|
|
571
|
+
const err = new AloreAuthError(ErrorTypes.FAILED_TO_FETCH, normalizedMessage);
|
|
572
|
+
// @ts-ignore attach server payload for consumers
|
|
573
|
+
err.data = Object.assign(Object.assign({}, parsed), { message: normalizedMessage });
|
|
574
|
+
throw err;
|
|
575
|
+
});
|
|
576
|
+
}
|
|
577
|
+
verifyBackendStatus() {
|
|
578
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
579
|
+
const endpoint = '/health/ready';
|
|
580
|
+
// eslint-disable-next-line no-undef
|
|
581
|
+
const init = { mode: 'no-cors', cache: 'no-store' };
|
|
582
|
+
try {
|
|
583
|
+
yield fetch(`${this.aloreBaseUrl}${endpoint}`, init);
|
|
584
|
+
return { checkedEndpoints: [endpoint] };
|
|
576
585
|
}
|
|
577
586
|
catch (_a) {
|
|
578
587
|
throw new Error('Server down');
|
|
579
588
|
}
|
|
580
589
|
});
|
|
581
590
|
}
|
|
591
|
+
// Capture a concise, serializable snapshot of an unknown error
|
|
592
|
+
// eslint-disable-next-line class-methods-use-this
|
|
593
|
+
extractErrorInfo(error) {
|
|
594
|
+
if (error instanceof Error) {
|
|
595
|
+
return { name: error.name, message: error.message, stack: error.stack };
|
|
596
|
+
}
|
|
597
|
+
try {
|
|
598
|
+
return JSON.parse(JSON.stringify(error));
|
|
599
|
+
}
|
|
600
|
+
catch (_a) {
|
|
601
|
+
return { error };
|
|
602
|
+
}
|
|
603
|
+
}
|
|
582
604
|
}
|
|
@@ -10,6 +10,7 @@ export declare enum ErrorTypes {
|
|
|
10
10
|
USER_ALREADY_EXISTS = "USER_ALREADY_EXISTS",
|
|
11
11
|
FAILED_TO_FETCH = "FAILED_TO_FETCH",
|
|
12
12
|
SERVER_DOWN = "SERVER_DOWN",
|
|
13
|
+
CORS_BLOCKED = "CORS_BLOCKED",
|
|
13
14
|
BAD_RESPONSE = "BAD_RESPONSE",
|
|
14
15
|
UNKNOWN_ERROR = "UNKNOWN_ERROR",
|
|
15
16
|
INVALID_CREDENTIALS = "INVALID_CREDENTIALS",
|
package/dist/utils/AuthError.js
CHANGED
|
@@ -13,6 +13,7 @@ export var ErrorTypes;
|
|
|
13
13
|
ErrorTypes["USER_ALREADY_EXISTS"] = "USER_ALREADY_EXISTS";
|
|
14
14
|
ErrorTypes["FAILED_TO_FETCH"] = "FAILED_TO_FETCH";
|
|
15
15
|
ErrorTypes["SERVER_DOWN"] = "SERVER_DOWN";
|
|
16
|
+
ErrorTypes["CORS_BLOCKED"] = "CORS_BLOCKED";
|
|
16
17
|
ErrorTypes["BAD_RESPONSE"] = "BAD_RESPONSE";
|
|
17
18
|
ErrorTypes["UNKNOWN_ERROR"] = "UNKNOWN_ERROR";
|
|
18
19
|
ErrorTypes["INVALID_CREDENTIALS"] = "INVALID_CREDENTIALS";
|