@molecule/app-auth 1.2.0 → 1.2.1

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.
Files changed (2) hide show
  1. package/README.md +276 -276
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -3,7 +3,7 @@ AUTO-GENERATED — DO NOT EDIT THIS FILE.
3
3
  Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
4
4
  Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
5
5
  To change this document, edit the module-level JSDoc in src/index.ts.
6
- Generated: 2026-10-15T09:33:35.638Z
6
+ Generated: 2026-10-16T00:48:55.816Z
7
7
  -->
8
8
 
9
9
  # @molecule/app-auth
@@ -50,6 +50,203 @@ npm install @molecule/app-auth @molecule/app-bond @molecule/app-i18n @molecule/a
50
50
 
51
51
  ### Interfaces
52
52
 
53
+ #### `UserProfile`
54
+
55
+ User profile information.
56
+
57
+ ```typescript
58
+ interface UserProfile {
59
+ /**
60
+ * Unique user identifier.
61
+ */
62
+ id: string
63
+
64
+ /**
65
+ * User's email address.
66
+ */
67
+ email?: string
68
+
69
+ /**
70
+ * User's display name.
71
+ */
72
+ name?: string
73
+
74
+ /**
75
+ * User's avatar/profile image URL.
76
+ */
77
+ avatar?: string
78
+
79
+ /**
80
+ * User's roles or permissions.
81
+ */
82
+ roles?: string[]
83
+
84
+ /**
85
+ * Additional metadata.
86
+ */
87
+ metadata?: Record<string, unknown>
88
+ }
89
+ ```
90
+
91
+ #### `LoginCredentials`
92
+
93
+ Email/password login credentials with optional "remember me" flag.
94
+
95
+ ```typescript
96
+ interface LoginCredentials {
97
+ /**
98
+ * Email or username.
99
+ */
100
+ email: string
101
+
102
+ /**
103
+ * Password.
104
+ */
105
+ password: string
106
+
107
+ /**
108
+ * Whether to remember the session.
109
+ */
110
+ remember?: boolean
111
+
112
+ /**
113
+ * Time-based one-time password (TOTP) from the user's authenticator app.
114
+ * Required when the account has two-factor auth enabled — the server
115
+ * answers `{ twoFactorRequired: true }` (HTTP 206) until a valid token
116
+ * is supplied.
117
+ */
118
+ twoFactorToken?: string
119
+ }
120
+ ```
121
+
122
+ #### `RegisterData`
123
+
124
+ New user registration payload (email, password, optional name and metadata).
125
+
126
+ ```typescript
127
+ interface RegisterData {
128
+ /**
129
+ * Email address.
130
+ */
131
+ email: string
132
+
133
+ /**
134
+ * Password.
135
+ */
136
+ password: string
137
+
138
+ /**
139
+ * Display name.
140
+ */
141
+ name?: string
142
+
143
+ /**
144
+ * Additional registration fields.
145
+ */
146
+ metadata?: Record<string, unknown>
147
+ }
148
+ ```
149
+
150
+ #### `AuthResult`
151
+
152
+ Successful authentication result containing the user, access/refresh tokens, and expiration.
153
+
154
+ ```typescript
155
+ interface AuthResult<T = UserProfile> {
156
+ /**
157
+ * Authenticated user.
158
+ */
159
+ user: T
160
+
161
+ /**
162
+ * Access token (for token-based auth).
163
+ */
164
+ accessToken?: string
165
+
166
+ /**
167
+ * Refresh token (for token-based auth).
168
+ */
169
+ refreshToken?: string
170
+
171
+ /**
172
+ * Token expiration time (Unix timestamp).
173
+ */
174
+ expiresAt?: number
175
+
176
+ /**
177
+ * Set (with HTTP 206) when the account has two-factor auth enabled and the
178
+ * login did not include a valid `twoFactorToken`. No tokens or user are
179
+ * issued until login is retried with the TOTP — callers must check this
180
+ * flag BEFORE treating the result as a successful authentication.
181
+ */
182
+ twoFactorRequired?: boolean
183
+ }
184
+ ```
185
+
186
+ #### `PasswordResetRequest`
187
+
188
+ Password reset request data.
189
+
190
+ ```typescript
191
+ interface PasswordResetRequest {
192
+ /**
193
+ * Email address.
194
+ */
195
+ email: string
196
+ }
197
+ ```
198
+
199
+ #### `PasswordResetConfirm`
200
+
201
+ Password reset confirmation data.
202
+
203
+ ```typescript
204
+ interface PasswordResetConfirm {
205
+ /**
206
+ * Reset token.
207
+ */
208
+ token: string
209
+
210
+ /**
211
+ * New password.
212
+ */
213
+ password: string
214
+ }
215
+ ```
216
+
217
+ #### `AuthState`
218
+
219
+ Reactive authentication state snapshot (initialized, authenticated, user, loading, and error).
220
+
221
+ ```typescript
222
+ interface AuthState<T = UserProfile> {
223
+ /**
224
+ * Whether auth state has been initialized.
225
+ */
226
+ initialized: boolean
227
+
228
+ /**
229
+ * Whether the user is authenticated.
230
+ */
231
+ authenticated: boolean
232
+
233
+ /**
234
+ * Current user (if authenticated).
235
+ */
236
+ user: T | null
237
+
238
+ /**
239
+ * Whether an auth operation is in progress.
240
+ */
241
+ loading: boolean
242
+
243
+ /**
244
+ * Last auth error (if any).
245
+ */
246
+ error: string | null
247
+ }
248
+ ```
249
+
53
250
  #### `AuthClient`
