astro-tokenkit 1.0.7 → 1.0.9
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 +13 -2
- package/dist/auth/manager.d.ts +2 -2
- package/dist/auth/manager.js +3 -3
- package/dist/client/client.d.ts +2 -2
- package/dist/client/client.js +12 -2
- package/dist/index.cjs +22 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +15 -4
- package/dist/index.js +22 -5
- package/dist/index.js.map +1 -1
- package/dist/middleware.js +7 -0
- package/dist/types.d.ts +13 -2
- package/package.json +1 -1
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
|
```
|
package/dist/auth/manager.d.ts
CHANGED
|
@@ -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
|
*/
|
package/dist/auth/manager.js
CHANGED
|
@@ -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 (
|
|
83
|
-
yield
|
|
82
|
+
if (onLogin) {
|
|
83
|
+
yield onLogin(bundle, body, ctx);
|
|
84
84
|
}
|
|
85
85
|
return bundle;
|
|
86
86
|
});
|
package/dist/client/client.d.ts
CHANGED
|
@@ -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,
|
|
68
|
+
login(credentials: any, options?: LoginOptions | TokenKitContext): Promise<void>;
|
|
69
69
|
/**
|
|
70
70
|
* Logout
|
|
71
71
|
*/
|
package/dist/client/client.js
CHANGED
|
@@ -283,13 +283,23 @@ export class APIClient {
|
|
|
283
283
|
/**
|
|
284
284
|
* Login
|
|
285
285
|
*/
|
|
286
|
-
login(credentials,
|
|
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 (
|
|
446
|
-
yield
|
|
445
|
+
if (onLogin) {
|
|
446
|
+
yield onLogin(bundle, body, ctx);
|
|
447
447
|
}
|
|
448
448
|
return bundle;
|
|
449
449
|
});
|
|
@@ -748,6 +748,13 @@ function createMiddleware() {
|
|
|
748
748
|
// We skip runWithContext to avoid nesting ALS.run() unnecessarily,
|
|
749
749
|
// UNLESS a custom runWithContext is provided.
|
|
750
750
|
if (config.getContextStore && !config.runWithContext) {
|
|
751
|
+
const storage = config.getContextStore();
|
|
752
|
+
if (storage) {
|
|
753
|
+
storage.cookies = ctx.cookies;
|
|
754
|
+
}
|
|
755
|
+
else {
|
|
756
|
+
console.error("[TokenKit] getContextStore returned null or undefined");
|
|
757
|
+
}
|
|
751
758
|
return runLogic();
|
|
752
759
|
}
|
|
753
760
|
const runner = (_a = config.runWithContext) !== null && _a !== void 0 ? _a : runWithContext;
|
|
@@ -1023,13 +1030,23 @@ class APIClient {
|
|
|
1023
1030
|
/**
|
|
1024
1031
|
* Login
|
|
1025
1032
|
*/
|
|
1026
|
-
login(credentials,
|
|
1033
|
+
login(credentials, options) {
|
|
1027
1034
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1028
1035
|
if (!this.tokenManager) {
|
|
1029
1036
|
throw new Error('Auth is not configured for this client');
|
|
1030
1037
|
}
|
|
1038
|
+
let ctx;
|
|
1039
|
+
let onLogin;
|
|
1040
|
+
if (options && 'cookies' in options) {
|
|
1041
|
+
ctx = options;
|
|
1042
|
+
}
|
|
1043
|
+
else if (options) {
|
|
1044
|
+
const opt = options;
|
|
1045
|
+
ctx = opt.ctx;
|
|
1046
|
+
onLogin = opt.onLogin;
|
|
1047
|
+
}
|
|
1031
1048
|
const context = getContextStore(ctx);
|
|
1032
|
-
yield this.tokenManager.login(context, credentials);
|
|
1049
|
+
yield this.tokenManager.login(context, credentials, onLogin);
|
|
1033
1050
|
});
|
|
1034
1051
|
}
|
|
1035
1052
|
/**
|