@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.
Files changed (75) hide show
  1. package/README.md +125 -9
  2. package/dist/auth.cjs +1865 -402
  3. package/dist/auth.cjs.map +1 -1
  4. package/dist/auth.d.cts +352 -76
  5. package/dist/auth.d.ts +352 -76
  6. package/dist/auth.mjs +1867 -404
  7. package/dist/auth.mjs.map +1 -1
  8. package/dist/clients.cjs +1637 -137
  9. package/dist/clients.cjs.map +1 -1
  10. package/dist/clients.d.cts +1394 -282
  11. package/dist/clients.d.ts +1394 -282
  12. package/dist/clients.mjs +1637 -137
  13. package/dist/clients.mjs.map +1 -1
  14. package/dist/hooks.cjs +24 -11
  15. package/dist/hooks.cjs.map +1 -1
  16. package/dist/hooks.d.cts +88 -21
  17. package/dist/hooks.d.ts +88 -21
  18. package/dist/hooks.mjs +24 -11
  19. package/dist/hooks.mjs.map +1 -1
  20. package/dist/index.cjs +38 -17
  21. package/dist/index.cjs.map +1 -1
  22. package/dist/index.d.cts +94 -21
  23. package/dist/index.d.ts +94 -21
  24. package/dist/index.mjs +38 -17
  25. package/dist/index.mjs.map +1 -1
  26. package/package.json +3 -3
  27. package/src/auth/context/AccountsContext.tsx +8 -1
  28. package/src/auth/context/AuthContext.tsx +31 -8
  29. package/src/auth/context/types.ts +8 -1
  30. package/src/auth/hooks/index.ts +29 -5
  31. package/src/auth/hooks/useAuthForm.ts +292 -226
  32. package/src/auth/hooks/useAuthFormState.ts +60 -0
  33. package/src/auth/hooks/useAuthValidation.ts +77 -0
  34. package/src/auth/hooks/useGithubAuth.ts +26 -5
  35. package/src/auth/hooks/useTwoFactor.ts +239 -0
  36. package/src/auth/hooks/useTwoFactorSetup.ts +213 -0
  37. package/src/auth/index.ts +3 -0
  38. package/src/auth/types/form.ts +194 -0
  39. package/src/auth/types/index.ts +28 -0
  40. package/src/clients.ts +10 -0
  41. package/src/generated/cfg_accounts/_utils/schemas/OAuthTokenResponse.schema.ts +26 -3
  42. package/src/generated/cfg_accounts/_utils/schemas/OTPVerifyResponse.schema.ts +26 -3
  43. package/src/generated/cfg_accounts/accounts/client.ts +4 -1
  44. package/src/generated/cfg_accounts/accounts/models.ts +15 -6
  45. package/src/generated/cfg_accounts/accounts__oauth/models.ts +16 -7
  46. package/src/generated/cfg_accounts/client.ts +5 -2
  47. package/src/generated/cfg_accounts/http.ts +8 -2
  48. package/src/generated/cfg_accounts/schema.json +47 -19
  49. package/src/generated/cfg_centrifugo/client.ts +5 -2
  50. package/src/generated/cfg_centrifugo/http.ts +8 -2
  51. package/src/generated/cfg_totp/CLAUDE.md +12 -12
  52. package/src/generated/cfg_totp/_utils/fetchers/index.ts +3 -3
  53. package/src/generated/cfg_totp/_utils/fetchers/{totp__2fa_management.ts → totp__totp_management.ts} +3 -3
  54. package/src/generated/cfg_totp/_utils/fetchers/{totp__2fa_setup.ts → totp__totp_setup.ts} +3 -3
  55. package/src/generated/cfg_totp/_utils/fetchers/{totp__2fa_verification.ts → totp__totp_verification.ts} +3 -3
  56. package/src/generated/cfg_totp/_utils/hooks/index.ts +3 -3
  57. package/src/generated/cfg_totp/_utils/hooks/{totp__2fa_management.ts → totp__totp_management.ts} +2 -2
  58. package/src/generated/cfg_totp/_utils/hooks/{totp__2fa_setup.ts → totp__totp_setup.ts} +2 -2
  59. package/src/generated/cfg_totp/_utils/hooks/{totp__2fa_verification.ts → totp__totp_verification.ts} +2 -2
  60. package/src/generated/cfg_totp/_utils/schemas/DeviceList.schema.ts +1 -1
  61. package/src/generated/cfg_totp/client.ts +14 -11
  62. package/src/generated/cfg_totp/http.ts +8 -2
  63. package/src/generated/cfg_totp/index.ts +16 -16
  64. package/src/generated/cfg_totp/schema.json +8 -7
  65. package/src/generated/cfg_totp/{totp__2fa_management → totp__totp_management}/client.ts +2 -2
  66. package/src/generated/cfg_totp/{totp__2fa_management → totp__totp_management}/models.ts +1 -1
  67. package/src/generated/cfg_totp/{totp__2fa_setup → totp__totp_setup}/client.ts +4 -4
  68. package/src/generated/cfg_totp/{totp__2fa_verification → totp__totp_verification}/client.ts +2 -2
  69. package/src/generated/cfg_webpush/client.ts +5 -2
  70. package/src/generated/cfg_webpush/http.ts +8 -2
  71. /package/src/generated/cfg_totp/{totp__2fa_management → totp__totp_management}/index.ts +0 -0
  72. /package/src/generated/cfg_totp/{totp__2fa_setup → totp__totp_setup}/index.ts +0 -0
  73. /package/src/generated/cfg_totp/{totp__2fa_setup → totp__totp_setup}/models.ts +0 -0
  74. /package/src/generated/cfg_totp/{totp__2fa_verification → totp__totp_verification}/index.ts +0 -0
  75. /package/src/generated/cfg_totp/{totp__2fa_verification → totp__totp_verification}/models.ts +0 -0
