@classic-homes/auth 0.1.22 → 0.1.24

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,86 @@
1
+ /**
2
+ * Auth Configuration
3
+ *
4
+ * Manages global configuration for the auth package.
5
+ * Must be initialized via initAuth() before using auth services.
6
+ */
7
+ interface SSOConfig {
8
+ /** Whether SSO is enabled */
9
+ enabled: boolean;
10
+ /** SSO provider name (e.g., 'authentik', 'okta') */
11
+ provider: string;
12
+ /** Override the default authorize URL (defaults to /auth/sso/authorize) */
13
+ authorizeUrl?: string;
14
+ }
15
+ interface StorageAdapter {
16
+ getItem(key: string): string | null;
17
+ setItem(key: string, value: string): void;
18
+ removeItem(key: string): void;
19
+ }
20
+ interface AuthConfig {
21
+ /** Base URL for the API (e.g., 'https://api.example.com/v1') */
22
+ baseUrl: string;
23
+ /** Custom fetch implementation (useful for SSR or testing) */
24
+ fetch?: typeof fetch;
25
+ /** Storage adapter for token persistence (defaults to localStorage in browser) */
26
+ storage?: StorageAdapter;
27
+ /** Storage key prefix for auth data */
28
+ storageKey?: string;
29
+ /** Callback when auth errors occur (e.g., for logging or analytics) */
30
+ onAuthError?: (error: Error) => void;
31
+ /** Callback when tokens are refreshed */
32
+ onTokenRefresh?: (tokens: {
33
+ accessToken: string;
34
+ refreshToken: string;
35
+ }) => void;
36
+ /** Callback when user is logged out (e.g., for redirect) */
37
+ onLogout?: () => void;
38
+ /** SSO configuration */
39
+ sso?: SSOConfig;
40
+ }
41
+ /**
42
+ * Initialize the auth package with configuration.
43
+ * Must be called before using any auth services.
44
+ *
45
+ * @example
46
+ * ```typescript
47
+ * import { initAuth } from '@classic-homes/auth';
48
+ *
49
+ * initAuth({
50
+ * baseUrl: import.meta.env.PUBLIC_API_URL,
51
+ * sso: {
52
+ * enabled: true,
53
+ * provider: 'authentik',
54
+ * },
55
+ * });
56
+ * ```
57
+ */
58
+ declare function initAuth(options: AuthConfig): void;
59
+ /**
60
+ * Get the current auth configuration.
61
+ * Throws if initAuth() has not been called.
62
+ */
63
+ declare function getConfig(): AuthConfig;
64
+ /**
65
+ * Check if auth has been initialized.
66
+ */
67
+ declare function isInitialized(): boolean;
68
+ /**
69
+ * Reset the auth configuration (useful for testing).
70
+ */
71
+ declare function resetConfig(): void;
72
+ /**
73
+ * Get the default storage adapter.
74
+ * Returns localStorage in browser, or a no-op adapter in SSR.
75
+ */
76
+ declare function getDefaultStorage(): StorageAdapter;
77
+ /**
78
+ * Get the storage adapter from config or use default.
79
+ */
80
+ declare function getStorage(): StorageAdapter;
81
+ /**
82
+ * Get the fetch implementation from config or use global fetch.
83
+ */
84
+ declare function getFetch(): typeof fetch;
85
+
86
+ export { type AuthConfig as A, type SSOConfig as S, isInitialized as a, getDefaultStorage as b, getStorage as c, getFetch as d, type StorageAdapter as e, getConfig as g, initAuth as i, resetConfig as r };
@@ -1,90 +1,6 @@
1
- import { L as LoginCredentials, a as LoginResponse, R as RegisterData, b as RegisterResponse, U as User, j as ProfileUpdateData, k as ChangePasswordData, S as Session, c as ApiKey, C as CreateApiKeyRequest, d as CreateApiKeyResponse, e as MFAStatus, M as MFASetupResponse, f as MFAChallengeData, D as Device, g as UserPreferences, h as LinkedAccount, i as SecurityEvent, P as Pagination } from '../types-exFUQyBX.js';
2
- export { A as AuthState, l as ResetPasswordData } from '../types-exFUQyBX.js';
3
-
4
- /**
5
- * Auth Configuration
6
- *
7
- * Manages global configuration for the auth package.
8
- * Must be initialized via initAuth() before using auth services.
9
- */
10
- interface SSOConfig {
11
- /** Whether SSO is enabled */
12
- enabled: boolean;
13
- /** SSO provider name (e.g., 'authentik', 'okta') */
14
- provider: string;
15
- /** Override the default authorize URL (defaults to /auth/sso/authorize) */
16
- authorizeUrl?: string;
17
- }
18
- interface StorageAdapter {
19
- getItem(key: string): string | null;
20
- setItem(key: string, value: string): void;
21
- removeItem(key: string): void;
22
- }
23
- interface AuthConfig {
24
- /** Base URL for the API (e.g., 'https://api.example.com/v1') */
25
- baseUrl: string;
26
- /** Custom fetch implementation (useful for SSR or testing) */
27
- fetch?: typeof fetch;
28
- /** Storage adapter for token persistence (defaults to localStorage in browser) */
29
- storage?: StorageAdapter;
30
- /** Storage key prefix for auth data */
31
- storageKey?: string;
32
- /** Callback when auth errors occur (e.g., for logging or analytics) */
33
- onAuthError?: (error: Error) => void;
34
- /** Callback when tokens are refreshed */
35
- onTokenRefresh?: (tokens: {
36
- accessToken: string;
37
- refreshToken: string;
38
- }) => void;
39
- /** Callback when user is logged out (e.g., for redirect) */
40
- onLogout?: () => void;
41
- /** SSO configuration */
42
- sso?: SSOConfig;
43
- }
44
- /**
45
- * Initialize the auth package with configuration.
46
- * Must be called before using any auth services.
47
- *
48
- * @example
49
- * ```typescript
50
- * import { initAuth } from '@classic-homes/auth';
51
- *
52
- * initAuth({
53
- * baseUrl: import.meta.env.PUBLIC_API_URL,
54
- * sso: {
55
- * enabled: true,
56
- * provider: 'authentik',
57
- * },
58
- * });
59
- * ```
60
- */
61
- declare function initAuth(options: AuthConfig): void;
62
- /**
63
- * Get the current auth configuration.
64
- * Throws if initAuth() has not been called.
65
- */
66
- declare function getConfig(): AuthConfig;
67
- /**
68
- * Check if auth has been initialized.
69
- */
70
- declare function isInitialized(): boolean;
71
- /**
72
- * Reset the auth configuration (useful for testing).
73
- */
74
- declare function resetConfig(): void;
75
- /**
76
- * Get the default storage adapter.
77
- * Returns localStorage in browser, or a no-op adapter in SSR.
78
- */
79
- declare function getDefaultStorage(): StorageAdapter;
80
- /**
81
- * Get the storage adapter from config or use default.
82
- */
83
- declare function getStorage(): StorageAdapter;
84
- /**
85
- * Get the fetch implementation from config or use global fetch.
86
- */
87
- declare function getFetch(): typeof fetch;
1
+ export { A as AuthConfig, S as SSOConfig, e as StorageAdapter, g as getConfig, b as getDefaultStorage, d as getFetch, c as getStorage, i as initAuth, a as isInitialized, r as resetConfig } from '../config-C-iBNu07.js';
2
+ import { L as LoginCredentials, a as LoginResponse, b as LogoutResponse, R as RegisterData, c as RegisterResponse, U as User, k as ProfileUpdateData, l as ChangePasswordData, S as Session, d as ApiKey, C as CreateApiKeyRequest, e as CreateApiKeyResponse, f as MFAStatus, M as MFASetupResponse, g as MFAChallengeData, D as Device, h as UserPreferences, i as LinkedAccount, j as SecurityEvent, P as Pagination } from '../types-DGN45Uih.js';
3
+ export { A as AuthState, m as ResetPasswordData } from '../types-DGN45Uih.js';
88
4
 
