astro-tokenkit 1.0.7 → 1.0.8

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/README.md CHANGED
@@ -128,11 +128,17 @@ const specializedClient = createClient({
128
128
  | `fields` | `FieldMapping` | Custom mapping for token fields in API responses. |
129
129
  | `parseLogin` | `Function` | Custom parser for login response: `(body: any) => TokenBundle`. |
130
130
  | `parseRefresh`| `Function` | Custom parser for refresh response: `(body: any) => TokenBundle`. |
131
- | `onLogin` | `Function` | Callback after login: `(bundle, body, ctx) => void`. |
132
131
  | `injectToken` | `Function` | Custom token injection: `(token: string) => string` (default: Bearer). |
133
132
  | `cookies` | `CookieConfig` | Configuration for auth cookies. |
134
133
  | `policy` | `RefreshPolicy` | Strategy for when to trigger token refresh. |
135
134
 
135
+ ### Login Options
136
+
137
+ | Property | Type | Description |
138
+ | :--- | :--- | :--- |
139
+ | `ctx` | `TokenKitContext` | Optional Astro context. |
140
+ | `onLogin` | `Function` | Callback after successful login: `(bundle, body, ctx) => void`. |
141
+
136
142
  ## Advanced Usage
137
143
 
138
144
  ### Manual Context
@@ -163,7 +169,12 @@ const api = createClient({
163
169
 
164
170
  ```typescript
165
171
  // In an API route or server-side component
166
- await api.login({ username, password });
172
+ await api.login({ username, password }, {
173
+ onLogin: (bundle, body, ctx) => {
174
+ // Post-login logic (e.g., sync session to another store)
175
+ console.log('User logged in!', bundle.sessionPayload);
176
+ }
177
+ });
167
178
 
168
179
  await api.logout();
169
180
  ```
@@ -1,4 +1,4 @@
1
- import type { TokenBundle, Session, AuthConfig, TokenKitContext } from '../types';
1
+ import type { TokenBundle, Session, AuthConfig, TokenKitContext, OnLoginCallback } from '../types';
2
2
  /**
3
3
  * Token Manager handles all token operations
4
4
  */
@@ -10,7 +10,7 @@ export declare class TokenManager {
10
10
  /**
11
11
  * Perform login
12
12
  */
13
- login(ctx: TokenKitContext, credentials: any): Promise<TokenBundle>;
13
+ login(ctx: TokenKitContext, credentials: any, onLogin?: OnLoginCallback): Promise<TokenBundle>;
14
14
  /**
15
15
  * Perform token refresh
16
16
  */
@@ -52,7 +52,7 @@ export class TokenManager {
52
52
  /**
53
53
  * Perform login
54
54
  */
55
- login(ctx, credentials) {
55
+ login(ctx, credentials, onLogin) {
56
56
  return __awaiter(this, void 0, void 0, function* () {
57
57
  const url = this.baseURL + this.config.login;
58
58
  const response = yield fetch(url, {
@@ -79,8 +79,8 @@ export class TokenManager {
79
79
  // Store in cookies
80
80
  storeTokens(ctx, bundle, this.config.cookies);
81
81
  // Call onLogin callback if provided
82
- if (this.config.onLogin) {
83
- yield this.config.onLogin(bundle, body, ctx);
82
+ if (onLogin) {
83
+ yield onLogin(bundle, body, ctx);
84
84
  }
85
85
  return bundle;
86
86
  });
@@ -1,4 +1,4 @@
1
- import type { ClientConfig, RequestConfig, RequestOptions, Session, TokenKitContext, TokenKitConfig } from '../types';
1
+ import type { ClientConfig, RequestConfig, RequestOptions, Session, TokenKitContext, TokenKitConfig, LoginOptions } from '../types';
2
2
  import { TokenManager } from '../auth/manager';
3
3
  /**
4
4
  * API Client
@@ -65,7 +65,7 @@ export declare class APIClient {
65
65
  /**
66
66
  * Login
67
67
  */
68
- login(credentials: any, ctx?: TokenKitContext): Promise<void>;
68
+ login(credentials: any, options?: LoginOptions | TokenKitContext): Promise<void>;
69
69
  /**
70
70
  * Logout
71
71
  */
@@ -283,13 +283,23 @@ export class APIClient {
283
283
  /**
284
284
  * Login
285
285
  */
286
- login(credentials, ctx) {
286
+ login(credentials, options) {
287
287
  return __awaiter(this, void 0, void 0, function* () {
288
288
  if (!this.tokenManager) {
289
289
  throw new Error('Auth is not configured for this client');
290
290
  }
291
+ let ctx;
292
+ let onLogin;
293
+ if (options && 'cookies' in options) {
294
+ ctx = options;
295
+ }
296
+ else if (options) {
297
+ const opt = options;
298
+ ctx = opt.ctx;
299
+ onLogin = opt.onLogin;
300
+ }
291
301
  const context = getContextStore(ctx);
292
- yield this.tokenManager.login(context, credentials);
302
+ yield this.tokenManager.login(context, credentials, onLogin);
293
303
  });
294
304
  }
295
305
  /**
package/dist/index.cjs CHANGED
@@ -415,7 +415,7 @@ class TokenManager {
415
415
  /**
416
416
  * Perform login
417
417
  */
418
- login(ctx, credentials) {
418
+ login(ctx, credentials, onLogin) {
419
419
  return __awaiter(this, void 0, void 0, function* () {
420
420
  const url = this.baseURL + this.config.login;
421
421
  const response = yield fetch(url, {
@@ -442,8 +442,8 @@ class TokenManager {
442
442
  // Store in cookies
443
443
  storeTokens(ctx, bundle, this.config.cookies);
444
444
  // Call onLogin callback if provided
445
- if (this.config.onLogin) {
446
- yield this.config.onLogin(bundle, body, ctx);
445
+ if (onLogin) {
446
+ yield onLogin(bundle, body, ctx);
447
447
  }
448
448
  return bundle;
449
449
  });
@@ -1023,13 +1023,23 @@ class APIClient {
1023
1023
  /**
1024
1024
  * Login
1025
1025
  */
1026
- login(credentials, ctx) {
1026
+ login(credentials, options) {
1027
1027
  return __awaiter(this, void 0, void 0, function* () {
1028
1028
  if (!this.tokenManager) {
1029
1029
  throw new Error('Auth is not configured for this client');
1030
1030
  }
1031
+ let ctx;
1032
+ let onLogin;
1033
+ if (options && 'cookies' in options) {
1034
+ ctx = options;
1035
+ }
1036
+ else if (options) {
1037
+ const opt = options;
1038
+ ctx = opt.ctx;
1039
+ onLogin = opt.onLogin;
1040
+ }
1031
1041
  const context = getContextStore(ctx);
1032
- yield this.tokenManager.login(context, credentials);
1042
+ yield this.tokenManager.login(context, credentials, onLogin);
1033
1043
  });
1034
1044
  }
1035
1045
  /**