@alore/auth-react-sdk 1.1.0-alpha.6 → 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 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;
@@ -5,7 +5,6 @@ export declare class AloreAuth {
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
- error?: undefined;
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
- error?: undefined;
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,6 +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;
269
253
  private extractErrorInfo;
270
254
  }
@@ -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
- return { error: response === null || response === void 0 ? void 0 : response.statusText };
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
- const data = yield response.json();
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
- throw new Error(data.message || data.error || 'Authentication failed');
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
- return {};
120
- return { error: response === null || response === void 0 ? void 0 : response.statusText };
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
- if (response.status === 403) {
151
- const resJson = yield response.json();
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
- return { error: response === null || response === void 0 ? void 0 : response.statusText };
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
- throw new Error((data === null || data === void 0 ? void 0 : data.message) || (data === null || data === void 0 ? void 0 : data.error) || data);
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
- throw new AloreAuthError(ErrorTypes.FAILED_TO_FETCH, data.message || data.error || data);
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
- throw new Error(data.message || data.error || 'Authentication failed');
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
- throw new Error(data.message || data.error || 'Authentication failed');
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
- throw new Error((data === null || data === void 0 ? void 0 : data.message) || (data === null || data === void 0 ? void 0 : data.error) || data);
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
- throw new Error(data || 'Authentication failed');
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
- throw new Error(`Failed to fetchWithProgressiveBackoff: ${data}`);
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
- throw new Error(data.error || data.message || data);
434
+ yield this.throwParsedResponseError(response);
492
435
  return {};
493
436
  }),
494
437
  verifyGoogleLogin: (context, event) => __awaiter(this, void 0, void 0, function* () {
@@ -593,6 +536,44 @@ export class AloreAuth {
593
536
  throw new AloreAuthError(ErrorTypes.FAILED_TO_FETCH, `Max attempts (${maxAttempts}) exceeded while contacting ${this.aloreBaseUrl}${url}`);
594
537
  });
595
538
  }
539
+ // eslint-disable-next-line class-methods-use-this
540
+ parseErrorResponse(response) {
541
+ return __awaiter(this, void 0, void 0, function* () {
542
+ try {
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
+ };
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
+ }
596
577
  verifyBackendStatus() {
597
578
  return __awaiter(this, void 0, void 0, function* () {
598
579
  const endpoint = '/health/ready';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alore/auth-react-sdk",
3
- "version": "1.1.0-alpha.6",
3
+ "version": "1.1.0-alpha.7",
4
4
  "repository": "https://github.com/0xCarbon/alore-js",
5
5
  "description": "SDK for Alore Auth",
6
6
  "main": "dist/index.js",