astro-tokenkit 1.0.3 → 1.0.6

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.
@@ -15,6 +15,10 @@ export declare class TokenManager {
15
15
  * Perform token refresh
16
16
  */
17
17
  refresh(ctx: TokenKitContext, refreshToken: string): Promise<TokenBundle | null>;
18
+ /**
19
+ * Internal refresh implementation
20
+ */
21
+ private performRefresh;
18
22
  /**
19
23
  * Ensure valid tokens (with automatic refresh)
20
24
  */
@@ -8,6 +8,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
+ import { AuthError } from '../types';
11
12
  import { autoDetectFields, parseJWTPayload } from './detector';
12
13
  import { storeTokens, retrieveTokens, clearTokens } from './storage';
13
14
  import { shouldRefresh, isExpired } from './policy';
@@ -58,17 +59,29 @@ export class TokenManager {
58
59
  method: 'POST',
59
60
  headers: { 'Content-Type': 'application/json' },
60
61
  body: JSON.stringify(credentials),
62
+ }).catch(error => {
63
+ throw new AuthError(`Login request failed: ${error.message}`);
61
64
  });
62
65
  if (!response.ok) {
63
- throw new Error(`Login failed: ${response.status} ${response.statusText}`);
66
+ throw new AuthError(`Login failed: ${response.status} ${response.statusText}`, response.status, response);
64
67
  }
65
- const body = yield response.json();
68
+ const body = yield response.json().catch(() => ({}));
66
69
  // Parse response
67
- const bundle = this.config.parseLogin
68
- ? this.config.parseLogin(body)
69
- : autoDetectFields(body, this.config.fields);
70
+ let bundle;
71
+ try {
72
+ bundle = this.config.parseLogin
73
+ ? this.config.parseLogin(body)
74
+ : autoDetectFields(body, this.config.fields);
75
+ }
76
+ catch (error) {
77
+ throw new AuthError(`Invalid login response: ${error.message}`, response.status, response);
78
+ }
70
79
  // Store in cookies
71
80
  storeTokens(ctx, bundle, this.config.cookies);
81
+ // Call onLogin callback if provided
82
+ if (this.config.onLogin) {
83
+ yield this.config.onLogin(bundle, body, ctx);
84
+ }
72
85
  return bundle;
73
86
  });
74
87
  }
