@djangocfg/api 2.1.57 → 2.1.58
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 +125 -9
- package/dist/auth.cjs +1865 -402
- package/dist/auth.cjs.map +1 -1
- package/dist/auth.d.cts +352 -76
- package/dist/auth.d.ts +352 -76
- package/dist/auth.mjs +1867 -404
- package/dist/auth.mjs.map +1 -1
- package/dist/clients.cjs +1637 -137
- package/dist/clients.cjs.map +1 -1
- package/dist/clients.d.cts +1394 -282
- package/dist/clients.d.ts +1394 -282
- package/dist/clients.mjs +1637 -137
- package/dist/clients.mjs.map +1 -1
- package/dist/hooks.cjs +24 -11
- package/dist/hooks.cjs.map +1 -1
- package/dist/hooks.d.cts +88 -21
- package/dist/hooks.d.ts +88 -21
- package/dist/hooks.mjs +24 -11
- package/dist/hooks.mjs.map +1 -1
- package/dist/index.cjs +38 -17
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +94 -21
- package/dist/index.d.ts +94 -21
- package/dist/index.mjs +38 -17
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
- package/src/auth/context/AccountsContext.tsx +8 -1
- package/src/auth/context/AuthContext.tsx +31 -8
- package/src/auth/context/types.ts +8 -1
- package/src/auth/hooks/index.ts +29 -5
- package/src/auth/hooks/useAuthForm.ts +292 -226
- package/src/auth/hooks/useAuthFormState.ts +60 -0
- package/src/auth/hooks/useAuthValidation.ts +77 -0
- package/src/auth/hooks/useGithubAuth.ts +26 -5
- package/src/auth/hooks/useTwoFactor.ts +239 -0
- package/src/auth/hooks/useTwoFactorSetup.ts +213 -0
- package/src/auth/index.ts +3 -0
- package/src/auth/types/form.ts +194 -0
- package/src/auth/types/index.ts +28 -0
- package/src/clients.ts +10 -0
- package/src/generated/cfg_accounts/_utils/schemas/OAuthTokenResponse.schema.ts +26 -3
- package/src/generated/cfg_accounts/_utils/schemas/OTPVerifyResponse.schema.ts +26 -3
- package/src/generated/cfg_accounts/accounts/client.ts +4 -1
- package/src/generated/cfg_accounts/accounts/models.ts +15 -6
- package/src/generated/cfg_accounts/accounts__oauth/models.ts +16 -7
- package/src/generated/cfg_accounts/client.ts +5 -2
- package/src/generated/cfg_accounts/http.ts +8 -2
- package/src/generated/cfg_accounts/schema.json +47 -19
- package/src/generated/cfg_centrifugo/client.ts +5 -2
- package/src/generated/cfg_centrifugo/http.ts +8 -2
- package/src/generated/cfg_totp/CLAUDE.md +12 -12
- package/src/generated/cfg_totp/_utils/fetchers/index.ts +3 -3
- package/src/generated/cfg_totp/_utils/fetchers/{totp__2fa_management.ts → totp__totp_management.ts} +3 -3
- package/src/generated/cfg_totp/_utils/fetchers/{totp__2fa_setup.ts → totp__totp_setup.ts} +3 -3
- package/src/generated/cfg_totp/_utils/fetchers/{totp__2fa_verification.ts → totp__totp_verification.ts} +3 -3
- package/src/generated/cfg_totp/_utils/hooks/index.ts +3 -3
- package/src/generated/cfg_totp/_utils/hooks/{totp__2fa_management.ts → totp__totp_management.ts} +2 -2
- package/src/generated/cfg_totp/_utils/hooks/{totp__2fa_setup.ts → totp__totp_setup.ts} +2 -2
- package/src/generated/cfg_totp/_utils/hooks/{totp__2fa_verification.ts → totp__totp_verification.ts} +2 -2
- package/src/generated/cfg_totp/_utils/schemas/DeviceList.schema.ts +1 -1
- package/src/generated/cfg_totp/client.ts +14 -11
- package/src/generated/cfg_totp/http.ts +8 -2
- package/src/generated/cfg_totp/index.ts +16 -16
- package/src/generated/cfg_totp/schema.json +8 -7
- package/src/generated/cfg_totp/{totp__2fa_management → totp__totp_management}/client.ts +2 -2
- package/src/generated/cfg_totp/{totp__2fa_management → totp__totp_management}/models.ts +1 -1
- package/src/generated/cfg_totp/{totp__2fa_setup → totp__totp_setup}/client.ts +4 -4
- package/src/generated/cfg_totp/{totp__2fa_verification → totp__totp_verification}/client.ts +2 -2
- package/src/generated/cfg_webpush/client.ts +5 -2
- package/src/generated/cfg_webpush/http.ts +8 -2
- /package/src/generated/cfg_totp/{totp__2fa_management → totp__totp_management}/index.ts +0 -0
- /package/src/generated/cfg_totp/{totp__2fa_setup → totp__totp_setup}/index.ts +0 -0
- /package/src/generated/cfg_totp/{totp__2fa_setup → totp__totp_setup}/models.ts +0 -0
- /package/src/generated/cfg_totp/{totp__2fa_verification → totp__totp_verification}/index.ts +0 -0
- /package/src/generated/cfg_totp/{totp__2fa_verification → totp__totp_verification}/models.ts +0 -0
package/dist/auth.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React$1, { ReactNode } from 'react';
|
|
1
|
+
import React$1, { ReactNode, MutableRefObject } from 'react';
|
|
2
2
|
import { z } from 'zod';
|
|
3
3
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
4
4
|
import * as consola from 'consola';
|
|
@@ -86,15 +86,37 @@ type OTPVerifyRequest = z.infer<typeof OTPVerifyRequestSchema>;
|
|
|
86
86
|
*
|
|
87
87
|
* This schema provides runtime validation and type inference.
|
|
88
88
|
* * OTP verification response.
|
|
89
|
+
|
|
90
|
+
When 2FA is required:
|
|
91
|
+
- requires_2fa: True
|
|
92
|
+
- session_id: UUID of 2FA verification session
|
|
93
|
+
- refresh/access/user: null
|
|
94
|
+
|
|
95
|
+
When 2FA is not required:
|
|
96
|
+
- requires_2fa: False
|
|
97
|
+
- session_id: null
|
|
98
|
+
- refresh/access/user: populated
|
|
89
99
|
* */
|
|
90
100
|
|
|
91
101
|
/**
|
|
92
102
|
* OTP verification response.
|
|
103
|
+
|
|
104
|
+
When 2FA is required:
|
|
105
|
+
- requires_2fa: True
|
|
106
|
+
- session_id: UUID of 2FA verification session
|
|
107
|
+
- refresh/access/user: null
|
|
108
|
+
|
|
109
|
+
When 2FA is not required:
|
|
110
|
+
- requires_2fa: False
|
|
111
|
+
- session_id: null
|
|
112
|
+
- refresh/access/user: populated
|
|
93
113
|
*/
|
|
94
114
|
declare const OTPVerifyResponseSchema: z.ZodObject<{
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
115
|
+
requires_2fa: z.ZodOptional<z.ZodBoolean>;
|
|
116
|
+
session_id: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
117
|
+
refresh: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
118
|
+
access: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
119
|
+
user: z.ZodOptional<z.ZodNullable<z.ZodObject<{
|
|
98
120
|
id: z.ZodInt;
|
|
99
121
|
email: z.ZodEmail;
|
|
100
122
|
first_name: z.ZodOptional<z.ZodString>;
|
|
@@ -117,7 +139,8 @@ declare const OTPVerifyResponseSchema: z.ZodObject<{
|
|
|
117
139
|
expires_at: z.ZodISODateTime;
|
|
118
140
|
channels: z.ZodArray<z.ZodString>;
|
|
119
141
|
}, z.core.$strip>>;
|
|
120
|
-
}, z.core.$strip
|
|
142
|
+
}, z.core.$strip>>>;
|
|
143
|
+
should_prompt_2fa: z.ZodOptional<z.ZodBoolean>;
|
|
121
144
|
}, z.core.$strip>;
|
|
122
145
|
/**
|
|
123
146
|
* Infer TypeScript type from Zod schema
|
|
@@ -260,10 +283,13 @@ interface AuthContextType {
|
|
|
260
283
|
success: boolean;
|
|
261
284
|
message: string;
|
|
262
285
|
}>;
|
|
263
|
-
verifyOTP: (identifier: string, otpCode: string, channel?: 'email' | 'phone', sourceUrl?: string, redirectUrl?: string) => Promise<{
|
|
286
|
+
verifyOTP: (identifier: string, otpCode: string, channel?: 'email' | 'phone', sourceUrl?: string, redirectUrl?: string, skipRedirect?: boolean) => Promise<{
|
|
264
287
|
success: boolean;
|
|
265
288
|
message: string;
|
|
266
289
|
user?: UserProfile;
|
|
290
|
+
requires_2fa?: boolean;
|
|
291
|
+
session_id?: string | null;
|
|
292
|
+
should_prompt_2fa?: boolean;
|
|
267
293
|
}>;
|
|
268
294
|
refreshToken: () => Promise<{
|
|
269
295
|
success: boolean;
|
|
@@ -302,76 +328,92 @@ interface AccountsProviderProps {
|
|
|
302
328
|
declare function AccountsProvider({ children }: AccountsProviderProps): react_jsx_runtime.JSX.Element;
|
|
303
329
|
declare function useAccountsContext(): AccountsContextValue;
|
|
304
330
|
|
|
305
|
-
interface AuthRedirectOptions {
|
|
306
|
-
fallbackUrl?: string;
|
|
307
|
-
clearOnUse?: boolean;
|
|
308
|
-
}
|
|
309
|
-
declare const useAuthRedirectManager: (options?: AuthRedirectOptions) => {
|
|
310
|
-
redirectUrl: string;
|
|
311
|
-
setRedirect: (url: string) => void;
|
|
312
|
-
getRedirect: () => string;
|
|
313
|
-
clearRedirect: () => void;
|
|
314
|
-
hasRedirect: () => boolean;
|
|
315
|
-
getFinalRedirectUrl: () => string;
|
|
316
|
-
useAndClearRedirect: () => string;
|
|
317
|
-
};
|
|
318
|
-
|
|
319
|
-
interface UseAuthGuardOptions {
|
|
320
|
-
redirectTo?: string;
|
|
321
|
-
requireAuth?: boolean;
|
|
322
|
-
/** Whether to save current URL for redirect after auth (default: true) */
|
|
323
|
-
saveRedirectUrl?: boolean;
|
|
324
|
-
}
|
|
325
|
-
declare const useAuthGuard: (options?: UseAuthGuardOptions) => {
|
|
326
|
-
isAuthenticated: boolean;
|
|
327
|
-
isLoading: boolean;
|
|
328
|
-
isRedirecting: boolean;
|
|
329
|
-
};
|
|
330
|
-
|
|
331
|
-
/**
|
|
332
|
-
* Simple sessionStorage hook with better error handling
|
|
333
|
-
* @param key - Storage key
|
|
334
|
-
* @param initialValue - Default value if key doesn't exist
|
|
335
|
-
* @returns [value, setValue, removeValue] - Current value, setter function, and remove function
|
|
336
|
-
*/
|
|
337
|
-
declare function useSessionStorage<T>(key: string, initialValue: T): readonly [T, (value: T | ((val: T) => T)) => void, () => void];
|
|
338
|
-
|
|
339
331
|
/**
|
|
340
|
-
*
|
|
341
|
-
*
|
|
342
|
-
*
|
|
343
|
-
*
|
|
332
|
+
* Auth Form Types
|
|
333
|
+
*
|
|
334
|
+
* Single source of truth for auth form state and handlers.
|
|
335
|
+
* Used by both @djangocfg/api/auth hooks and @djangocfg/layouts/AuthLayout.
|
|
344
336
|
*/
|
|
345
|
-
declare function useLocalStorage<T>(key: string, initialValue: T): readonly [T, (value: T | ((val: T) => T)) => void, () => void];
|
|
346
337
|
|
|
338
|
+
type AuthChannel = 'email' | 'phone';
|
|
339
|
+
type AuthStep = 'identifier' | 'otp' | '2fa' | '2fa-setup' | 'success';
|
|
347
340
|
interface AuthFormState {
|
|
341
|
+
/** Email or phone number */
|
|
348
342
|
identifier: string;
|
|
349
|
-
|
|
343
|
+
/** Current auth channel */
|
|
344
|
+
channel: AuthChannel;
|
|
345
|
+
/** OTP code input */
|
|
350
346
|
otp: string;
|
|
347
|
+
/** Loading state */
|
|
351
348
|
isLoading: boolean;
|
|
349
|
+
/** Terms acceptance state */
|
|
352
350
|
acceptedTerms: boolean;
|
|
353
|
-
|
|
351
|
+
/** Current form step */
|
|
352
|
+
step: AuthStep;
|
|
353
|
+
/** Error message */
|
|
354
354
|
error: string;
|
|
355
|
+
/** 2FA session ID from OTP/OAuth verification */
|
|
356
|
+
twoFactorSessionId: string | null;
|
|
357
|
+
/** Whether user should be prompted to enable 2FA */
|
|
358
|
+
shouldPrompt2FA: boolean;
|
|
359
|
+
/** 2FA code input */
|
|
360
|
+
twoFactorCode: string;
|
|
361
|
+
/** Using backup code instead of TOTP */
|
|
362
|
+
useBackupCode: boolean;
|
|
355
363
|
}
|
|
356
|
-
interface
|
|
364
|
+
interface AuthFormStateHandlers {
|
|
357
365
|
setIdentifier: (identifier: string) => void;
|
|
358
|
-
setChannel: (channel:
|
|
366
|
+
setChannel: (channel: AuthChannel) => void;
|
|
359
367
|
setOtp: (otp: string) => void;
|
|
360
368
|
setAcceptedTerms: (accepted: boolean) => void;
|
|
361
369
|
setError: (error: string) => void;
|
|
362
370
|
clearError: () => void;
|
|
371
|
+
setStep: (step: AuthStep) => void;
|
|
372
|
+
setIsLoading: (loading: boolean) => void;
|
|
373
|
+
setTwoFactorSessionId: (sessionId: string | null) => void;
|
|
374
|
+
setShouldPrompt2FA: (prompt: boolean) => void;
|
|
375
|
+
setTwoFactorCode: (code: string) => void;
|
|
376
|
+
setUseBackupCode: (useBackup: boolean) => void;
|
|
377
|
+
}
|
|
378
|
+
interface AuthFormSubmitHandlers {
|
|
363
379
|
handleIdentifierSubmit: (e: React.FormEvent) => Promise<void>;
|
|
364
380
|
handleOTPSubmit: (e: React.FormEvent) => Promise<void>;
|
|
365
381
|
handleResendOTP: () => Promise<void>;
|
|
366
382
|
handleBackToIdentifier: () => void;
|
|
367
383
|
forceOTPStep: () => void;
|
|
368
|
-
|
|
369
|
-
|
|
384
|
+
/** Handle 2FA TOTP/backup code verification */
|
|
385
|
+
handle2FASubmit: (e: React.FormEvent) => Promise<void>;
|
|
386
|
+
/** Switch to backup code input */
|
|
387
|
+
handleUseBackupCode: () => void;
|
|
388
|
+
/** Switch back to TOTP input */
|
|
389
|
+
handleUseTOTP: () => void;
|
|
390
|
+
}
|
|
391
|
+
interface AuthFormValidation {
|
|
392
|
+
/** Detect channel from identifier string */
|
|
393
|
+
detectChannelFromIdentifier: (identifier: string) => AuthChannel | null;
|
|
394
|
+
/** Validate identifier format */
|
|
395
|
+
validateIdentifier: (identifier: string, channel?: AuthChannel) => boolean;
|
|
396
|
+
}
|
|
397
|
+
interface AuthFormAutoSubmit {
|
|
398
|
+
/** Ref to track if auto-submit from URL is in progress */
|
|
399
|
+
isAutoSubmittingFromUrl: MutableRefObject<boolean>;
|
|
400
|
+
}
|
|
401
|
+
interface AuthForm2FAState {
|
|
402
|
+
/** Loading state for 2FA verification */
|
|
403
|
+
is2FALoading: boolean;
|
|
404
|
+
/** Warning message from 2FA (e.g., low backup codes) */
|
|
405
|
+
twoFactorWarning: string | null;
|
|
406
|
+
}
|
|
407
|
+
interface AuthFormReturn extends AuthFormState, AuthFormStateHandlers, AuthFormSubmitHandlers, AuthFormValidation, AuthFormAutoSubmit, AuthForm2FAState {
|
|
370
408
|
}
|
|
371
409
|
interface UseAuthFormOptions {
|
|
372
|
-
|
|
410
|
+
/** Callback when identifier step succeeds */
|
|
411
|
+
onIdentifierSuccess?: (identifier: string, channel: AuthChannel) => void;
|
|
412
|
+
/** Callback when OTP verification succeeds */
|
|
373
413
|
onOTPSuccess?: () => void;
|
|
414
|
+
/** Callback on any error */
|
|
374
415
|
onError?: (message: string) => void;
|
|
416
|
+
/** Source URL for tracking */
|
|
375
417
|
sourceUrl: string;
|
|
376
418
|
/** URL to redirect after successful OTP verification */
|
|
377
419
|
redirectUrl?: string;
|
|
@@ -380,7 +422,66 @@ interface UseAuthFormOptions {
|
|
|
380
422
|
/** Path to auth page for auto-OTP detection. Default: '/auth' */
|
|
381
423
|
authPath?: string;
|
|
382
424
|
}
|
|
383
|
-
|
|
425
|
+
interface AuthLayoutConfig {
|
|
426
|
+
/** Support page URL */
|
|
427
|
+
supportUrl?: string;
|
|
428
|
+
/** Terms of service URL */
|
|
429
|
+
termsUrl?: string;
|
|
430
|
+
/** Privacy policy URL */
|
|
431
|
+
privacyUrl?: string;
|
|
432
|
+
/** Source URL for tracking */
|
|
433
|
+
sourceUrl: string;
|
|
434
|
+
/** Enable phone authentication tab */
|
|
435
|
+
enablePhoneAuth?: boolean;
|
|
436
|
+
/** Enable GitHub OAuth button */
|
|
437
|
+
enableGithubAuth?: boolean;
|
|
438
|
+
/** Logo URL for success screen (SVG recommended) */
|
|
439
|
+
logoUrl?: string;
|
|
440
|
+
/** URL to redirect after successful auth (default: /dashboard) */
|
|
441
|
+
redirectUrl?: string;
|
|
442
|
+
}
|
|
443
|
+
interface AuthFormContextType extends AuthFormReturn, AuthLayoutConfig {
|
|
444
|
+
}
|
|
445
|
+
interface AuthLayoutProps extends AuthLayoutConfig {
|
|
446
|
+
children?: React.ReactNode;
|
|
447
|
+
className?: string;
|
|
448
|
+
/** URL to redirect after successful auth (default: /dashboard) */
|
|
449
|
+
redirectUrl?: string;
|
|
450
|
+
/** Callback when identifier step succeeds */
|
|
451
|
+
onIdentifierSuccess?: (identifier: string, channel: AuthChannel) => void;
|
|
452
|
+
/** Callback when OTP verification succeeds */
|
|
453
|
+
onOTPSuccess?: () => void;
|
|
454
|
+
/** Callback when OAuth succeeds */
|
|
455
|
+
onOAuthSuccess?: (user: any, isNewUser: boolean, provider: string) => void;
|
|
456
|
+
/** Callback on any error */
|
|
457
|
+
onError?: (message: string) => void;
|
|
458
|
+
}
|
|
459
|
+
interface AuthHelpProps {
|
|
460
|
+
className?: string;
|
|
461
|
+
variant?: 'default' | 'compact';
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
interface UseAuthFormStateReturn extends AuthFormState, AuthFormStateHandlers {
|
|
465
|
+
}
|
|
466
|
+
/**
|
|
467
|
+
* Hook for auth form state management.
|
|
468
|
+
* Pure state - no side effects, no API calls.
|
|
469
|
+
*/
|
|
470
|
+
declare const useAuthFormState: (initialIdentifier?: string, initialChannel?: AuthChannel) => UseAuthFormStateReturn;
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* Hook for auth identifier validation.
|
|
474
|
+
* Pure functions - no state, no side effects.
|
|
475
|
+
*/
|
|
476
|
+
declare const useAuthValidation: () => AuthFormValidation;
|
|
477
|
+
declare const detectChannelFromIdentifier: (id: string) => AuthChannel | null;
|
|
478
|
+
declare const validateIdentifier: (id: string, channelType?: AuthChannel) => boolean;
|
|
479
|
+
|
|
480
|
+
/**
|
|
481
|
+
* Complete auth form hook.
|
|
482
|
+
* Composes smaller hooks for state, validation, and submission.
|
|
483
|
+
*/
|
|
484
|
+
declare const useAuthForm: (options: UseAuthFormOptions) => AuthFormReturn;
|
|
384
485
|
|
|
385
486
|
interface UseAutoAuthOptions {
|
|
386
487
|
onOTPDetected?: (otp: string) => void;
|
|
@@ -402,7 +503,11 @@ interface UseGithubAuthOptions {
|
|
|
402
503
|
sourceUrl?: string;
|
|
403
504
|
onSuccess?: (user: any, isNewUser: boolean) => void;
|
|
404
505
|
onError?: (error: string) => void;
|
|
506
|
+
/** Callback when 2FA is required */
|
|
507
|
+
onRequires2FA?: (sessionId: string, shouldPrompt2FA: boolean) => void;
|
|
405
508
|
redirectUrl?: string;
|
|
509
|
+
/** Skip automatic redirect after success (caller handles navigation) */
|
|
510
|
+
skipRedirect?: boolean;
|
|
406
511
|
}
|
|
407
512
|
interface UseGithubAuthReturn {
|
|
408
513
|
isLoading: boolean;
|
|
@@ -431,6 +536,199 @@ interface UseGithubAuthReturn {
|
|
|
431
536
|
*/
|
|
432
537
|
declare const useGithubAuth: (options?: UseGithubAuthOptions) => UseGithubAuthReturn;
|
|
433
538
|
|
|
539
|
+
interface UseTwoFactorOptions {
|
|
540
|
+
/** Callback on successful 2FA verification */
|
|
541
|
+
onSuccess?: (user: any) => void;
|
|
542
|
+
/** Callback on error */
|
|
543
|
+
onError?: (error: string) => void;
|
|
544
|
+
/** URL to redirect after successful verification */
|
|
545
|
+
redirectUrl?: string;
|
|
546
|
+
/** Skip automatic redirect after success (caller handles navigation) */
|
|
547
|
+
skipRedirect?: boolean;
|
|
548
|
+
}
|
|
549
|
+
interface UseTwoFactorReturn {
|
|
550
|
+
/** Loading state */
|
|
551
|
+
isLoading: boolean;
|
|
552
|
+
/** Error message */
|
|
553
|
+
error: string | null;
|
|
554
|
+
/** Warning message (e.g., low backup codes) */
|
|
555
|
+
warning: string | null;
|
|
556
|
+
/** Remaining backup codes (if backup code was used) */
|
|
557
|
+
remainingBackupCodes: number | null;
|
|
558
|
+
/** Verify TOTP code */
|
|
559
|
+
verifyTOTP: (sessionId: string, code: string) => Promise<boolean>;
|
|
560
|
+
/** Verify backup code */
|
|
561
|
+
verifyBackupCode: (sessionId: string, backupCode: string) => Promise<boolean>;
|
|
562
|
+
/** Clear error */
|
|
563
|
+
clearError: () => void;
|
|
564
|
+
}
|
|
565
|
+
/**
|
|
566
|
+
* Hook for 2FA verification during login.
|
|
567
|
+
*
|
|
568
|
+
* Usage:
|
|
569
|
+
* 1. After OTP/OAuth verification returns requires_2fa=true and session_id
|
|
570
|
+
* 2. Show 2FA form and collect TOTP code from user
|
|
571
|
+
* 3. Call verifyTOTP(sessionId, code) to complete authentication
|
|
572
|
+
*
|
|
573
|
+
* @example
|
|
574
|
+
* ```tsx
|
|
575
|
+
* const { isLoading, error, verifyTOTP, verifyBackupCode } = useTwoFactor({
|
|
576
|
+
* onSuccess: (user) => console.log('Logged in:', user),
|
|
577
|
+
* onError: (error) => console.error(error),
|
|
578
|
+
* redirectUrl: '/dashboard',
|
|
579
|
+
* });
|
|
580
|
+
*
|
|
581
|
+
* const handleSubmit = async (code: string) => {
|
|
582
|
+
* if (useBackupCode) {
|
|
583
|
+
* await verifyBackupCode(sessionId, code);
|
|
584
|
+
* } else {
|
|
585
|
+
* await verifyTOTP(sessionId, code);
|
|
586
|
+
* }
|
|
587
|
+
* };
|
|
588
|
+
* ```
|
|
589
|
+
*/
|
|
590
|
+
declare const useTwoFactor: (options?: UseTwoFactorOptions) => UseTwoFactorReturn;
|
|
591
|
+
|
|
592
|
+
interface TwoFactorSetupData {
|
|
593
|
+
/** Device ID to use for confirmation */
|
|
594
|
+
deviceId: string;
|
|
595
|
+
/** Base32-encoded TOTP secret (for manual entry) */
|
|
596
|
+
secret: string;
|
|
597
|
+
/** otpauth:// URI for QR code generation */
|
|
598
|
+
provisioningUri: string;
|
|
599
|
+
/** Base64-encoded QR code image (data URI) */
|
|
600
|
+
qrCodeBase64: string;
|
|
601
|
+
/** Seconds until setup expires */
|
|
602
|
+
expiresIn: number;
|
|
603
|
+
}
|
|
604
|
+
interface UseTwoFactorSetupOptions {
|
|
605
|
+
/** Callback when setup is confirmed and backup codes are generated */
|
|
606
|
+
onComplete?: (backupCodes: string[]) => void;
|
|
607
|
+
/** Callback on error */
|
|
608
|
+
onError?: (error: string) => void;
|
|
609
|
+
}
|
|
610
|
+
interface UseTwoFactorSetupReturn {
|
|
611
|
+
/** Loading state */
|
|
612
|
+
isLoading: boolean;
|
|
613
|
+
/** Error message */
|
|
614
|
+
error: string | null;
|
|
615
|
+
/** Setup data (QR code, secret, etc.) */
|
|
616
|
+
setupData: TwoFactorSetupData | null;
|
|
617
|
+
/** Backup codes after confirmation */
|
|
618
|
+
backupCodes: string[] | null;
|
|
619
|
+
/** Warning message about backup codes */
|
|
620
|
+
backupCodesWarning: string | null;
|
|
621
|
+
/** Current setup step */
|
|
622
|
+
setupStep: 'idle' | 'scanning' | 'confirming' | 'complete';
|
|
623
|
+
/** Start 2FA setup - returns QR code data */
|
|
624
|
+
startSetup: (deviceName?: string) => Promise<TwoFactorSetupData | null>;
|
|
625
|
+
/** Confirm setup with TOTP code - returns backup codes */
|
|
626
|
+
confirmSetup: (code: string) => Promise<string[] | null>;
|
|
627
|
+
/** Reset setup state */
|
|
628
|
+
resetSetup: () => void;
|
|
629
|
+
/** Clear error */
|
|
630
|
+
clearError: () => void;
|
|
631
|
+
}
|
|
632
|
+
/**
|
|
633
|
+
* Hook for 2FA setup (enabling TOTP authentication).
|
|
634
|
+
*
|
|
635
|
+
* Flow:
|
|
636
|
+
* 1. Call startSetup() to get QR code and provisioning URI
|
|
637
|
+
* 2. User scans QR code with authenticator app
|
|
638
|
+
* 3. Call confirmSetup(code) with the 6-digit code from app
|
|
639
|
+
* 4. Show backup codes to user (they must save these!)
|
|
640
|
+
*
|
|
641
|
+
* @example
|
|
642
|
+
* ```tsx
|
|
643
|
+
* const {
|
|
644
|
+
* isLoading,
|
|
645
|
+
* error,
|
|
646
|
+
* setupData,
|
|
647
|
+
* backupCodes,
|
|
648
|
+
* setupStep,
|
|
649
|
+
* startSetup,
|
|
650
|
+
* confirmSetup,
|
|
651
|
+
* } = useTwoFactorSetup({
|
|
652
|
+
* onComplete: (codes) => console.log('Backup codes:', codes),
|
|
653
|
+
* onError: (error) => console.error(error),
|
|
654
|
+
* });
|
|
655
|
+
*
|
|
656
|
+
* // Start setup
|
|
657
|
+
* const data = await startSetup('My iPhone');
|
|
658
|
+
*
|
|
659
|
+
* // Show QR code
|
|
660
|
+
* <QRCodeSVG value={data.provisioningUri} />
|
|
661
|
+
*
|
|
662
|
+
* // Confirm with code from app
|
|
663
|
+
* const codes = await confirmSetup('123456');
|
|
664
|
+
* ```
|
|
665
|
+
*/
|
|
666
|
+
declare const useTwoFactorSetup: (options?: UseTwoFactorSetupOptions) => UseTwoFactorSetupReturn;
|
|
667
|
+
|
|
668
|
+
interface AuthRedirectOptions {
|
|
669
|
+
fallbackUrl?: string;
|
|
670
|
+
clearOnUse?: boolean;
|
|
671
|
+
}
|
|
672
|
+
declare const useAuthRedirectManager: (options?: AuthRedirectOptions) => {
|
|
673
|
+
redirectUrl: string;
|
|
674
|
+
setRedirect: (url: string) => void;
|
|
675
|
+
getRedirect: () => string;
|
|
676
|
+
clearRedirect: () => void;
|
|
677
|
+
hasRedirect: () => boolean;
|
|
678
|
+
getFinalRedirectUrl: () => string;
|
|
679
|
+
useAndClearRedirect: () => string;
|
|
680
|
+
};
|
|
681
|
+
|
|
682
|
+
interface UseAuthGuardOptions {
|
|
683
|
+
redirectTo?: string;
|
|
684
|
+
requireAuth?: boolean;
|
|
685
|
+
/** Whether to save current URL for redirect after auth (default: true) */
|
|
686
|
+
saveRedirectUrl?: boolean;
|
|
687
|
+
}
|
|
688
|
+
declare const useAuthGuard: (options?: UseAuthGuardOptions) => {
|
|
689
|
+
isAuthenticated: boolean;
|
|
690
|
+
isLoading: boolean;
|
|
691
|
+
isRedirecting: boolean;
|
|
692
|
+
};
|
|
693
|
+
|
|
694
|
+
/**
|
|
695
|
+
* Simple sessionStorage hook with better error handling
|
|
696
|
+
* @param key - Storage key
|
|
697
|
+
* @param initialValue - Default value if key doesn't exist
|
|
698
|
+
* @returns [value, setValue, removeValue] - Current value, setter function, and remove function
|
|
699
|
+
*/
|
|
700
|
+
declare function useSessionStorage<T>(key: string, initialValue: T): readonly [T, (value: T | ((val: T) => T)) => void, () => void];
|
|
701
|
+
|
|
702
|
+
/**
|
|
703
|
+
* Simple localStorage hook with better error handling
|
|
704
|
+
* @param key - Storage key
|
|
705
|
+
* @param initialValue - Default value if key doesn't exist
|
|
706
|
+
* @returns [value, setValue, removeValue] - Current value, setter function, and remove function
|
|
707
|
+
*/
|
|
708
|
+
declare function useLocalStorage<T>(key: string, initialValue: T): readonly [T, (value: T | ((val: T) => T)) => void, () => void];
|
|
709
|
+
|
|
710
|
+
/**
|
|
711
|
+
* Encode string to base64
|
|
712
|
+
* @param data - String data to encode
|
|
713
|
+
* @returns Base64 encoded string (or original in dev mode)
|
|
714
|
+
*/
|
|
715
|
+
declare function encodeBase64(data: string): string;
|
|
716
|
+
/**
|
|
717
|
+
* Decode base64 to string
|
|
718
|
+
* @param encoded - Base64 encoded string
|
|
719
|
+
* @returns Decoded string (or original in dev mode)
|
|
720
|
+
*/
|
|
721
|
+
declare function decodeBase64(encoded: string): string;
|
|
722
|
+
/**
|
|
723
|
+
* Hook for base64 encoding/decoding
|
|
724
|
+
* @returns Object with encode and decode functions
|
|
725
|
+
*/
|
|
726
|
+
declare function useBase64(): {
|
|
727
|
+
encode: typeof encodeBase64;
|
|
728
|
+
decode: typeof decodeBase64;
|
|
729
|
+
isDev: boolean;
|
|
730
|
+
};
|
|
731
|
+
|
|
434
732
|
/**
|
|
435
733
|
* Profile Cache Hook
|
|
436
734
|
*
|
|
@@ -473,28 +771,6 @@ declare function getCacheMetadata(): {
|
|
|
473
771
|
expiresIn?: number;
|
|
474
772
|
} | null;
|
|
475
773
|
|
|
476
|
-
/**
|
|
477
|
-
* Encode string to base64
|
|
478
|
-
* @param data - String data to encode
|
|
479
|
-
* @returns Base64 encoded string (or original in dev mode)
|
|
480
|
-
*/
|
|
481
|
-
declare function encodeBase64(data: string): string;
|
|
482
|
-
/**
|
|
483
|
-
* Decode base64 to string
|
|
484
|
-
* @param encoded - Base64 encoded string
|
|
485
|
-
* @returns Decoded string (or original in dev mode)
|
|
486
|
-
*/
|
|
487
|
-
declare function decodeBase64(encoded: string): string;
|
|
488
|
-
/**
|
|
489
|
-
* Hook for base64 encoding/decoding
|
|
490
|
-
* @returns Object with encode and decode functions
|
|
491
|
-
*/
|
|
492
|
-
declare function useBase64(): {
|
|
493
|
-
encode: typeof encodeBase64;
|
|
494
|
-
decode: typeof decodeBase64;
|
|
495
|
-
isDev: boolean;
|
|
496
|
-
};
|
|
497
|
-
|
|
498
774
|
/**
|
|
499
775
|
* Email validation utility
|
|
500
776
|
*/
|
|
@@ -554,4 +830,4 @@ declare const Analytics: {
|
|
|
554
830
|
setUser(userId: string): void;
|
|
555
831
|
};
|
|
556
832
|
|
|
557
|
-
export { type AccountsContextValue, AccountsProvider, Analytics, AnalyticsCategory, type AnalyticsCategoryType, AnalyticsEvent, type AnalyticsEventType, type AuthConfig, type AuthContextType, AuthProvider, type AuthProviderProps, type PatchedUserProfileUpdateRequest, PatchedUserProfileUpdateRequestSchema, type ProfileCacheOptions, type UseGithubAuthOptions, type UseGithubAuthReturn, type UserProfile, authLogger, clearProfileCache, decodeBase64, encodeBase64, formatAuthError, getCacheMetadata, getCachedProfile, hasValidCache, logger, setCachedProfile, useAccountsContext, useAuth, useAuthForm, useAuthGuard, useAuthRedirectManager, useAutoAuth, useBase64, useGithubAuth, useLocalStorage, useSessionStorage, validateEmail };
|
|
833
|
+
export { type AccountsContextValue, AccountsProvider, Analytics, AnalyticsCategory, type AnalyticsCategoryType, AnalyticsEvent, type AnalyticsEventType, type AuthChannel, type AuthConfig, type AuthContextType, type AuthFormAutoSubmit, type AuthFormContextType, type AuthFormReturn, type AuthFormState, type AuthFormStateHandlers, type AuthFormSubmitHandlers, type AuthFormValidation, type AuthHelpProps, type AuthLayoutConfig, type AuthLayoutProps, AuthProvider, type AuthProviderProps, type AuthStep, type PatchedUserProfileUpdateRequest, PatchedUserProfileUpdateRequestSchema, type ProfileCacheOptions, type TwoFactorSetupData, type UseAuthFormOptions, type UseAuthFormStateReturn, type UseAutoAuthOptions, type UseGithubAuthOptions, type UseGithubAuthReturn, type UseTwoFactorOptions, type UseTwoFactorReturn, type UseTwoFactorSetupOptions, type UseTwoFactorSetupReturn, type UserProfile, authLogger, clearProfileCache, decodeBase64, detectChannelFromIdentifier, encodeBase64, formatAuthError, getCacheMetadata, getCachedProfile, hasValidCache, logger, setCachedProfile, useAccountsContext, useAuth, useAuthForm, useAuthFormState, useAuthGuard, useAuthRedirectManager, useAuthValidation, useAutoAuth, useBase64, useGithubAuth, useLocalStorage, useSessionStorage, useTwoFactor, useTwoFactorSetup, validateEmail, validateIdentifier };
|