@molecule/app-auth 1.0.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.
@@ -0,0 +1,387 @@
1
+ /**
2
+ * Type definitions for client-side authentication.
3
+ *
4
+ * @module
5
+ */
6
+ /**
7
+ * User profile information.
8
+ */
9
+ export interface UserProfile {
10
+ /**
11
+ * Unique user identifier.
12
+ */
13
+ id: string;
14
+ /**
15
+ * User's email address.
16
+ */
17
+ email?: string;
18
+ /**
19
+ * User's display name.
20
+ */
21
+ name?: string;
22
+ /**
23
+ * User's avatar/profile image URL.
24
+ */
25
+ avatar?: string;
26
+ /**
27
+ * User's roles or permissions.
28
+ */
29
+ roles?: string[];
30
+ /**
31
+ * Additional metadata.
32
+ */
33
+ metadata?: Record<string, unknown>;
34
+ }
35
+ /**
36
+ * Email/password login credentials with optional "remember me" flag.
37
+ */
38
+ export interface LoginCredentials {
39
+ /**
40
+ * Email or username.
41
+ */
42
+ email: string;
43
+ /**
44
+ * Password.
45
+ */
46
+ password: string;
47
+ /**
48
+ * Whether to remember the session.
49
+ */
50
+ remember?: boolean;
51
+ /**
52
+ * Time-based one-time password (TOTP) from the user's authenticator app.
53
+ * Required when the account has two-factor auth enabled — the server
54
+ * answers `{ twoFactorRequired: true }` (HTTP 206) until a valid token
55
+ * is supplied.
56
+ */
57
+ twoFactorToken?: string;
58
+ }
59
+ /**
60
+ * New user registration payload (email, password, optional name and metadata).
61
+ */
62
+ export interface RegisterData {
63
+ /**
64
+ * Email address.
65
+ */
66
+ email: string;
67
+ /**
68
+ * Password.
69
+ */
70
+ password: string;
71
+ /**
72
+ * Display name.
73
+ */
74
+ name?: string;
75
+ /**
76
+ * Additional registration fields.
77
+ */
78
+ metadata?: Record<string, unknown>;
79
+ }
80
+ /**
81
+ * Successful authentication result containing the user, access/refresh tokens, and expiration.
82
+ */
83
+ export interface AuthResult<T = UserProfile> {
84
+ /**
85
+ * Authenticated user.
86
+ */
87
+ user: T;
88
+ /**
89
+ * Access token (for token-based auth).
90
+ */
91
+ accessToken?: string;
92
+ /**
93
+ * Refresh token (for token-based auth).
94
+ */
95
+ refreshToken?: string;
96
+ /**
97
+ * Token expiration time (Unix timestamp).
98
+ */
99
+ expiresAt?: number;
100
+ /**
101
+ * Set (with HTTP 206) when the account has two-factor auth enabled and the
102
+ * login did not include a valid `twoFactorToken`. No tokens or user are
103
+ * issued until login is retried with the TOTP — callers must check this
104
+ * flag BEFORE treating the result as a successful authentication.
105
+ */
106
+ twoFactorRequired?: boolean;
107
+ }
108
+ /**
109
+ * Password reset request data.
110
+ */
111
+ export interface PasswordResetRequest {
112
+ /**
113
+ * Email address.
114
+ */
115
+ email: string;
116
+ }
117
+ /**
118
+ * Password reset confirmation data.
119
+ */
120
+ export interface PasswordResetConfirm {
121
+ /**
122
+ * Reset token.
123
+ */
124
+ token: string;
125
+ /**
126
+ * New password.
127
+ */
128
+ password: string;
129
+ }
130
+ /**
131
+ * Reactive authentication state snapshot (initialized, authenticated, user, loading, and error).
132
+ */
133
+ export interface AuthState<T = UserProfile> {
134
+ /**
135
+ * Whether auth state has been initialized.
136
+ */
137
+ initialized: boolean;
138
+ /**
139
+ * Whether the user is authenticated.
140
+ */
141
+ authenticated: boolean;
142
+ /**
143
+ * Current user (if authenticated).
144
+ */
145
+ user: T | null;
146
+ /**
147
+ * Whether an auth operation is in progress.
148
+ */
149
+ loading: boolean;
150
+ /**
151
+ * Last auth error (if any).
152
+ */
153
+ error: string | null;
154
+ }
155
+ /**
156
+ * Auth event types.
157
+ */
158
+ export type AuthEvent = {
159
+ type: 'login';
160
+ user: UserProfile;
161
+ } | {
162
+ type: 'logout';
163
+ } | {
164
+ type: 'register';
165
+ user: UserProfile;
166
+ } | {
167
+ type: 'refresh';
168
+ } | {
169
+ type: 'error';
170
+ error: string;
171
+ };
172
+ /**
173
+ * Callback invoked when an authentication event occurs (login, logout, etc.).
174
+ */
175
+ export type AuthEventListener = (event: AuthEvent) => void;
176
+ /**
177
+ * Auth client interface that all auth bond packages must implement.
178
+ * Provides login/logout/register flows, token management, profile
179
+ * updates, and auth state subscription.
180
+ */
181
+ export interface AuthClient<T = UserProfile> {
182
+ /**
183
+ * Returns the current authentication state snapshot.
184
+ */
185
+ getState(): AuthState<T>;
186
+ /**
187
+ * Returns whether the user is currently authenticated.
188
+ */
189
+ isAuthenticated(): boolean;
190
+ /**
191
+ * Gets the current user.
192
+ */
193
+ getUser(): T | null;
194
+ /**
195
+ * Updates the cached user object (state + persistent storage) without
196
+ * hitting the network. Intended for local refreshes after a per-app
197
+ * mutation (e.g., the user just PATCHed their own profile and the
198
+ * server returned the canonical row). Does NOT change tokens.
199
+ */
200
+ setUser(user: T | null): void;
201
+ /**
202
+ * Gets the current access token.
203
+ */
204
+ getAccessToken(): string | null;
205
+ /**
206
+ * Stores the access token in the configured token storage adapter (in-memory
207
+ * by default). Use this to seed the token after an out-of-band exchange (e.g.
208
+ * the OAuth code→token redirect) instead of writing to `localStorage` directly,
209
+ * which would violate the in-memory-default storage contract and make the bearer
210
+ * token JS-readable (XSS-exfiltratable). Pass `null` to clear it.
211
+ */
212
+ setAccessToken(token: string | null): void;
213
+ /**
214
+ * Gets the refresh token.
215
+ */
216
+ getRefreshToken(): string | null;
217
+ /**
218
+ * Logs in with credentials.
219
+ */
220
+ login(credentials: LoginCredentials): Promise<AuthResult<T>>;
221
+ /**
222
+ * Logs out the current user.
223
+ */
224
+ logout(): Promise<void>;
225
+ /**
226
+ * Registers a new user.
227
+ */
228
+ register(data: RegisterData): Promise<AuthResult<T>>;
229
+ /**
230
+ * Refreshes the access token.
231
+ */
232
+ refresh(): Promise<AuthResult<T>>;
233
+ /**
234
+ * Requests a password reset.
235
+ */
236
+ requestPasswordReset(data: PasswordResetRequest): Promise<void>;
237
+ /**
238
+ * Confirms a password reset.
239
+ */
240
+ confirmPasswordReset(data: PasswordResetConfirm): Promise<void>;
241
+ /**
242
+ * Updates the current user's profile.
243
+ */
244
+ updateProfile(data: Partial<T>): Promise<T>;
245
+ /**
246
+ * Changes the current user's password.
247
+ */
248
+ changePassword(oldPassword: string, newPassword: string): Promise<void>;
249
+ /**
250
+ * Initializes auth state (e.g., from stored tokens).
251
+ */
252
+ initialize(): Promise<void>;
253
+ /**
254
+ * Subscribes to auth state changes.
255
+ */
256
+ subscribe(callback: (state: AuthState<T>) => void): () => void;
257
+ /**
258
+ * Subscribes to auth state changes (alias for subscribe).
259
+ */
260
+ onAuthChange(callback: (state: AuthState<T>) => void): () => void;
261
+ /**
262
+ * Gets the current access token (alias for getAccessToken).
263
+ */
264
+ getToken?(): string | null;
265
+ /**
266
+ * Adds an auth event listener.
267
+ */
268
+ addEventListener(listener: AuthEventListener): () => void;
269
+ /**
270
+ * Destroys the auth client.
271
+ */
272
+ destroy(): void;
273
+ }
274
+ /**
275
+ * Minimal key-value storage adapter used by the auth client for
276
+ * persisting tokens and user data.
277
+ */
278
+ export interface StorageAdapter {
279
+ getItem(key: string): string | null;
280
+ setItem(key: string, value: string): void;
281
+ removeItem(key: string): void;
282
+ }
283
+ /**
284
+ * Configuration options for creating an auth client, including API
285
+ * endpoints, storage strategy, token refresh, and OAuth providers.
286
+ */
287
+ export interface AuthClientConfig {
288
+ /**
289
+ * API base URL.
290
+ */
291
+ baseURL?: string;
292
+ /**
293
+ * Login endpoint.
294
+ */
295
+ loginEndpoint?: string;
296
+ /**
297
+ * Logout endpoint.
298
+ */
299
+ logoutEndpoint?: string;
300
+ /**
301
+ * Register endpoint.
302
+ */
303
+ registerEndpoint?: string;
304
+ /**
305
+ * Refresh endpoint.
306
+ */
307
+ refreshEndpoint?: string;
308
+ /**
309
+ * User profile endpoint.
310
+ */
311
+ profileEndpoint?: string;
312
+ /**
313
+ * Current-user endpoint for session restore (default `/users/me`). Called by
314
+ * `initialize()` with credentials to re-establish the session from the
315
+ * httpOnly cookie after a full page load — the only way to stay logged in when
316
+ * the bearer token is held in memory (the secure default; a localStorage copy
317
+ * is XSS-exfiltratable). Must return the user (as `user` or `props`).
318
+ */
319
+ currentUserEndpoint?: string;
320
+ /**
321
+ * Storage key prefix.
322
+ */
323
+ storagePrefix?: string;
324
+ /**
325
+ * Token storage type or custom storage adapter.
326
+ * - 'memory': In-memory storage (lost on page refresh)
327
+ * - StorageAdapter: Custom storage implementation (e.g., from `@molecule/app-storage`)
328
+ *
329
+ * @example
330
+ * ```typescript
331
+ * // Use in-memory storage
332
+ * storage: 'memory'
333
+ *
334
+ * // Use custom storage from `@molecule/app-storage`
335
+ * import { getProvider } from '@molecule/app-storage'
336
+ * const storageProvider = getProvider()
337
+ * storage: {
338
+ * getItem: (key) => storageProvider.get(key),
339
+ * setItem: (key, value) => storageProvider.set(key, value),
340
+ * removeItem: (key) => storageProvider.remove(key),
341
+ * }
342
+ * ```
343
+ */
344
+ storage?: 'memory' | StorageAdapter;
345
+ /**
346
+ * Auto refresh tokens before expiry.
347
+ */
348
+ autoRefresh?: boolean;
349
+ /**
350
+ * Refresh tokens this many seconds before expiry.
351
+ */
352
+ refreshBuffer?: number;
353
+ /**
354
+ * Forgot password / password reset request endpoint.
355
+ */
356
+ forgotPasswordEndpoint?: string;
357
+ /**
358
+ * Password reset confirmation endpoint.
359
+ */
360
+ resetPasswordEndpoint?: string;
361
+ /**
362
+ * Change password endpoint.
363
+ */
364
+ changePasswordEndpoint?: string;
365
+ /**
366
+ * Available OAuth providers (e.g., ['github', 'google', 'gitlab']).
367
+ */
368
+ oauthProviders?: string[];
369
+ /**
370
+ * OAuth endpoint path (default: '/oauth').
371
+ */
372
+ oauthEndpoint?: string;
373
+ }
374
+ /**
375
+ * Internal token storage interface for reading and writing access/refresh
376
+ * tokens and user profile data.
377
+ */
378
+ export interface TokenStorage {
379
+ getAccessToken(): string | null;
380
+ setAccessToken(token: string | null): void;
381
+ getRefreshToken(): string | null;
382
+ setRefreshToken(token: string | null): void;
383
+ getUser<T = UserProfile>(): T | null;
384
+ setUser<T = UserProfile>(user: T | null): void;
385
+ clear(): void;
386
+ }
387
+ //# source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,CAAA;IAE9B;;OAEG;IACH,sBAAsB,CAAC,EAAE,MAAM,CAAA;IAE/B;;OAEG;IACH,cAAc,CAAC,EAAE,MAAM,EAAE,CAAA;IAEzB;;OAEG;IACH,aAAa,CAAC,EAAE,MAAM,CAAA;CACvB;AAED;;;GAGG;AACH,MAAM,WAAW,YAAY;IAC3B,cAAc,IAAI,MAAM,GAAG,IAAI,CAAA;IAC/B,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI,CAAA;IAC1C,eAAe,IAAI,MAAM,GAAG,IAAI,CAAA;IAChC,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI,CAAA;IAC3C,OAAO,CAAC,CAAC,GAAG,WAAW,KAAK,CAAC,GAAG,IAAI,CAAA;IACpC,OAAO,CAAC,CAAC,GAAG,WAAW,EAAE,IAAI,EAAE,CAAC,GAAG,IAAI,GAAG,IAAI,CAAA;IAC9C,KAAK,IAAI,IAAI,CAAA;CACd"}
package/dist/types.js ADDED
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Type definitions for client-side authentication.
3
+ *
4
+ * @module
5
+ */
6
+ export {};
7
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG"}
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Convenience wrappers around the auth client methods.
3
+ *
4
+ * These delegate to the bonded auth client retrieved via `getClient()`.
5
+ *
6
+ * @module
7
+ */
8
+ import type { AuthResult, LoginCredentials, RegisterData, UserProfile } from './types.js';
9
+ /**
10
+ * Checks if the current user is authenticated.
11
+ *
12
+ * @returns `true` if the user has an active session.
13
+ */
14
+ export declare const isAuthenticated: () => boolean;
15
+ /**
16
+ * Returns the current user profile, or `null` if not authenticated.
17
+ * @returns The user profile, or `null` if not authenticated.
18
+ */
19
+ export declare const getUser: <T extends UserProfile = UserProfile>() => T | null;
20
+ /**
21
+ * Logs in with the given credentials (email/username + password).
22
+ *
23
+ * @param credentials - Email/username and password.
24
+ * @returns The auth result containing tokens and user profile.
25
+ */
26
+ export declare const login: (credentials: LoginCredentials) => Promise<AuthResult>;
27
+ /**
28
+ * Logs out the current user, clearing tokens and session state.
29
+ * @returns A promise that resolves when logout completes.
30
+ */
31
+ export declare const logout: () => Promise<void>;
32
+ /**
33
+ * Registers a new user account.
34
+ *
35
+ * @param data - Registration data (email, password, name, etc.).
36
+ * @returns The auth result containing tokens and user profile.
37
+ */
38
+ export declare const register: (data: RegisterData) => Promise<AuthResult>;
39
+ //# sourceMappingURL=utilities.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utilities.d.ts","sourceRoot":"","sources":["../src/utilities.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,KAAK,EAAE,UAAU,EAAE,gBAAgB,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAEzF;;;;GAIG;AACH,eAAO,MAAM,eAAe,QAAO,OAAwC,CAAA;AAE3E;;;GAGG;AACH,eAAO,MAAM,OAAO,GAAI,CAAC,SAAS,WAAW,GAAG,WAAW,OAAK,CAAC,GAAG,IAAgC,CAAA;AAEpG;;;;;GAKG;AACH,eAAO,MAAM,KAAK,GAAI,aAAa,gBAAgB,KAAG,OAAO,CAAC,UAAU,CACxC,CAAA;AAEhC;;;GAGG;AACH,eAAO,MAAM,MAAM,QAAO,OAAO,CAAC,IAAI,CAAyB,CAAA;AAE/D;;;;;GAKG;AACH,eAAO,MAAM,QAAQ,GAAI,MAAM,YAAY,KAAG,OAAO,CAAC,UAAU,CAA+B,CAAA"}
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Convenience wrappers around the auth client methods.
3
+ *
4
+ * These delegate to the bonded auth client retrieved via `getClient()`.
5
+ *
6
+ * @module
7
+ */
8
+ import { getClient } from './provider.js';
9
+ /**
10
+ * Checks if the current user is authenticated.
11
+ *
12
+ * @returns `true` if the user has an active session.
13
+ */
14
+ export const isAuthenticated = () => getClient().isAuthenticated();
15
+ /**
16
+ * Returns the current user profile, or `null` if not authenticated.
17
+ * @returns The user profile, or `null` if not authenticated.
18
+ */
19
+ export const getUser = () => getClient().getUser();
20
+ /**
21
+ * Logs in with the given credentials (email/username + password).
22
+ *
23
+ * @param credentials - Email/username and password.
24
+ * @returns The auth result containing tokens and user profile.
25
+ */
26
+ export const login = (credentials) => getClient().login(credentials);
27
+ /**
28
+ * Logs out the current user, clearing tokens and session state.
29
+ * @returns A promise that resolves when logout completes.
30
+ */
31
+ export const logout = () => getClient().logout();
32
+ /**
33
+ * Registers a new user account.
34
+ *
35
+ * @param data - Registration data (email, password, name, etc.).
36
+ * @returns The auth result containing tokens and user profile.
37
+ */
38
+ export const register = (data) => getClient().register(data);
39
+ //# sourceMappingURL=utilities.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utilities.js","sourceRoot":"","sources":["../src/utilities.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;AAGzC;;;;GAIG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,GAAY,EAAE,CAAC,SAAS,EAAE,CAAC,eAAe,EAAE,CAAA;AAE3E;;;GAGG;AACH,MAAM,CAAC,MAAM,OAAO,GAAG,GAAkD,EAAE,CAAC,SAAS,EAAK,CAAC,OAAO,EAAE,CAAA;AAEpG;;;;;GAKG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG,CAAC,WAA6B,EAAuB,EAAE,CAC1E,SAAS,EAAE,CAAC,KAAK,CAAC,WAAW,CAAC,CAAA;AAEhC;;;GAGG;AACH,MAAM,CAAC,MAAM,MAAM,GAAG,GAAkB,EAAE,CAAC,SAAS,EAAE,CAAC,MAAM,EAAE,CAAA;AAE/D;;;;;GAKG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,IAAkB,EAAuB,EAAE,CAAC,SAAS,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAA"}
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@molecule/app-auth",
3
+ "version": "1.0.0",
4
+ "description": "Client-side authentication interface for molecule.dev",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "scripts": {
9
+ "build": "tsc",
10
+ "test": "vitest run",
11
+ "test:watch": "vitest"
12
+ },
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "import": "./dist/index.js"
17
+ }
18
+ },
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "keywords": [
23
+ "molecule",
24
+ "auth",
25
+ "authentication",
26
+ "jwt"
27
+ ],
28
+ "license": "Apache-2.0",
29
+ "peerDependencies": {
30
+ "@molecule/app-bond": "^1.0.0",
31
+ "@molecule/app-i18n": "^1.0.0",
32
+ "@molecule/app-logger": "^1.0.0"
33
+ },
34
+ "devDependencies": {
35
+ "@molecule/app-bond": "1.0.0",
36
+ "@molecule/app-i18n": "1.0.0",
37
+ "@molecule/app-logger": "1.0.0",
38
+ "@types/node": "26.1.2",
39
+ "typescript": "6.0.3",
40
+ "vitest": "4.1.10"
41
+ },
42
+ "repository": {
43
+ "type": "git",
44
+ "url": "https://github.com/molecule-dev/molecule.git",
45
+ "directory": "packages/app/core/auth"
46
+ },
47
+ "homepage": "https://github.com/molecule-dev/molecule/tree/main/packages/app/core/auth",
48
+ "bugs": "https://github.com/molecule-dev/molecule/issues",
49
+ "publishConfig": {
50
+ "access": "public"
51
+ }
52
+ }