89
5
  /**
90
6
  * HTTP Client
@@ -125,6 +41,7 @@ declare function getRefreshToken(): string | null;
125
41
  declare function getSessionToken(): string | null;
126
42
  /**
127
43
  * Update stored tokens.
44
+ * Also updates the Svelte auth store if available.
128
45
  */
129
46
  declare function updateStoredTokens(accessToken: string, refreshToken: string): void;
130
47
  /**
@@ -167,8 +84,9 @@ declare const authApi: {
167
84
  login(credentials: LoginCredentials): Promise<LoginResponse>;
168
85
  /**
169
86
  * Logout the current user.
87
+ * Returns SSO logout URL if applicable for SSO users.
170
88
  */
171
- logout(): Promise<void>;
89
+ logout(): Promise<LogoutResponse>;
172
90
  /**
173
91
  * Register a new user.
174
92
  */
@@ -190,8 +108,13 @@ declare const authApi: {
190
108
  }>;
191
109
  /**
192
110
  * Initiate SSO login by redirecting to the SSO provider.
111
+ * @param options.callbackUrl - The URL where the SSO provider should redirect after auth
112
+ * @param options.redirectUrl - The final URL to redirect to after processing the callback
193
113
  */
194
- initiateSSOLogin(): void;
114
+ initiateSSOLogin(options?: {
115
+ callbackUrl?: string;
116
+ redirectUrl?: string;
117
+ }): void;
195
118
  /**
196
119
  * Get the current user's profile.
197
120
  */