@@ -77,38 +90,54 @@ export class TokenManager {
77
90
  */
78
91
  refresh(ctx, refreshToken) {
79
92
  return __awaiter(this, void 0, void 0, function* () {
80
- const url = this.baseURL + this.config.refresh;
81
93
  try {
82
- const response = yield fetch(url, {
83
- method: 'POST',
84
- headers: { 'Content-Type': 'application/json' },
85
- body: JSON.stringify({ refreshToken }),
86
- });
87
- if (!response.ok) {
88
- // 401/403 = invalid refresh token
89
- if (response.status === 401 || response.status === 403) {
90
- clearTokens(ctx, this.config.cookies);
91
- return null;
92
- }
93
- throw new Error(`Refresh failed: ${response.status} ${response.statusText}`);
94
+ return yield this.performRefresh(ctx, refreshToken);
95
+ }
96
+ catch (error) {
97
+ clearTokens(ctx, this.config.cookies);
98
+ throw error;
99
+ }
100
+ });
101
+ }
102
+ /**
103
+ * Internal refresh implementation
104
+ */
105
+ performRefresh(ctx, refreshToken) {
106
+ return __awaiter(this, void 0, void 0, function* () {
107
+ const url = this.baseURL + this.config.refresh;
108
+ const response = yield fetch(url, {
109
+ method: 'POST',
110
+ headers: { 'Content-Type': 'application/json' },
111
+ body: JSON.stringify({ refreshToken }),
112
+ }).catch(error => {
113
+ throw new AuthError(`Refresh request failed: ${error.message}`);
114
+ });
115
+ if (!response.ok) {
116
+ // 401/403 = invalid refresh token
117
+ if (response.status === 401 || response.status === 403) {
118
+ clearTokens(ctx, this.config.cookies);
119
+ return null;
94
120
  }
95
- const body = yield response.json();
96
- // Parse response
97
- const bundle = this.config.parseRefresh
121
+ throw new AuthError(`Refresh failed: ${response.status} ${response.statusText}`, response.status, response);
122
+ }
123
+ const body = yield response.json().catch(() => ({}));
124
+ // Parse response
125
+ let bundle;
126
+ try {
127
+ bundle = this.config.parseRefresh
98
128
  ? this.config.parseRefresh(body)
99
129
  : autoDetectFields(body, this.config.fields);
100
- // Validate bundle
101
- if (!bundle.accessToken || !bundle.refreshToken || !bundle.accessExpiresAt) {
102
- throw new Error('Invalid token bundle returned from refresh endpoint');
103
- }
104
- // Store new tokens
105
- storeTokens(ctx, bundle, this.config.cookies);
106
- return bundle;
107
130
  }
108
131
  catch (error) {
109
- clearTokens(ctx, this.config.cookies);
110
- throw error;
132
+ throw new AuthError(`Invalid refresh response: ${error.message}`, response.status, response);
133
+ }
134
+ // Validate bundle
135
+ if (!bundle.accessToken || !bundle.refreshToken || !bundle.accessExpiresAt) {
136
+ throw new AuthError('Invalid token bundle returned from refresh endpoint', response.status, response);
111
137
  }
138
+ // Store new tokens
139
+ storeTokens(ctx, bundle, this.config.cookies);
140
+ return bundle;
112
141
  });
113
142
  }
114
143
  /**
package/dist/index.cjs CHANGED
@@ -422,17 +422,29 @@ class TokenManager {
422
422
  method: 'POST',
423
423
  headers: { 'Content-Type': 'application/json' },
424
424
  body: JSON.stringify(credentials),
425
+ }).catch(error => {
426
+ throw new AuthError(`Login request failed: ${error.message}`);
425
427
  });
426
428
  if (!response.ok) {
427
- throw new Error(`Login failed: ${response.status} ${response.statusText}`);
429
+ throw new AuthError(`Login failed: ${response.status} ${response.statusText}`, response.status, response);
428
430
  }
429
- const body = yield response.json();
431
+ const body = yield response.json().catch(() => ({}));
430
432
  // Parse response
431
- const bundle = this.config.parseLogin
432
- ? this.config.parseLogin(body)
433
- : autoDetectFields(body, this.config.fields);
433
+ let bundle;
434
+ try {
435
+ bundle = this.config.parseLogin
436
+ ? this.config.parseLogin(body)
437
+ : autoDetectFields(body, this.config.fields);
438
+ }
439
+ catch (error) {
440
+ throw new AuthError(`Invalid login response: ${error.message}`, response.status, response);
441
+ }
434
442
  // Store in cookies
435
443
  storeTokens(ctx, bundle, this.config.cookies);
444
+ // Call onLogin callback if provided
445
+ if (this.config.onLogin) {
446
+ yield this.config.onLogin(bundle, body, ctx);
447
+ }
436
448
  return bundle;
437
449
  });
438
450
  }
@@ -441,38 +453,54 @@ class TokenManager {
441
453
  */
442
454
  refresh(ctx, refreshToken) {
443
455
  return __awaiter(this, void 0, void 0, function* () {
444
- const url = this.baseURL + this.config.refresh;
445
456
  try {
446
- const response = yield fetch(url, {
447
- method: 'POST',
448
- headers: { 'Content-Type': 'application/json' },
449
- body: JSON.stringify({ refreshToken }),
450
- });
451
- if (!response.ok) {
452
- // 401/403 = invalid refresh token
453
- if (response.status === 401 || response.status === 403) {
454
- clearTokens(ctx, this.config.cookies);
455
- return null;
456
- }
457
- throw new Error(`Refresh failed: ${response.status} ${response.statusText}`);
457
+ return yield this.performRefresh(ctx, refreshToken);
458
+ }
459
+ catch (error) {
460
+ clearTokens(ctx, this.config.cookies);
461
+ throw error;
462
+ }
463
+ });
464
+ }
465
+ /**
466
+ * Internal refresh implementation
467
+ */
468
+ performRefresh(ctx, refreshToken) {
469
+ return __awaiter(this, void 0, void 0, function* () {
470
+ const url = this.baseURL + this.config.refresh;
471
+ const response = yield fetch(url, {
472
+ method: 'POST',
473
+ headers: { 'Content-Type': 'application/json' },
474
+ body: JSON.stringify({ refreshToken }),
475
+ }).catch(error => {
476
+ throw new AuthError(`Refresh request failed: ${error.message}`);
477
+ });
478
+ if (!response.ok) {
479
+ // 401/403 = invalid refresh token
480
+ if (response.status === 401 || response.status === 403) {
481
+ clearTokens(ctx, this.config.cookies);
482
+ return null;
458
483
  }
459
- const body = yield response.json();
460
- // Parse response
461
- const bundle = this.config.parseRefresh
484
+ throw new AuthError(`Refresh failed: ${response.status} ${response.statusText}`, response.status, response);
485
+ }
486
+ const body = yield response.json().catch(() => ({}));
487
+ // Parse response
488
+ let bundle;
489
+ try {
490
+ bundle = this.config.parseRefresh
462
491
  ? this.config.parseRefresh(body)
463
492
  : autoDetectFields(body, this.config.fields);
464
- // Validate bundle
465
- if (!bundle.accessToken || !bundle.refreshToken || !bundle.accessExpiresAt) {
466
- throw new Error('Invalid token bundle returned from refresh endpoint');
467
- }
468
- // Store new tokens
469
- storeTokens(ctx, bundle, this.config.cookies);
470
- return bundle;
471
493
  }
472
494
  catch (error) {
473
- clearTokens(ctx, this.config.cookies);
474
- throw error;
495
+ throw new AuthError(`Invalid refresh response: ${error.message}`, response.status, response);
496
+ }
497
+ // Validate bundle
498
+ if (!bundle.accessToken || !bundle.refreshToken || !bundle.accessExpiresAt) {
499
+ throw new AuthError('Invalid token bundle returned from refresh endpoint', response.status, response);
475
500
  }
501
+ // Store new tokens
502
+ storeTokens(ctx, bundle, this.config.cookies);
503
+ return bundle;
476
504
  });
477
505
  }
478
506
  /**