@oxog/authkeeper 1.3.0 → 1.4.0
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/CHANGELOG.md +24 -0
- package/README.md +80 -2
- package/dist/auth-source-BaZ_-Fu7.d.cts +24 -0
- package/dist/auth-source-Cm0R6C6I.d.ts +24 -0
- package/dist/chunk-62XK5QLP.cjs +19 -0
- package/dist/chunk-62XK5QLP.cjs.map +1 -0
- package/dist/chunk-CYCWWUCJ.js +17 -0
- package/dist/chunk-CYCWWUCJ.js.map +1 -0
- package/dist/{chunk-7KKUG6IF.cjs → chunk-EXV2RG6T.cjs} +71 -45
- package/dist/chunk-EXV2RG6T.cjs.map +1 -0
- package/dist/{chunk-3YDUKFDO.js → chunk-KE7V52DI.js} +61 -34
- package/dist/chunk-KE7V52DI.js.map +1 -0
- package/dist/chunk-KLT44YR2.cjs +69 -0
- package/dist/chunk-KLT44YR2.cjs.map +1 -0
- package/dist/chunk-SUGFPZ6O.js +66 -0
- package/dist/chunk-SUGFPZ6O.js.map +1 -0
- package/dist/index-BF4j_buH.d.ts +488 -0
- package/dist/index-CuvfPscu.d.cts +488 -0
- package/dist/index.cjs +69 -54
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +9 -3
- package/dist/index.d.ts +9 -3
- package/dist/index.js +21 -5
- package/dist/index.js.map +1 -1
- package/dist/plugins/index.cjs +15 -14
- package/dist/plugins/index.d.cts +2 -1
- package/dist/plugins/index.d.ts +2 -1
- package/dist/plugins/index.js +2 -1
- package/dist/react/index.cjs +40 -3
- package/dist/react/index.cjs.map +1 -1
- package/dist/react/index.d.cts +42 -5
- package/dist/react/index.d.ts +42 -5
- package/dist/react/index.js +38 -3
- package/dist/react/index.js.map +1 -1
- package/dist/svelte/index.cjs +37 -3
- package/dist/svelte/index.cjs.map +1 -1
- package/dist/svelte/index.d.cts +23 -5
- package/dist/svelte/index.d.ts +23 -5
- package/dist/svelte/index.js +37 -3
- package/dist/svelte/index.js.map +1 -1
- package/dist/types-Bmo8S8O4.d.cts +421 -0
- package/dist/types-Bmo8S8O4.d.ts +421 -0
- package/dist/vue/index.cjs +46 -3
- package/dist/vue/index.cjs.map +1 -1
- package/dist/vue/index.d.cts +45 -5
- package/dist/vue/index.d.ts +45 -5
- package/dist/vue/index.js +43 -3
- package/dist/vue/index.js.map +1 -1
- package/package.json +10 -5
- package/dist/chunk-3YDUKFDO.js.map +0 -1
- package/dist/chunk-7KKUG6IF.cjs.map +0 -1
- package/dist/index-B05Msx7M.d.cts +0 -898
- package/dist/index-B05Msx7M.d.ts +0 -898
|
@@ -0,0 +1,421 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AuthKeeper Type Definitions
|
|
3
|
+
*
|
|
4
|
+
* Complete type system for the AuthKeeper library.
|
|
5
|
+
* All interfaces and types are exported for use by consumers.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Token set returned from authentication/refresh operations
|
|
9
|
+
*/
|
|
10
|
+
interface TokenSet {
|
|
11
|
+
/** JWT access token */
|
|
12
|
+
accessToken: string;
|
|
13
|
+
/** Optional refresh token for obtaining new access tokens */
|
|
14
|
+
refreshToken?: string;
|
|
15
|
+
/** Token expiration in seconds from now */
|
|
16
|
+
expiresIn?: number;
|
|
17
|
+
/** Token expiration as Unix timestamp (seconds) */
|
|
18
|
+
expiresAt?: number;
|
|
19
|
+
/** Token type (default: 'Bearer') */
|
|
20
|
+
tokenType?: string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Standard JWT payload with common claims
|
|
24
|
+
*/
|
|
25
|
+
interface TokenPayload {
|
|
26
|
+
/** Subject - usually the user ID */
|
|
27
|
+
sub?: string;
|
|
28
|
+
/** Issuer - who created the token */
|
|
29
|
+
iss?: string;
|
|
30
|
+
/** Audience - who the token is intended for */
|
|
31
|
+
aud?: string | string[];
|
|
32
|
+
/** Expiration time (Unix timestamp in seconds) */
|
|
33
|
+
exp?: number;
|
|
34
|
+
/** Not before time (Unix timestamp in seconds) */
|
|
35
|
+
nbf?: number;
|
|
36
|
+
/** Issued at time (Unix timestamp in seconds) */
|
|
37
|
+
iat?: number;
|
|
38
|
+
/** JWT ID - unique identifier */
|
|
39
|
+
jti?: string;
|
|
40
|
+
/** User email address */
|
|
41
|
+
email?: string;
|
|
42
|
+
/** User display name */
|
|
43
|
+
name?: string;
|
|
44
|
+
/** User roles */
|
|
45
|
+
roles?: string[];
|
|
46
|
+
/** User permissions */
|
|
47
|
+
permissions?: string[];
|
|
48
|
+
/** OAuth2 scope */
|
|
49
|
+
scope?: string;
|
|
50
|
+
[key: string]: unknown;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* JWT header information
|
|
54
|
+
*/
|
|
55
|
+
interface TokenHeader {
|
|
56
|
+
/** Algorithm used to sign the token */
|
|
57
|
+
alg: string;
|
|
58
|
+
/** Token type (usually 'JWT') */
|
|
59
|
+
typ?: string;
|
|
60
|
+
/** Key ID used to sign the token */
|
|
61
|
+
kid?: string;
|
|
62
|
+
[key: string]: unknown;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Complete token information including decoded parts
|
|
66
|
+
*/
|
|
67
|
+
interface TokenInfo {
|
|
68
|
+
/** Raw JWT string */
|
|
69
|
+
raw: string;
|
|
70
|
+
/** Decoded payload */
|
|
71
|
+
payload: TokenPayload;
|
|
72
|
+
/** Decoded header */
|
|
73
|
+
header: TokenHeader;
|
|
74
|
+
/** When the token expires (null if no expiry) */
|
|
75
|
+
expiresAt: Date | null;
|
|
76
|
+
/** Whether the token is expired */
|
|
77
|
+
isExpired: boolean;
|
|
78
|
+
/** Time until expiry in milliseconds (null if no expiry) */
|
|
79
|
+
expiresIn: number | null;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Stored token data with metadata
|
|
83
|
+
*/
|
|
84
|
+
interface StoredTokens {
|
|
85
|
+
/** Access token */
|
|
86
|
+
accessToken: string;
|
|
87
|
+
/** Refresh token (null if not available) */
|
|
88
|
+
refreshToken: string | null;
|
|
89
|
+
/** Token type (e.g., 'Bearer') */
|
|
90
|
+
tokenType: string;
|
|
91
|
+
/** Expiration timestamp in milliseconds (null if no expiry) */
|
|
92
|
+
expiresAt: number | null;
|
|
93
|
+
/** When tokens were set (timestamp in milliseconds) */
|
|
94
|
+
setAt: number;
|
|
95
|
+
/** Number of times tokens have been refreshed */
|
|
96
|
+
refreshCount: number;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Error codes for authentication errors
|
|
100
|
+
*/
|
|
101
|
+
type AuthErrorCode = 'TOKEN_EXPIRED' | 'TOKEN_INVALID' | 'TOKEN_DECODE_FAILED' | 'REFRESH_FAILED' | 'REFRESH_TOKEN_MISSING' | 'STORAGE_ERROR' | 'NETWORK_ERROR' | 'UNAUTHORIZED' | 'FORBIDDEN' | 'OAUTH_ERROR' | 'PKCE_ERROR' | 'CALLBACK_ERROR' | 'CONFIG_ERROR';
|
|
102
|
+
/**
|
|
103
|
+
* Options for creating an AuthError
|
|
104
|
+
*/
|
|
105
|
+
interface AuthErrorOptions {
|
|
106
|
+
/** Additional context about the error */
|
|
107
|
+
context?: Record<string, unknown>;
|
|
108
|
+
/** Original error that caused this error */
|
|
109
|
+
cause?: Error;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Custom error class for authentication errors
|
|
113
|
+
*/
|
|
114
|
+
declare class AuthError extends Error {
|
|
115
|
+
/** Error code identifying the type of error */
|
|
116
|
+
code: AuthErrorCode;
|
|
117
|
+
/** Additional context about the error */
|
|
118
|
+
context?: Record<string, unknown>;
|
|
119
|
+
/** Original error that caused this error */
|
|
120
|
+
cause?: Error;
|
|
121
|
+
constructor(code: AuthErrorCode, message: string, options?: AuthErrorOptions);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* All possible event types
|
|
125
|
+
*/
|
|
126
|
+
type EventType = 'login' | 'logout' | 'refresh' | 'expired' | 'error' | 'storage-change' | 'tab-sync';
|
|
127
|
+
/**
|
|
128
|
+
* Login event - fired when user logs in
|
|
129
|
+
*/
|
|
130
|
+
interface LoginEvent {
|
|
131
|
+
type: 'login';
|
|
132
|
+
tokens: TokenSet;
|
|
133
|
+
timestamp: number;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Logout event - fired when user logs out
|
|
137
|
+
*/
|
|
138
|
+
interface LogoutEvent {
|
|
139
|
+
type: 'logout';
|
|
140
|
+
reason: 'manual' | 'expired' | 'error' | 'tab-sync';
|
|
141
|
+
timestamp: number;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Refresh event - fired when tokens are refreshed
|
|
145
|
+
*/
|
|
146
|
+
interface RefreshEvent {
|
|
147
|
+
type: 'refresh';
|
|
148
|
+
tokens: TokenSet;
|
|
149
|
+
previousExpiresAt: Date | null;
|
|
150
|
+
newExpiresAt: Date | null;
|
|
151
|
+
timestamp: number;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Expired event - fired when token expires
|
|
155
|
+
*/
|
|
156
|
+
interface ExpiredEvent {
|
|
157
|
+
type: 'expired';
|
|
158
|
+
expiredAt: Date;
|
|
159
|
+
timestamp: number;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Error event - fired when an error occurs
|
|
163
|
+
*/
|
|
164
|
+
interface ErrorEvent {
|
|
165
|
+
type: 'error';
|
|
166
|
+
error: AuthError;
|
|
167
|
+
context: 'refresh' | 'storage' | 'decode' | 'network' | 'plugin';
|
|
168
|
+
timestamp: number;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Storage change event - fired when storage changes
|
|
172
|
+
*/
|
|
173
|
+
interface StorageChangeEvent {
|
|
174
|
+
type: 'storage-change';
|
|
175
|
+
key: string;
|
|
176
|
+
oldValue: string | null;
|
|
177
|
+
newValue: string | null;
|
|
178
|
+
timestamp: number;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Tab sync event - fired when syncing across tabs
|
|
182
|
+
*/
|
|
183
|
+
interface TabSyncEvent {
|
|
184
|
+
type: 'tab-sync';
|
|
185
|
+
action: 'login' | 'logout' | 'refresh';
|
|
186
|
+
sourceTabId: string;
|
|
187
|
+
timestamp: number;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Union of all event types
|
|
191
|
+
*/
|
|
192
|
+
type AuthEvent = LoginEvent | LogoutEvent | RefreshEvent | ExpiredEvent | ErrorEvent | StorageChangeEvent | TabSyncEvent;
|
|
193
|
+
/**
|
|
194
|
+
* Event handler function type
|
|
195
|
+
*/
|
|
196
|
+
type EventHandler<E extends EventType> = (event: Extract<AuthEvent, {
|
|
197
|
+
type: E;
|
|
198
|
+
}>) => void;
|
|
199
|
+
/**
|
|
200
|
+
* Unsubscribe function returned from event handlers
|
|
201
|
+
*/
|
|
202
|
+
type Unsubscribe = () => void;
|
|
203
|
+
/**
|
|
204
|
+
* Storage adapter interface for token persistence
|
|
205
|
+
*/
|
|
206
|
+
interface StorageAdapter {
|
|
207
|
+
/** Get value by key */
|
|
208
|
+
get(key: string): string | null;
|
|
209
|
+
/** Set value by key */
|
|
210
|
+
set(key: string, value: string): void;
|
|
211
|
+
/** Remove value by key */
|
|
212
|
+
remove(key: string): void;
|
|
213
|
+
/** Clear all values */
|
|
214
|
+
clear(): void;
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Built-in storage types
|
|
218
|
+
*/
|
|
219
|
+
type StorageType = 'memory' | 'localStorage' | 'sessionStorage' | 'cookie';
|
|
220
|
+
/**
|
|
221
|
+
* Plugin interface for extending AuthKeeper
|
|
222
|
+
*/
|
|
223
|
+
interface Plugin {
|
|
224
|
+
/** Unique plugin name */
|
|
225
|
+
name: string;
|
|
226
|
+
/** Plugin version */
|
|
227
|
+
version: string;
|
|
228
|
+
/** Plugin type */
|
|
229
|
+
type: 'core' | 'optional';
|
|
230
|
+
/** Install plugin and return API */
|
|
231
|
+
install(kernel: AuthKeeper): any;
|
|
232
|
+
/** Uninstall plugin and cleanup */
|
|
233
|
+
uninstall?(): void | Promise<void>;
|
|
234
|
+
/** Reserved lifecycle hooks; not invoked by the kernel yet */
|
|
235
|
+
hooks?: {
|
|
236
|
+
/** Called before tokens are set */
|
|
237
|
+
beforeSetTokens?: (tokens: TokenSet) => TokenSet | false;
|
|
238
|
+
/** Called after tokens are set */
|
|
239
|
+
afterSetTokens?: (tokens: TokenSet) => void;
|
|
240
|
+
/** Called before refresh */
|
|
241
|
+
beforeRefresh?: () => boolean;
|
|
242
|
+
/** Called after refresh */
|
|
243
|
+
afterRefresh?: (tokens: TokenSet) => void;
|
|
244
|
+
/** Called before logout */
|
|
245
|
+
beforeLogout?: () => boolean;
|
|
246
|
+
/** Called after logout */
|
|
247
|
+
afterLogout?: () => void;
|
|
248
|
+
/** Called when token expires */
|
|
249
|
+
onExpired?: () => void;
|
|
250
|
+
/** Called on error */
|
|
251
|
+
onError?: (error: AuthError) => void;
|
|
252
|
+
/** Called before request */
|
|
253
|
+
beforeRequest?: (request: Request) => Request;
|
|
254
|
+
/** Called after response */
|
|
255
|
+
afterResponse?: (response: Response) => Response;
|
|
256
|
+
};
|
|
257
|
+
/** Plugin API exposed to consumers */
|
|
258
|
+
api?: Record<string, unknown>;
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Plugin information
|
|
262
|
+
*/
|
|
263
|
+
interface PluginInfo {
|
|
264
|
+
name: string;
|
|
265
|
+
version: string;
|
|
266
|
+
type: 'core' | 'optional';
|
|
267
|
+
enabled: boolean;
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Refresh token function type
|
|
271
|
+
*/
|
|
272
|
+
type RefreshTokenFn = (refreshToken: string) => Promise<TokenSet>;
|
|
273
|
+
/**
|
|
274
|
+
* Kernel configuration options
|
|
275
|
+
*/
|
|
276
|
+
interface KernelOptions {
|
|
277
|
+
/**
|
|
278
|
+
* Where tokens are persisted (default: 'memory', i.e. not persisted).
|
|
279
|
+
* 'localStorage' / 'sessionStorage' / 'cookie' fall back to memory where
|
|
280
|
+
* unavailable. Persistent storage is readable by any script on the page.
|
|
281
|
+
*/
|
|
282
|
+
storage?: StorageType | StorageAdapter;
|
|
283
|
+
/** Function to refresh tokens */
|
|
284
|
+
refreshToken: RefreshTokenFn;
|
|
285
|
+
/** Enable automatic token refresh (default: true) */
|
|
286
|
+
autoRefresh?: boolean;
|
|
287
|
+
/** Seconds before expiry to refresh (default: 60) */
|
|
288
|
+
refreshThreshold?: number;
|
|
289
|
+
/**
|
|
290
|
+
* Reserved for multi-tab sync; not implemented yet (currently a no-op).
|
|
291
|
+
* `getOptions().syncTabs` always reports `false`.
|
|
292
|
+
*/
|
|
293
|
+
syncTabs?: boolean;
|
|
294
|
+
/** Error handler */
|
|
295
|
+
onError?: (error: AuthError) => void;
|
|
296
|
+
/** Additional plugins to install */
|
|
297
|
+
plugins?: Plugin[];
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Logout options
|
|
301
|
+
*/
|
|
302
|
+
interface LogoutOptions {
|
|
303
|
+
/** Reserved for multi-tab logout; not implemented yet (currently a no-op) */
|
|
304
|
+
allTabs?: boolean;
|
|
305
|
+
/** Redirect URL after logout */
|
|
306
|
+
redirect?: string;
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Authenticated fetch function
|
|
310
|
+
*/
|
|
311
|
+
type AuthFetch = (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
|
|
312
|
+
/**
|
|
313
|
+
* Fetch interceptor options
|
|
314
|
+
*/
|
|
315
|
+
interface FetchInterceptorOptions {
|
|
316
|
+
/** Header name for auth token (default: 'Authorization') */
|
|
317
|
+
headerName?: string;
|
|
318
|
+
/** Header prefix (default: 'Bearer ') */
|
|
319
|
+
headerPrefix?: string;
|
|
320
|
+
/** Only include auth header for these URLs */
|
|
321
|
+
includeUrls?: (string | RegExp)[];
|
|
322
|
+
/** Exclude auth header for these URLs */
|
|
323
|
+
excludeUrls?: (string | RegExp)[];
|
|
324
|
+
/** Callback on 401 response */
|
|
325
|
+
onUnauthorized?: (response: Response) => void;
|
|
326
|
+
/** Retry on 401 after refresh (default: true) */
|
|
327
|
+
retry401?: boolean;
|
|
328
|
+
/** Max retries (default: 1) */
|
|
329
|
+
maxRetries?: number;
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* OAuth2 login options
|
|
333
|
+
*/
|
|
334
|
+
interface LoginOptions {
|
|
335
|
+
/** OAuth2 scope */
|
|
336
|
+
scope?: string;
|
|
337
|
+
/** OAuth2 state parameter */
|
|
338
|
+
state?: string;
|
|
339
|
+
/** OAuth2 prompt parameter */
|
|
340
|
+
prompt?: 'none' | 'login' | 'consent' | 'select_account';
|
|
341
|
+
/** Login hint (email, etc.) */
|
|
342
|
+
loginHint?: string;
|
|
343
|
+
/** Additional OAuth2 parameters */
|
|
344
|
+
extraParams?: Record<string, string>;
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* User info from OIDC userinfo endpoint
|
|
348
|
+
*/
|
|
349
|
+
interface UserInfo {
|
|
350
|
+
sub: string;
|
|
351
|
+
email?: string;
|
|
352
|
+
name?: string;
|
|
353
|
+
picture?: string;
|
|
354
|
+
[key: string]: unknown;
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* Main AuthKeeper interface - the kernel
|
|
358
|
+
*/
|
|
359
|
+
interface AuthKeeper {
|
|
360
|
+
/** Set authentication tokens */
|
|
361
|
+
setTokens(tokens: TokenSet): void;
|
|
362
|
+
/** Get access token */
|
|
363
|
+
getAccessToken(): string | null;
|
|
364
|
+
/** Get refresh token */
|
|
365
|
+
getRefreshToken(): string | null;
|
|
366
|
+
/** Clear all tokens */
|
|
367
|
+
clearTokens(): void;
|
|
368
|
+
/** Check if user is authenticated */
|
|
369
|
+
isAuthenticated(): boolean;
|
|
370
|
+
/** Check if token is expired */
|
|
371
|
+
isExpired(): boolean;
|
|
372
|
+
/** Get token expiration date */
|
|
373
|
+
getExpiresAt(): Date | null;
|
|
374
|
+
/** Get time until expiry in milliseconds */
|
|
375
|
+
getTimeUntilExpiry(): number | null;
|
|
376
|
+
/** Decode token payload */
|
|
377
|
+
decode<T = TokenPayload>(): T | null;
|
|
378
|
+
/** Get specific claim from token */
|
|
379
|
+
getClaim<K extends keyof TokenPayload>(key: K): TokenPayload[K] | undefined;
|
|
380
|
+
/** Get multiple claims from token */
|
|
381
|
+
getClaims<K extends keyof TokenPayload>(keys: K[]): Pick<TokenPayload, K>;
|
|
382
|
+
/** Manually refresh tokens */
|
|
383
|
+
refresh(): Promise<TokenSet>;
|
|
384
|
+
/** Schedule automatic refresh */
|
|
385
|
+
scheduleRefresh(): void;
|
|
386
|
+
/** Cancel scheduled refresh */
|
|
387
|
+
cancelScheduledRefresh(): void;
|
|
388
|
+
/** Logout user */
|
|
389
|
+
logout(options?: LogoutOptions): void;
|
|
390
|
+
/** Create authenticated fetch function */
|
|
391
|
+
createFetch(options?: FetchInterceptorOptions): AuthFetch;
|
|
392
|
+
/** Wrap global fetch with auth */
|
|
393
|
+
wrapFetch(fetch: typeof globalThis.fetch): void;
|
|
394
|
+
/**
|
|
395
|
+
* Register and install a plugin. Rejects if installation fails; the failure
|
|
396
|
+
* is also emitted as an 'error' event with context 'plugin'.
|
|
397
|
+
*/
|
|
398
|
+
register(plugin: Plugin): Promise<void>;
|
|
399
|
+
/** Unregister a plugin */
|
|
400
|
+
unregister(pluginName: string): Promise<void>;
|
|
401
|
+
/** Get plugin by name */
|
|
402
|
+
getPlugin<P extends Plugin>(name: string): P | undefined;
|
|
403
|
+
/** List all plugins */
|
|
404
|
+
listPlugins(): PluginInfo[];
|
|
405
|
+
/** Emit an event */
|
|
406
|
+
emit(event: AuthEvent): void;
|
|
407
|
+
/** Subscribe to events */
|
|
408
|
+
on<E extends EventType>(eventType: E, handler: EventHandler<E>): Unsubscribe;
|
|
409
|
+
/** Unsubscribe from events */
|
|
410
|
+
off<E extends EventType>(eventType: E, handler: EventHandler<E>): void;
|
|
411
|
+
/** Initialize kernel */
|
|
412
|
+
init(): Promise<void>;
|
|
413
|
+
/** Destroy kernel and cleanup */
|
|
414
|
+
destroy(): void;
|
|
415
|
+
/** Update configuration */
|
|
416
|
+
configure(options: Partial<KernelOptions>): void;
|
|
417
|
+
/** Get current configuration */
|
|
418
|
+
getOptions(): KernelOptions;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
export { type AuthKeeper as A, type EventType as E, type FetchInterceptorOptions as F, type KernelOptions as K, type LogoutOptions as L, type Plugin as P, type RefreshEvent as R, type StorageAdapter as S, type TokenSet as T, type Unsubscribe as U, type TokenPayload as a, AuthError as b, type EventHandler as c, type AuthEvent as d, type PluginInfo as e, type AuthFetch as f, type TokenHeader as g, type AuthErrorCode as h, type AuthErrorOptions as i, type ErrorEvent as j, type ExpiredEvent as k, type LoginEvent as l, type LoginOptions as m, type LogoutEvent as n, type RefreshTokenFn as o, type StorageChangeEvent as p, type StorageType as q, type StoredTokens as r, type TabSyncEvent as s, type TokenInfo as t, type UserInfo as u };
|
package/dist/vue/index.cjs
CHANGED
|
@@ -1,8 +1,51 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
var
|
|
3
|
+
var chunk62XK5QLP_cjs = require('../chunk-62XK5QLP.cjs');
|
|
4
|
+
var chunkKLT44YR2_cjs = require('../chunk-KLT44YR2.cjs');
|
|
5
|
+
var vue = require('vue');
|
|
5
6
|
|
|
6
|
-
|
|
7
|
+
var AUTH_KEY = /* @__PURE__ */ Symbol("authkeeper");
|
|
8
|
+
function provideAuth(auth) {
|
|
9
|
+
vue.provide(AUTH_KEY, auth);
|
|
10
|
+
}
|
|
11
|
+
function injectAuth() {
|
|
12
|
+
const auth = vue.inject(AUTH_KEY, null);
|
|
13
|
+
if (!auth) {
|
|
14
|
+
throw new chunk62XK5QLP_cjs.AuthError("CONFIG_ERROR", "No AuthKeeper provided: call provideAuth(auth) in an ancestor");
|
|
15
|
+
}
|
|
16
|
+
return auth;
|
|
17
|
+
}
|
|
18
|
+
function authKeeperPlugin(auth) {
|
|
19
|
+
return {
|
|
20
|
+
install(app) {
|
|
21
|
+
app.provide(AUTH_KEY, auth);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
function useAuth(auth) {
|
|
26
|
+
const instance = auth ?? injectAuth();
|
|
27
|
+
const source = chunkKLT44YR2_cjs.createAuthSource(instance);
|
|
28
|
+
const state = vue.shallowRef(source.getState());
|
|
29
|
+
const stop = source.subscribe(() => {
|
|
30
|
+
state.value = source.getState();
|
|
31
|
+
});
|
|
32
|
+
if (vue.getCurrentScope()) vue.onScopeDispose(stop);
|
|
33
|
+
return {
|
|
34
|
+
isAuthenticated: vue.computed(() => state.value.isAuthenticated),
|
|
35
|
+
accessToken: vue.computed(() => state.value.accessToken),
|
|
36
|
+
user: vue.computed(() => state.value.user),
|
|
37
|
+
expiresAt: vue.computed(() => state.value.expiresAt),
|
|
38
|
+
error: vue.computed(() => state.value.error),
|
|
39
|
+
setTokens: (tokens) => instance.setTokens(tokens),
|
|
40
|
+
logout: (options) => instance.logout(options),
|
|
41
|
+
refresh: () => instance.refresh(),
|
|
42
|
+
stop
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
exports.authKeeperPlugin = authKeeperPlugin;
|
|
47
|
+
exports.injectAuth = injectAuth;
|
|
48
|
+
exports.provideAuth = provideAuth;
|
|
49
|
+
exports.useAuth = useAuth;
|
|
7
50
|
//# sourceMappingURL=index.cjs.map
|
|
8
51
|
//# sourceMappingURL=index.cjs.map
|
package/dist/vue/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/adapters/vue/
|
|
1
|
+
{"version":3,"sources":["../../src/adapters/vue/use-auth.ts"],"names":["provide","inject","AuthError","createAuthSource","shallowRef","getCurrentScope","onScopeDispose","computed"],"mappings":";;;;;;AAkBA,IAAM,QAAA,0BAA4C,YAAY,CAAA;AAMvD,SAAS,YAAY,IAAA,EAAwB;AAClD,EAAAA,WAAA,CAAQ,UAAU,IAAI,CAAA;AACxB;AAMO,SAAS,UAAA,GAAyB;AACvC,EAAA,MAAM,IAAA,GAAOC,UAAA,CAAO,QAAA,EAAU,IAAI,CAAA;AAClC,EAAA,IAAI,CAAC,IAAA,EAAM;AACT,IAAA,MAAM,IAAIC,2BAAA,CAAU,cAAA,EAAgB,+DAA+D,CAAA;AAAA,EACrG;AACA,EAAA,OAAO,IAAA;AACT;AAKO,SAAS,iBAAiB,IAAA,EAA+C;AAC9E,EAAA,OAAO;AAAA,IACL,QAAQ,GAAA,EAAU;AAChB,MAAA,GAAA,CAAI,OAAA,CAAQ,UAAU,IAAI,CAAA;AAAA,IAC5B;AAAA,GACF;AACF;AAyBO,SAAS,QAAQ,IAAA,EAAkC;AACxD,EAAA,MAAM,QAAA,GAAW,QAAQ,UAAA,EAAW;AACpC,EAAA,MAAM,MAAA,GAASC,mCAAiB,QAAQ,CAAA;AACxC,EAAA,MAAM,KAAA,GAAQC,cAAA,CAAW,MAAA,CAAO,QAAA,EAAU,CAAA;AAC1C,EAAA,MAAM,IAAA,GAAO,MAAA,CAAO,SAAA,CAAU,MAAM;AAClC,IAAA,KAAA,CAAM,KAAA,GAAQ,OAAO,QAAA,EAAS;AAAA,EAChC,CAAC,CAAA;AACD,EAAA,IAAIC,mBAAA,EAAgB,EAAGC,kBAAA,CAAe,IAAI,CAAA;AAE1C,EAAA,OAAO;AAAA,IACL,eAAA,EAAiBC,YAAA,CAAS,MAAM,KAAA,CAAM,MAAM,eAAe,CAAA;AAAA,IAC3D,WAAA,EAAaA,YAAA,CAAS,MAAM,KAAA,CAAM,MAAM,WAAW,CAAA;AAAA,IACnD,IAAA,EAAMA,YAAA,CAAS,MAAM,KAAA,CAAM,MAAM,IAAI,CAAA;AAAA,IACrC,SAAA,EAAWA,YAAA,CAAS,MAAM,KAAA,CAAM,MAAM,SAAS,CAAA;AAAA,IAC/C,KAAA,EAAOA,YAAA,CAAS,MAAM,KAAA,CAAM,MAAM,KAAK,CAAA;AAAA,IACvC,SAAA,EAAW,CAAC,MAAA,KAAW,QAAA,CAAS,UAAU,MAAM,CAAA;AAAA,IAChD,MAAA,EAAQ,CAAC,OAAA,KAAY,QAAA,CAAS,OAAO,OAAO,CAAA;AAAA,IAC5C,OAAA,EAAS,MAAM,QAAA,CAAS,OAAA,EAAQ;AAAA,IAChC;AAAA,GACF;AACF","file":"index.cjs","sourcesContent":["/**\n * Vue 3 bindings: provideAuth() / injectAuth() and the useAuth() composable\n */\n\nimport {\n computed,\n getCurrentScope,\n inject,\n onScopeDispose,\n provide,\n shallowRef,\n type App,\n type ComputedRef,\n type InjectionKey,\n} from 'vue'\nimport { AuthError, type AuthKeeper, type LogoutOptions, type TokenPayload, type TokenSet } from '../../types'\nimport { createAuthSource } from '../auth-source'\n\nconst AUTH_KEY: InjectionKey<AuthKeeper> = Symbol('authkeeper')\n\n/**\n * Provide an AuthKeeper instance to the component's descendants\n * (call in `setup()`; for the whole app use `app.use(authKeeperPlugin(auth))`)\n */\nexport function provideAuth(auth: AuthKeeper): void {\n provide(AUTH_KEY, auth)\n}\n\n/**\n * The provided AuthKeeper instance (call in `setup()`)\n * @throws {AuthError} CONFIG_ERROR when none was provided\n */\nexport function injectAuth(): AuthKeeper {\n const auth = inject(AUTH_KEY, null)\n if (!auth) {\n throw new AuthError('CONFIG_ERROR', 'No AuthKeeper provided: call provideAuth(auth) in an ancestor')\n }\n return auth\n}\n\n/**\n * Vue plugin: `app.use(authKeeperPlugin(auth))` provides the instance app-wide\n */\nexport function authKeeperPlugin(auth: AuthKeeper): { install(app: App): void } {\n return {\n install(app: App) {\n app.provide(AUTH_KEY, auth)\n },\n }\n}\n\n/**\n * useAuth() return value: read-only refs plus actions\n */\nexport interface UseAuthReturn {\n isAuthenticated: ComputedRef<boolean>\n accessToken: ComputedRef<string | null>\n user: ComputedRef<TokenPayload | null>\n expiresAt: ComputedRef<Date | null>\n error: ComputedRef<AuthError | null>\n setTokens: (tokens: TokenSet) => void\n logout: (options?: LogoutOptions) => void\n refresh: () => Promise<TokenSet>\n /** Stop updating (done automatically when the component / effect scope is disposed) */\n stop: () => void\n}\n\n/**\n * Reactive auth state. Updates after login, logout, refresh and error events\n * and when the access token expires; stops when the calling component (or\n * effect scope) is disposed.\n *\n * @param auth - Instance to use (default: the provided one)\n */\nexport function useAuth(auth?: AuthKeeper): UseAuthReturn {\n const instance = auth ?? injectAuth()\n const source = createAuthSource(instance)\n const state = shallowRef(source.getState())\n const stop = source.subscribe(() => {\n state.value = source.getState()\n })\n if (getCurrentScope()) onScopeDispose(stop)\n\n return {\n isAuthenticated: computed(() => state.value.isAuthenticated),\n accessToken: computed(() => state.value.accessToken),\n user: computed(() => state.value.user),\n expiresAt: computed(() => state.value.expiresAt),\n error: computed(() => state.value.error),\n setTokens: (tokens) => instance.setTokens(tokens),\n logout: (options) => instance.logout(options),\n refresh: () => instance.refresh(),\n stop,\n }\n}\n"]}
|
package/dist/vue/index.d.cts
CHANGED
|
@@ -1,9 +1,49 @@
|
|
|
1
|
+
import { ComputedRef, App } from 'vue';
|
|
2
|
+
import { a as TokenPayload, b as AuthError, T as TokenSet, L as LogoutOptions, A as AuthKeeper } from '../types-Bmo8S8O4.cjs';
|
|
3
|
+
export { A as AuthState } from '../auth-source-BaZ_-Fu7.cjs';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Vue 3 bindings: provideAuth() / injectAuth() and the useAuth() composable
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Provide an AuthKeeper instance to the component's descendants
|
|
11
|
+
* (call in `setup()`; for the whole app use `app.use(authKeeperPlugin(auth))`)
|
|
12
|
+
*/
|
|
13
|
+
declare function provideAuth(auth: AuthKeeper): void;
|
|
14
|
+
/**
|
|
15
|
+
* The provided AuthKeeper instance (call in `setup()`)
|
|
16
|
+
* @throws {AuthError} CONFIG_ERROR when none was provided
|
|
17
|
+
*/
|
|
18
|
+
declare function injectAuth(): AuthKeeper;
|
|
19
|
+
/**
|
|
20
|
+
* Vue plugin: `app.use(authKeeperPlugin(auth))` provides the instance app-wide
|
|
21
|
+
*/
|
|
22
|
+
declare function authKeeperPlugin(auth: AuthKeeper): {
|
|
23
|
+
install(app: App): void;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* useAuth() return value: read-only refs plus actions
|
|
27
|
+
*/
|
|
28
|
+
interface UseAuthReturn {
|
|
29
|
+
isAuthenticated: ComputedRef<boolean>;
|
|
30
|
+
accessToken: ComputedRef<string | null>;
|
|
31
|
+
user: ComputedRef<TokenPayload | null>;
|
|
32
|
+
expiresAt: ComputedRef<Date | null>;
|
|
33
|
+
error: ComputedRef<AuthError | null>;
|
|
34
|
+
setTokens: (tokens: TokenSet) => void;
|
|
35
|
+
logout: (options?: LogoutOptions) => void;
|
|
36
|
+
refresh: () => Promise<TokenSet>;
|
|
37
|
+
/** Stop updating (done automatically when the component / effect scope is disposed) */
|
|
38
|
+
stop: () => void;
|
|
39
|
+
}
|
|
1
40
|
/**
|
|
2
|
-
*
|
|
41
|
+
* Reactive auth state. Updates after login, logout, refresh and error events
|
|
42
|
+
* and when the access token expires; stops when the calling component (or
|
|
43
|
+
* effect scope) is disposed.
|
|
3
44
|
*
|
|
4
|
-
*
|
|
5
|
-
* Will be implemented in Phase 8.
|
|
45
|
+
* @param auth - Instance to use (default: the provided one)
|
|
6
46
|
*/
|
|
7
|
-
declare
|
|
47
|
+
declare function useAuth(auth?: AuthKeeper): UseAuthReturn;
|
|
8
48
|
|
|
9
|
-
export {
|
|
49
|
+
export { type UseAuthReturn, authKeeperPlugin, injectAuth, provideAuth, useAuth };
|
package/dist/vue/index.d.ts
CHANGED
|
@@ -1,9 +1,49 @@
|
|
|
1
|
+
import { ComputedRef, App } from 'vue';
|
|
2
|
+
import { a as TokenPayload, b as AuthError, T as TokenSet, L as LogoutOptions, A as AuthKeeper } from '../types-Bmo8S8O4.js';
|
|
3
|
+
export { A as AuthState } from '../auth-source-Cm0R6C6I.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Vue 3 bindings: provideAuth() / injectAuth() and the useAuth() composable
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Provide an AuthKeeper instance to the component's descendants
|
|
11
|
+
* (call in `setup()`; for the whole app use `app.use(authKeeperPlugin(auth))`)
|
|
12
|
+
*/
|
|
13
|
+
declare function provideAuth(auth: AuthKeeper): void;
|
|
14
|
+
/**
|
|
15
|
+
* The provided AuthKeeper instance (call in `setup()`)
|
|
16
|
+
* @throws {AuthError} CONFIG_ERROR when none was provided
|
|
17
|
+
*/
|
|
18
|
+
declare function injectAuth(): AuthKeeper;
|
|
19
|
+
/**
|
|
20
|
+
* Vue plugin: `app.use(authKeeperPlugin(auth))` provides the instance app-wide
|
|
21
|
+
*/
|
|
22
|
+
declare function authKeeperPlugin(auth: AuthKeeper): {
|
|
23
|
+
install(app: App): void;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* useAuth() return value: read-only refs plus actions
|
|
27
|
+
*/
|
|
28
|
+
interface UseAuthReturn {
|
|
29
|
+
isAuthenticated: ComputedRef<boolean>;
|
|
30
|
+
accessToken: ComputedRef<string | null>;
|
|
31
|
+
user: ComputedRef<TokenPayload | null>;
|
|
32
|
+
expiresAt: ComputedRef<Date | null>;
|
|
33
|
+
error: ComputedRef<AuthError | null>;
|
|
34
|
+
setTokens: (tokens: TokenSet) => void;
|
|
35
|
+
logout: (options?: LogoutOptions) => void;
|
|
36
|
+
refresh: () => Promise<TokenSet>;
|
|
37
|
+
/** Stop updating (done automatically when the component / effect scope is disposed) */
|
|
38
|
+
stop: () => void;
|
|
39
|
+
}
|
|
1
40
|
/**
|
|
2
|
-
*
|
|
41
|
+
* Reactive auth state. Updates after login, logout, refresh and error events
|
|
42
|
+
* and when the access token expires; stops when the calling component (or
|
|
43
|
+
* effect scope) is disposed.
|
|
3
44
|
*
|
|
4
|
-
*
|
|
5
|
-
* Will be implemented in Phase 8.
|
|
45
|
+
* @param auth - Instance to use (default: the provided one)
|
|
6
46
|
*/
|
|
7
|
-
declare
|
|
47
|
+
declare function useAuth(auth?: AuthKeeper): UseAuthReturn;
|
|
8
48
|
|
|
9
|
-
export {
|
|
49
|
+
export { type UseAuthReturn, authKeeperPlugin, injectAuth, provideAuth, useAuth };
|
package/dist/vue/index.js
CHANGED
|
@@ -1,6 +1,46 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { AuthError } from '../chunk-CYCWWUCJ.js';
|
|
2
|
+
import { createAuthSource } from '../chunk-SUGFPZ6O.js';
|
|
3
|
+
import { provide, inject, shallowRef, getCurrentScope, onScopeDispose, computed } from 'vue';
|
|
3
4
|
|
|
4
|
-
|
|
5
|
+
var AUTH_KEY = /* @__PURE__ */ Symbol("authkeeper");
|
|
6
|
+
function provideAuth(auth) {
|
|
7
|
+
provide(AUTH_KEY, auth);
|
|
8
|
+
}
|
|
9
|
+
function injectAuth() {
|
|
10
|
+
const auth = inject(AUTH_KEY, null);
|
|
11
|
+
if (!auth) {
|
|
12
|
+
throw new AuthError("CONFIG_ERROR", "No AuthKeeper provided: call provideAuth(auth) in an ancestor");
|
|
13
|
+
}
|
|
14
|
+
return auth;
|
|
15
|
+
}
|
|
16
|
+
function authKeeperPlugin(auth) {
|
|
17
|
+
return {
|
|
18
|
+
install(app) {
|
|
19
|
+
app.provide(AUTH_KEY, auth);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
function useAuth(auth) {
|
|
24
|
+
const instance = auth ?? injectAuth();
|
|
25
|
+
const source = createAuthSource(instance);
|
|
26
|
+
const state = shallowRef(source.getState());
|
|
27
|
+
const stop = source.subscribe(() => {
|
|
28
|
+
state.value = source.getState();
|
|
29
|
+
});
|
|
30
|
+
if (getCurrentScope()) onScopeDispose(stop);
|
|
31
|
+
return {
|
|
32
|
+
isAuthenticated: computed(() => state.value.isAuthenticated),
|
|
33
|
+
accessToken: computed(() => state.value.accessToken),
|
|
34
|
+
user: computed(() => state.value.user),
|
|
35
|
+
expiresAt: computed(() => state.value.expiresAt),
|
|
36
|
+
error: computed(() => state.value.error),
|
|
37
|
+
setTokens: (tokens) => instance.setTokens(tokens),
|
|
38
|
+
logout: (options) => instance.logout(options),
|
|
39
|
+
refresh: () => instance.refresh(),
|
|
40
|
+
stop
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export { authKeeperPlugin, injectAuth, provideAuth, useAuth };
|
|
5
45
|
//# sourceMappingURL=index.js.map
|
|
6
46
|
//# sourceMappingURL=index.js.map
|
package/dist/vue/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/adapters/vue/
|
|
1
|
+
{"version":3,"sources":["../../src/adapters/vue/use-auth.ts"],"names":[],"mappings":";;;;AAkBA,IAAM,QAAA,0BAA4C,YAAY,CAAA;AAMvD,SAAS,YAAY,IAAA,EAAwB;AAClD,EAAA,OAAA,CAAQ,UAAU,IAAI,CAAA;AACxB;AAMO,SAAS,UAAA,GAAyB;AACvC,EAAA,MAAM,IAAA,GAAO,MAAA,CAAO,QAAA,EAAU,IAAI,CAAA;AAClC,EAAA,IAAI,CAAC,IAAA,EAAM;AACT,IAAA,MAAM,IAAI,SAAA,CAAU,cAAA,EAAgB,+DAA+D,CAAA;AAAA,EACrG;AACA,EAAA,OAAO,IAAA;AACT;AAKO,SAAS,iBAAiB,IAAA,EAA+C;AAC9E,EAAA,OAAO;AAAA,IACL,QAAQ,GAAA,EAAU;AAChB,MAAA,GAAA,CAAI,OAAA,CAAQ,UAAU,IAAI,CAAA;AAAA,IAC5B;AAAA,GACF;AACF;AAyBO,SAAS,QAAQ,IAAA,EAAkC;AACxD,EAAA,MAAM,QAAA,GAAW,QAAQ,UAAA,EAAW;AACpC,EAAA,MAAM,MAAA,GAAS,iBAAiB,QAAQ,CAAA;AACxC,EAAA,MAAM,KAAA,GAAQ,UAAA,CAAW,MAAA,CAAO,QAAA,EAAU,CAAA;AAC1C,EAAA,MAAM,IAAA,GAAO,MAAA,CAAO,SAAA,CAAU,MAAM;AAClC,IAAA,KAAA,CAAM,KAAA,GAAQ,OAAO,QAAA,EAAS;AAAA,EAChC,CAAC,CAAA;AACD,EAAA,IAAI,eAAA,EAAgB,EAAG,cAAA,CAAe,IAAI,CAAA;AAE1C,EAAA,OAAO;AAAA,IACL,eAAA,EAAiB,QAAA,CAAS,MAAM,KAAA,CAAM,MAAM,eAAe,CAAA;AAAA,IAC3D,WAAA,EAAa,QAAA,CAAS,MAAM,KAAA,CAAM,MAAM,WAAW,CAAA;AAAA,IACnD,IAAA,EAAM,QAAA,CAAS,MAAM,KAAA,CAAM,MAAM,IAAI,CAAA;AAAA,IACrC,SAAA,EAAW,QAAA,CAAS,MAAM,KAAA,CAAM,MAAM,SAAS,CAAA;AAAA,IAC/C,KAAA,EAAO,QAAA,CAAS,MAAM,KAAA,CAAM,MAAM,KAAK,CAAA;AAAA,IACvC,SAAA,EAAW,CAAC,MAAA,KAAW,QAAA,CAAS,UAAU,MAAM,CAAA;AAAA,IAChD,MAAA,EAAQ,CAAC,OAAA,KAAY,QAAA,CAAS,OAAO,OAAO,CAAA;AAAA,IAC5C,OAAA,EAAS,MAAM,QAAA,CAAS,OAAA,EAAQ;AAAA,IAChC;AAAA,GACF;AACF","file":"index.js","sourcesContent":["/**\n * Vue 3 bindings: provideAuth() / injectAuth() and the useAuth() composable\n */\n\nimport {\n computed,\n getCurrentScope,\n inject,\n onScopeDispose,\n provide,\n shallowRef,\n type App,\n type ComputedRef,\n type InjectionKey,\n} from 'vue'\nimport { AuthError, type AuthKeeper, type LogoutOptions, type TokenPayload, type TokenSet } from '../../types'\nimport { createAuthSource } from '../auth-source'\n\nconst AUTH_KEY: InjectionKey<AuthKeeper> = Symbol('authkeeper')\n\n/**\n * Provide an AuthKeeper instance to the component's descendants\n * (call in `setup()`; for the whole app use `app.use(authKeeperPlugin(auth))`)\n */\nexport function provideAuth(auth: AuthKeeper): void {\n provide(AUTH_KEY, auth)\n}\n\n/**\n * The provided AuthKeeper instance (call in `setup()`)\n * @throws {AuthError} CONFIG_ERROR when none was provided\n */\nexport function injectAuth(): AuthKeeper {\n const auth = inject(AUTH_KEY, null)\n if (!auth) {\n throw new AuthError('CONFIG_ERROR', 'No AuthKeeper provided: call provideAuth(auth) in an ancestor')\n }\n return auth\n}\n\n/**\n * Vue plugin: `app.use(authKeeperPlugin(auth))` provides the instance app-wide\n */\nexport function authKeeperPlugin(auth: AuthKeeper): { install(app: App): void } {\n return {\n install(app: App) {\n app.provide(AUTH_KEY, auth)\n },\n }\n}\n\n/**\n * useAuth() return value: read-only refs plus actions\n */\nexport interface UseAuthReturn {\n isAuthenticated: ComputedRef<boolean>\n accessToken: ComputedRef<string | null>\n user: ComputedRef<TokenPayload | null>\n expiresAt: ComputedRef<Date | null>\n error: ComputedRef<AuthError | null>\n setTokens: (tokens: TokenSet) => void\n logout: (options?: LogoutOptions) => void\n refresh: () => Promise<TokenSet>\n /** Stop updating (done automatically when the component / effect scope is disposed) */\n stop: () => void\n}\n\n/**\n * Reactive auth state. Updates after login, logout, refresh and error events\n * and when the access token expires; stops when the calling component (or\n * effect scope) is disposed.\n *\n * @param auth - Instance to use (default: the provided one)\n */\nexport function useAuth(auth?: AuthKeeper): UseAuthReturn {\n const instance = auth ?? injectAuth()\n const source = createAuthSource(instance)\n const state = shallowRef(source.getState())\n const stop = source.subscribe(() => {\n state.value = source.getState()\n })\n if (getCurrentScope()) onScopeDispose(stop)\n\n return {\n isAuthenticated: computed(() => state.value.isAuthenticated),\n accessToken: computed(() => state.value.accessToken),\n user: computed(() => state.value.user),\n expiresAt: computed(() => state.value.expiresAt),\n error: computed(() => state.value.error),\n setTokens: (tokens) => instance.setTokens(tokens),\n logout: (options) => instance.logout(options),\n refresh: () => instance.refresh(),\n stop,\n }\n}\n"]}
|