package/README.md CHANGED
@@ -18,6 +18,7 @@ This package provides everything needed for authentication and user management:
18
18
  - **Auth System** - Complete authentication module with contexts, hooks, and utilities
19
19
  - **JWT Management** - Automatic token refresh and storage
20
20
  - **OAuth Integration** - GitHub OAuth with callback handling
21
+ - **Two-Factor Authentication** - TOTP-based 2FA setup and verification
21
22
  - **Server Middleware** - Proxy middleware for Next.js
22
23
  - **Shared Storage** - Authentication storage used by all extensions
23
24
 
@@ -26,13 +27,17 @@ This package provides everything needed for authentication and user management:
26
27
  ```
27
28
  src/
28
29
  ├── generated/
29
- └── cfg_accounts/ # Generated API client
30
- ├── api/ # API class with all endpoints
31
- ├── schemas/ # Zod validation schemas
32
- └── types/ # TypeScript types
30
+ ├── cfg_accounts/ # Generated API client (accounts, profiles, OAuth)
31
+ ├── api/ # API class with all endpoints
32
+ ├── schemas/ # Zod validation schemas
33
+ └── types/ # TypeScript types
34
+ │ └── cfg_totp/ # Generated TOTP/2FA client
35
+ │ ├── api/ # TOTP device, setup, verification endpoints
36
+ │ ├── schemas/ # TOTP-specific schemas
37
+ │ └── types/ # TOTP types
33
38
  └── auth/
34
39
  ├── context/ # AuthProvider, AccountsProvider
35
- ├── hooks/ # useAuth, useAuthGuard, useGithubAuth, etc.
40
+ ├── hooks/ # useAuth, useAuthGuard, useGithubAuth, useTwoFactor, etc.
36
41
  ├── middlewares/ # Next.js proxy middleware
37
42
  └── utils/ # Validation, errors, logger, analytics
38
43
  ```
@@ -88,7 +93,20 @@ This is handled automatically by `createExtensionAPI()` from `@djangocfg/ext-bas
88
93
 
89
94
  ## Auth Module
90
95
 
91
- Complete OTP-based authentication system with React contexts, hooks, and utilities.
96
+ Complete authentication system with OTP, OAuth, and Two-Factor Authentication (2FA) support.
97
+
98
+ ### Authentication Flow
99
+
100
+ ```
101
+ 1. User enters email/phone → OTP sent
102
+ 2. User enters OTP code → Verify
103
+ ├── If 2FA enabled → Show TOTP verification
104
+ │ └── User enters 6-digit TOTP → Verify
105
+ └── If no 2FA → Continue
106
+ 3. Success screen with logo → Auto-redirect to dashboard
107
+ ```
108
+
109
+ All authentication methods (OTP, OAuth, 2FA) show a success screen with your logo before redirecting.
92
110
 
93
111
  ### Setup
94
112
 
@@ -122,8 +140,10 @@ export default function RootLayout({ children }) {
122
140
  import {
123
141
  useAuth, // Main auth context (user, isAuthenticated, OTP methods)
124
142
  useAuthGuard, // Protect routes (redirect if not authenticated)
125
- useAuthForm, // OTP form state management (identifier, otp, steps)
143
+ useAuthForm, // Auth form state management (OTP + 2FA steps)
126
144
  useGithubAuth, // GitHub OAuth integration
145
+ useTwoFactor, // 2FA verification (verify TOTP code)
146
+ useTwoFactorSetup, // 2FA setup flow (generate QR, verify, enable)
127
147
  useAutoAuth, // Auto-authentication on mount
128
148
  useLocalStorage, // localStorage helper
129
149
  useSessionStorage, // sessionStorage helper
@@ -185,7 +205,7 @@ export default function DashboardPage() {
185
205
 
186
206
  ### useAuthForm
187
207
 
188
- Manage OTP authentication form state (two-step flow):
208
+ Manage authentication form state with OTP and 2FA support:
189
209
 
190
210
  ```tsx