@@ -348,6 +271,22 @@ declare const authApi: {
348
271
  * Wraps authApi calls and provides a clean interface for components.
349
272
  */
350
273
 
274
+ interface LoginOptions {
275
+ /**
276
+ * Automatically update the auth store after successful login.
277
+ * Set to false to manually handle auth state.
278
+ * @default true
279
+ */
280
+ autoSetAuth?: boolean;
281
+ }
282
+ interface MFAVerifyOptions {
283
+ /**
284
+ * Automatically update the auth store after successful MFA verification.
285
+ * Set to false to manually handle auth state.
286
+ * @default true
287
+ */
288
+ autoSetAuth?: boolean;
289
+ }
351
290
  /**
352
291
  * AuthService
353
292
  *
@@ -357,12 +296,16 @@ declare const authApi: {
357
296
  declare class AuthService {
358
297
  /**
359
298
  * Login with username and password.
299
+ * By default, automatically sets the auth state on successful login (unless MFA is required).
300
+ * @param credentials - Username and password
301
+ * @param options - Optional settings for login behavior
360
302
  */
361
- login(credentials: LoginCredentials): Promise<LoginResponse>;
303
+ login(credentials: LoginCredentials, options?: LoginOptions): Promise<LoginResponse>;
362
304
  /**
363
305
  * Logout the current user.
306
+ * Returns SSO logout URL if applicable for SSO users.
364
307
  */
365
- logout(): Promise<void>;
308
+ logout(): Promise<LogoutResponse>;
366
309
  /**
367
310
  * Register a new user.
368
311
  */
@@ -388,8 +331,13 @@ declare class AuthService {
388
331
  }>;
389
332
  /**
390
333
  * Initiate SSO login (redirects to SSO provider).
334
+ * @param options.callbackUrl - The URL where the SSO provider should redirect after auth
335
+ * @param options.redirectUrl - The final URL to redirect to after processing the callback
391
336
  */
392
- initiateSSOLogin(): void;
337
+ initiateSSOLogin(options?: {
338
+ callbackUrl?: string;
339
+ redirectUrl?: string;
340
+ }): void;
393
341
  /**
394
342
  * Get the current user's profile.
395
343
  */
@@ -466,8 +414,11 @@ declare class AuthService {
466
414
  }>;
467
415
  /**
468
416
  * Verify MFA challenge during login.
417
+ * By default, automatically sets the auth state on successful verification.
418
+ * @param data - MFA challenge data including token and code
419
+ * @param options - Optional settings for verification behavior
469
420
  */
470
- verifyMFAChallenge(data: MFAChallengeData): Promise<LoginResponse>;
421
+ verifyMFAChallenge(data: MFAChallengeData, options?: MFAVerifyOptions): Promise<LoginResponse>;
471
422
  /**
472
423
  * Get all devices.
473
424
  */
@@ -535,6 +486,33 @@ declare class AuthService {
535
486
  /** Singleton instance of AuthService */
536
487
  declare const authService: AuthService;
537
488
 
489
+ /**
490
+ * Auth Guards
491
+ *
492
+ * Type guards and utility functions for checking authentication response types.
493
+ */
494
+
495
+ /**
496
+ * Check if a login response requires MFA verification.
497
+ * Returns true if the response indicates MFA is required.
498
+ */
499
+ declare function isMfaChallengeResponse(response: LoginResponse): boolean;
500
+ /**
501
+ * Check if a login response is a successful login (with tokens and user).
502
+ * Returns true if the response contains valid authentication data.
503
+ */
504
+ declare function isLoginSuccessResponse(response: LoginResponse): boolean;
505
+ /**
506
+ * Extract the MFA token from a login response.
507
+ * Returns the MFA challenge token or undefined if not present.
508
+ */
509
+ declare function getMfaToken(response: LoginResponse): string | undefined;
510
+ /**
511
+ * Get available MFA methods from a login response.
512
+ * Returns the list of available methods or defaults to ['totp'].
513
+ */
514
+ declare function getAvailableMethods(response: LoginResponse): string[];
515
+
538
516
  /**
539
517
  * JWT Utilities
540
518
  *
@@ -628,4 +606,4 @@ declare function getTokenExpiration(token: string): Date | null;
628
606
  */
629
607
  declare function extractClaims<T extends string>(token: string, claims: T[]): Pick<JWTPayload, T> | null;
630
608
 
631
- export { ApiKey, type ApiRequestOptions, type ApiResponse, type AuthConfig, AuthService, ChangePasswordData, CreateApiKeyRequest, CreateApiKeyResponse, Device, type JWTPayload, LinkedAccount, LoginCredentials, LoginResponse, MFAChallengeData, MFASetupResponse, MFAStatus, Pagination, ProfileUpdateData, RegisterData, RegisterResponse, type SSOConfig, SecurityEvent, Session, type StorageAdapter, User, UserPreferences, api, apiRequest, authApi, authService, clearStoredAuth, decodeJWT, extractClaims, extractData, getAccessToken, getConfig, getDefaultStorage, getFetch, getRefreshToken, getSessionToken, getStorage, getTokenExpiration, getTokenRemainingTime, initAuth, isInitialized, isTokenExpired, resetConfig, updateStoredTokens };
609
+ export { ApiKey, type ApiRequestOptions, type ApiResponse, AuthService, ChangePasswordData, CreateApiKeyRequest, CreateApiKeyResponse, Device, type JWTPayload, LinkedAccount, LoginCredentials, type LoginOptions, LoginResponse, LogoutResponse, MFAChallengeData, MFASetupResponse, MFAStatus, type MFAVerifyOptions, Pagination, ProfileUpdateData, RegisterData, RegisterResponse, SecurityEvent, Session, User, UserPreferences, api, apiRequest, authApi, authService, clearStoredAuth, decodeJWT, extractClaims, extractData, getAccessToken, getAvailableMethods, getMfaToken, getRefreshToken, getSessionToken, getTokenExpiration, getTokenRemainingTime, isLoginSuccessResponse, isMfaChallengeResponse, isTokenExpired, updateStoredTokens };