54
251
 
55
252
  Auth client interface that all auth bond packages must implement.
@@ -172,6 +369,19 @@ interface AuthClient<T = UserProfile> {
172
369
  }
173
370
  ```
174
371
 
372
+ #### `StorageAdapter`
373
+
374
+ Minimal key-value storage adapter used by the auth client for
375
+ persisting tokens and user data.
376
+
377
+ ```typescript
378
+ interface StorageAdapter {
379
+ getItem(key: string): string | null
380
+ setItem(key: string, value: string): void
381
+ removeItem(key: string): void
382
+ }
383
+ ```
384
+
175
385
  #### `AuthClientConfig`
176
386
 
177
387
  Configuration options for creating an auth client, including API
@@ -323,236 +533,26 @@ interface AuthClientConfig {
323
533
  }
324
534
  ````
325
535
 
326
- #### `AuthResult`
536
+ #### `TokenStorage`
327
537
 
328
- Successful authentication result containing the user, access/refresh tokens, and expiration.
538
+ Internal token storage interface for reading and writing access/refresh
539
+ tokens and user profile data.
329
540
 
330
541
  ```typescript
331
- interface AuthResult<T = UserProfile> {
332
- /**
333
- * Authenticated user.
334
- */
335
- user: T
336
-
337
- /**
338
- * Access token (for token-based auth).
339
- */
340
- accessToken?: string
341
-
342
- /**
343
- * Refresh token (for token-based auth).
344
- */
345
- refreshToken?: string
346
-
347
- /**
348
- * Token expiration time (Unix timestamp).
349
- */
350
- expiresAt?: number
351
-
352
- /**
353
- * Set (with HTTP 206) when the account has two-factor auth enabled and the
354
- * login did not include a valid `twoFactorToken`. No tokens or user are
355
- * issued until login is retried with the TOTP — callers must check this
356
- * flag BEFORE treating the result as a successful authentication.
357
- */
358
- twoFactorRequired?: boolean
542
+ interface TokenStorage {
543
+ getAccessToken(): string | null
544
+ setAccessToken(token: string | null): void
545
+ getRefreshToken(): string | null
546
+ setRefreshToken(token: string | null): void
547
+ getUser<T = UserProfile>(): T | null
548
+ setUser<T = UserProfile>(user: T | null): void
549
+ clear(): void
359
550
  }
360
551
  ```
361
552
 
362
- #### `AuthState`
553
+ ### Types
363
554
 
364
- Reactive authentication state snapshot (initialized, authenticated, user, loading, and error).
365
-
366
- ```typescript
367
- interface AuthState<T = UserProfile> {
368
- /**
369
- * Whether auth state has been initialized.
370
- */
371
- initialized: boolean
372
-
373
- /**
374
- * Whether the user is authenticated.
375
- */
376
- authenticated: boolean
377
-
378
- /**
379
- * Current user (if authenticated).
380
- */
381
- user: T | null
382
-
383
- /**
384
- * Whether an auth operation is in progress.
385
- */
386
- loading: boolean
387
-
388
- /**
389
- * Last auth error (if any).
390
- */
391
- error: string | null
392
- }
393
- ```
394
-
395
- #### `LoginCredentials`
396
-
397
- Email/password login credentials with optional "remember me" flag.
398
-
399
- ```typescript
400
- interface LoginCredentials {
401
- /**
402
- * Email or username.
403
- */
404
- email: string
405
-
406
- /**
407
- * Password.
408
- */
409
- password: string
410
-
411
- /**
412
- * Whether to remember the session.
413
- */
414
- remember?: boolean
415
-
416
- /**
417
- * Time-based one-time password (TOTP) from the user's authenticator app.
418
- * Required when the account has two-factor auth enabled — the server
419
- * answers `{ twoFactorRequired: true }` (HTTP 206) until a valid token
420
- * is supplied.
421
- */
422
- twoFactorToken?: string
423
- }
424
- ```
425
-
426
- #### `PasswordResetConfirm`
427
-
428
- Password reset confirmation data.
429
-
430
- ```typescript
431
- interface PasswordResetConfirm {
432
- /**
433
- * Reset token.
434
- */
435
- token: string
436
-
437
- /**
438
- * New password.
439
- */
440
- password: string
441
- }
442
- ```
443
-
444
- #### `PasswordResetRequest`
445
-
446
- Password reset request data.
447
-
448
- ```typescript
449
- interface PasswordResetRequest {
450
- /**
451
- * Email address.
452
- */
453
- email: string
454
- }
455
- ```
456
-
457
- #### `RegisterData`
458
-
459
- New user registration payload (email, password, optional name and metadata).
460
-
461
- ```typescript
462
- interface RegisterData {
463
- /**
464
- * Email address.
465
- */
466
- email: string
467
-
468
- /**
469
- * Password.
470
- */
471
- password: string
472
-
473
- /**
474
- * Display name.
475
- */
476
- name?: string
477
-
478
- /**
479
- * Additional registration fields.
480
- */
481
- metadata?: Record<string, unknown>
482
- }
483
- ```
484
-
485
- #### `StorageAdapter`
486
-
487
- Minimal key-value storage adapter used by the auth client for
488
- persisting tokens and user data.
489
-
490
- ```typescript
491
- interface StorageAdapter {
492
- getItem(key: string): string | null
493
- setItem(key: string, value: string): void
494
- removeItem(key: string): void
495
- }
496
- ```
497
-
498
- #### `TokenStorage`
499
-
500
- Internal token storage interface for reading and writing access/refresh
501
- tokens and user profile data.
502
-
503
- ```typescript
504
- interface TokenStorage {
505
- getAccessToken(): string | null
506
- setAccessToken(token: string | null): void
507
- getRefreshToken(): string | null
508
- setRefreshToken(token: string | null): void
509
- getUser<T = UserProfile>(): T | null
510
- setUser<T = UserProfile>(user: T | null): void
511
- clear(): void
512
- }
513
- ```
514
-
515
- #### `UserProfile`
516
-
517
- User profile information.
518
-
519
- ```typescript
520
- interface UserProfile {
521
- /**
522
- * Unique user identifier.
523
- */
524
- id: string
525
-
526
- /**
527
- * User's email address.
528
- */
529
- email?: string
530
-
531
- /**
532
- * User's display name.
533
- */
534
- name?: string
535
-
536
- /**
537
- * User's avatar/profile image URL.
538
- */
539
- avatar?: string
540
-
541
- /**
542
- * User's roles or permissions.
543
- */
544
- roles?: string[]
545
-
546
- /**
547
- * Additional metadata.
548
- */
549
- metadata?: Record<string, unknown>
550
- }
551
- ```
552
-
553
- ### Types
554
-
555
- #### `AuthEvent`
555
+ #### `AuthEvent`
556
556
 
557
557
  Auth event types.
558
558
 
@@ -590,19 +590,16 @@ function createJWTAuthClient(config?: AuthClientConfig): AuthClient<T>
590
590
 
591
591
  **Returns:** A fully configured `AuthClient` instance.
592
592
 
593
- #### `createTokenStorage(storage, prefix)`
593
+ #### `setClient(client)`
594
594
 
595
- Creates a token storage implementation backed by either in-memory
596
- storage or a custom `StorageAdapter`.
595
+ Registers an auth client as the active singleton. Called by bond
596
+ packages during application startup.
597
597
 
598
598
  ```typescript
599
- function createTokenStorage(storage?: 'memory' | StorageAdapter, prefix?: string): TokenStorage
599
+ function setClient(client: AuthClient<UserProfile>): void
600
600
  ```
601
601
 
602
- - `storage` — `'memory'` for in-memory storage (lost on refresh), or a `StorageAdapter` for persistent storage.
603
- - `prefix` — Key prefix for storage items (default: `'molecule:auth:'`).
604
-
605
- **Returns:** A `TokenStorage` instance.
602
+ - `client` — The auth client implementation to bond.
606
603
 
607
604
  #### `getClient()`
608
605
 
@@ -614,37 +611,67 @@ function getClient(): AuthClient<T>
614
611
 
615
612
  **Returns:** The bonded auth client.
616
613
 
617
- #### `getTokenExpiration(token)`
614
+ #### `hasClient()`
618
615
 
619
- Returns the expiration timestamp of a JWT token in milliseconds.
616
+ Checks whether an auth client is currently bonded.
620
617
 
621
618
  ```typescript
622
- function getTokenExpiration(token: string): number | null
619
+ function hasClient(): boolean
623
620
  ```
624
621
 
625
- - `token` — The raw JWT string.
622
+ **Returns:** `true` if an auth client is bonded.
626
623
 
627
- **Returns:** The expiration time in ms since epoch, or `null` if the token has no `exp` claim.
624
+ #### `createTokenStorage(storage, prefix)`
628
625
 
629
- #### `getUser()`
626
+ Creates a token storage implementation backed by either in-memory
627
+ storage or a custom `StorageAdapter`.
630
628
 
631
- Returns the current user profile, or `null` if not authenticated.
629
+ ```typescript
630
+ function createTokenStorage(storage?: 'memory' | StorageAdapter, prefix?: string): TokenStorage
631
+ ```
632
+
633
+ - `storage` — `'memory'` for in-memory storage (lost on refresh), or a `StorageAdapter` for persistent storage.
634
+ - `prefix` — Key prefix for storage items (default: `'molecule:auth:'`).
635
+
636
+ **Returns:** A `TokenStorage` instance.
637
+
638
+ #### `parseJWT(token)`
639
+
640
+ Parses a JWT token payload without verification (base64-decodes
641
+ the payload segment).
632
642
 
633
643
  ```typescript
634
- function getUser(): T | null
644
+ function parseJWT(token: string): T | null
635
645
  ```
636
646
 
637
- **Returns:** The user profile, or `null` if not authenticated.
647
+ - `token` — The raw JWT string (header.payload.signature).
638
648
 
639
- #### `hasClient()`
649
+ **Returns:** The decoded payload object, or `null` if parsing fails.
640
650
 
641
- Checks whether an auth client is currently bonded.
651
+ #### `isTokenExpired(token, bufferSeconds)`
652
+
653
+ Checks if a JWT token is expired (or will expire within the buffer window).
642
654
 
643
655
  ```typescript
644
- function hasClient(): boolean
656
+ function isTokenExpired(token: string, bufferSeconds?: number): boolean
645
657
  ```
646
658
 
647
- **Returns:** `true` if an auth client is bonded.
659
+ - `token` — The raw JWT string.
660
+ - `bufferSeconds` — Seconds before actual expiry to consider the token expired (default: 0).
661
+
662
+ **Returns:** `true` if the token is expired or lacks an `exp` claim.
663
+
664
+ #### `getTokenExpiration(token)`
665
+
666
+ Returns the expiration timestamp of a JWT token in milliseconds.
667
+
668
+ ```typescript
669
+ function getTokenExpiration(token: string): number | null
670
+ ```
671
+
672
+ - `token` — The raw JWT string.
673
+
674
+ **Returns:** The expiration time in ms since epoch, or `null` if the token has no `exp` claim.
648
675
 
649
676
  #### `isAuthenticated()`
650
677
 
@@ -656,18 +683,15 @@ function isAuthenticated(): boolean
656
683
 
657
684
  **Returns:** `true` if the user has an active session.
658
685
 
659
- #### `isTokenExpired(token, bufferSeconds)`
686
+ #### `getUser()`
660
687
 
661
- Checks if a JWT token is expired (or will expire within the buffer window).
688
+ Returns the current user profile, or `null` if not authenticated.
662
689
 
663
690
  ```typescript
664
- function isTokenExpired(token: string, bufferSeconds?: number): boolean
691
+ function getUser(): T | null
665
692
  ```
666
693
 
667
- - `token` — The raw JWT string.
668
- - `bufferSeconds` — Seconds before actual expiry to consider the token expired (default: 0).
669
-
670
- **Returns:** `true` if the token is expired or lacks an `exp` claim.
694
+ **Returns:** The user profile, or `null` if not authenticated.
671
695
 
672
696
  #### `login(credentials)`
673
697
 
@@ -691,19 +715,6 @@ function logout(): Promise<void>
691
715
 
692
716
  **Returns:** A promise that resolves when logout completes.
693
717
 
694
- #### `parseJWT(token)`
695
-
696
- Parses a JWT token payload without verification (base64-decodes
697
- the payload segment).
698
-
699
- ```typescript
700
- function parseJWT(token: string): T | null
701
- ```
702
-
703
- - `token` — The raw JWT string (header.payload.signature).
704
-
705
- **Returns:** The decoded payload object, or `null` if parsing fails.
706
-
707
718
  #### `register(data)`
708
719
 
709
720
  Registers a new user account.
@@ -716,17 +727,6 @@ function register(data: RegisterData): Promise<AuthResult<UserProfile>>
716
727
 
717
728
  **Returns:** The auth result containing tokens and user profile.
718
729
 
719
- #### `setClient(client)`
720
-
721
- Registers an auth client as the active singleton. Called by bond
722
- packages during application startup.
723
-
724
- ```typescript
725
- function setClient(client: AuthClient<UserProfile>): void
726
- ```
727
-
728
- - `client` — The auth client implementation to bond.
729
-
730
730
  ## Injection Notes
731
731
 
732
732
  ### Requirements
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@molecule/app-auth",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "description": "Client-side authentication interface for molecule.dev",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",