@djangocfg/api 2.1.57 → 2.1.59
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/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
|
-
│
|
|
30
|
-
│
|
|
31
|
-
│
|
|
32
|
-
│
|
|
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
|
|
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, //
|
|
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
|
|
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.
|