191
211
  'use client';
@@ -197,7 +217,7 @@ export function OTPLoginForm() {
197
217
  identifier, // Email or phone number
198
218
  channel, // 'email' | 'phone'
199
219
  otp, // 6-digit OTP code
200
- step, // 'identifier' | 'otp'
220
+ step, // 'identifier' | 'otp' | '2fa' | '2fa-setup' | 'success'
201
221
  isLoading,
202
222
  error,
203
223
  acceptedTerms,
@@ -304,6 +324,98 @@ export function GithubLoginButton() {
304
324
 
305
325
  OAuth callback is handled automatically by `@djangocfg/layouts` AuthLayout component.
306
326
 
327
+ ### useTwoFactor
328
+
329
+ Verify 2FA code during authentication:
330
+
331
+ ```tsx
332
+ 'use client';
333
+ import { useTwoFactor } from '@djangocfg/api/auth';
334
+
335
+ export function TwoFactorVerify({ sessionId }: { sessionId: string }) {
336
+ const {
337
+ verify, // (code: string) => Promise<void>
338
+ isLoading, // boolean
339
+ error, // string | null
340
+ } = useTwoFactor({
341
+ sessionId,
342
+ onSuccess: (user) => console.log('2FA verified:', user),
343
+ onError: (error) => console.error('2FA failed:', error),
344
+ redirectUrl: '/dashboard',
345
+ skipRedirect: false, // Set true if handling navigation manually
346
+ });
347
+
348
+ const [code, setCode] = useState('');
349
+
350
+ return (
351
+ <form onSubmit={(e) => { e.preventDefault(); verify(code); }}>
352
+ <input
353
+ type="text"
354
+ value={code}
355
+ onChange={(e) => setCode(e.target.value)}
356
+ placeholder="000000"
357
+ maxLength={6}
358
+ />
359
+ <button type="submit" disabled={isLoading || code.length < 6}>
360
+ {isLoading ? 'Verifying...' : 'Verify'}
361
+ </button>
362
+ </form>
363
+ );
364
+ }
365
+ ```
366
+
367
+ ### useTwoFactorSetup
368
+
369
+ Setup 2FA for authenticated users:
370
+
371
+ ```tsx
372
+ 'use client';
373
+ import { useTwoFactorSetup } from '@djangocfg/api/auth';
374
+
375
+ export function TwoFactorSetup() {
376
+ const {
377
+ // State
378
+ qrCode, // Base64 QR code image
379
+ secret, // Manual entry secret
380
+ isEnabled, // Current 2FA status
381
+ isLoading,
382
+ error,
383
+
384
+ // Actions
385
+ startSetup, // () => Promise<void> - generates QR code
386
+ verifySetup, // (code: string) => Promise<void> - enables 2FA
387
+ disable, // () => Promise<void> - disables 2FA
388
+ checkStatus, // () => Promise<boolean> - refresh status
389
+ } = useTwoFactorSetup({
390
+ onSetupComplete: () => console.log('2FA enabled successfully'),
391
+ onDisabled: () => console.log('2FA disabled'),
392
+ onError: (error) => console.error('2FA setup error:', error),
393
+ });
394
+
395
+ if (isEnabled) {
396
+ return (
397
+ <div>
398
+ <p>Two-factor authentication is enabled</p>
399
+ <button onClick={disable}>Disable 2FA</button>
400
+ </div>
401
+ );
402
+ }
403
+
404
+ if (qrCode) {
405
+ return (
406
+ <div>
407
+ <img src={qrCode} alt="2FA QR Code" />
408
+ <p>Secret: {secret}</p>
409
+ <input placeholder="Enter code from app" />
410
+ <button onClick={() => verifySetup('123456')}>Verify & Enable</button>
411
+ </div>
412
+ );
413
+ }
414
+
415
+ return <button onClick={startSetup}>Set up 2FA</button>;
416
+ }
417
+ ```
418
+
307
419
  ## Server Components & API Routes
308
420
 
309
421
  Use fetchers for server-side data fetching:
@@ -471,6 +583,10 @@ Auth events are automatically tracked when analytics is enabled:
471
583
  // - AUTH_OAUTH_START
472
584
  // - AUTH_OAUTH_SUCCESS
473
585
  // - AUTH_OAUTH_FAIL
586
+ // - AUTH_2FA_SETUP_START
587
+ // - AUTH_2FA_SETUP_COMPLETE
588
+ // - AUTH_2FA_VERIFY_SUCCESS
589
+ // - AUTH_2FA_VERIFY_FAIL
474
590
  ```
475
591
 
476
592
  Analytics setup is handled by `@djangocfg/layouts` package.