@massimo.mazzoleni/cognito-max 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.
- package/README.md +2410 -0
- package/dist/chunk-AD7T42HJ.js +3 -0
- package/dist/chunk-AD7T42HJ.js.map +1 -0
- package/dist/chunk-DKPFVGTY.js +683 -0
- package/dist/chunk-DKPFVGTY.js.map +1 -0
- package/dist/chunk-N4OQLBV6.js +135 -0
- package/dist/chunk-N4OQLBV6.js.map +1 -0
- package/dist/client-63FraVdm.d.ts +69 -0
- package/dist/client-BAoL8h4E.d.cts +69 -0
- package/dist/core/index.cjs +696 -0
- package/dist/core/index.cjs.map +1 -0
- package/dist/core/index.d.cts +3 -0
- package/dist/core/index.d.ts +3 -0
- package/dist/core/index.js +4 -0
- package/dist/core/index.js.map +1 -0
- package/dist/errors-BkUDHleb.d.cts +22 -0
- package/dist/errors-BkUDHleb.d.ts +22 -0
- package/dist/index.cjs +696 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +3 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/react/index.cjs +844 -0
- package/dist/react/index.cjs.map +1 -0
- package/dist/react/index.d.cts +104 -0
- package/dist/react/index.d.ts +104 -0
- package/dist/react/index.js +64 -0
- package/dist/react/index.js.map +1 -0
- package/dist/types-bxA1vonL.d.cts +113 -0
- package/dist/types-bxA1vonL.d.ts +113 -0
- package/dist/ui/index.cjs +1183 -0
- package/dist/ui/index.cjs.map +1 -0
- package/dist/ui/index.d.cts +241 -0
- package/dist/ui/index.d.ts +241 -0
- package/dist/ui/index.js +1109 -0
- package/dist/ui/index.js.map +1 -0
- package/package.json +81 -0
- package/src/core/client.ts +604 -0
- package/src/core/errors.ts +91 -0
- package/src/core/event-bus.ts +41 -0
- package/src/core/index.ts +5 -0
- package/src/core/internal/converters.ts +32 -0
- package/src/core/storage.ts +79 -0
- package/src/core/types.ts +87 -0
- package/src/index.ts +1 -0
- package/src/react/components/ProtectedRoute.tsx +56 -0
- package/src/react/context.tsx +126 -0
- package/src/react/hooks/useAuth.ts +75 -0
- package/src/react/hooks/useMfa.ts +19 -0
- package/src/react/hooks/useSession.ts +16 -0
- package/src/react/hooks/useUser.ts +24 -0
- package/src/react/index.ts +10 -0
- package/src/ui/components/ChangePasswordForm.tsx +105 -0
- package/src/ui/components/ForgotPasswordForm.tsx +159 -0
- package/src/ui/components/MfaSetupWizard.tsx +136 -0
- package/src/ui/components/RegisterForm.tsx +159 -0
- package/src/ui/components/SignInForm.tsx +296 -0
- package/src/ui/hooks/useChangePasswordForm.ts +81 -0
- package/src/ui/hooks/useForgotPasswordForm.ts +109 -0
- package/src/ui/hooks/useMfaSetup.ts +93 -0
- package/src/ui/hooks/useRegisterForm.ts +120 -0
- package/src/ui/hooks/useSignInForm.ts +245 -0
- package/src/ui/index.ts +31 -0
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import { useCallback, useState, type FormEvent } from 'react'
|
|
2
|
+
import { useAuth } from '../../react/hooks/useAuth'
|
|
3
|
+
import type { CognitoAuthError } from '../../core/errors'
|
|
4
|
+
import type { MfaType, SignInResult } from '../../core/types'
|
|
5
|
+
|
|
6
|
+
export type SignInStep = 'credentials' | 'mfa' | 'new_password' | 'mfa_setup'
|
|
7
|
+
|
|
8
|
+
export interface UseSignInFormOptions {
|
|
9
|
+
onSuccess?: (result: Extract<SignInResult, { status: 'SUCCESS' }>) => void
|
|
10
|
+
onError?: (error: CognitoAuthError) => void
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface UseSignInFormReturn {
|
|
14
|
+
step: SignInStep
|
|
15
|
+
isLoading: boolean
|
|
16
|
+
error: CognitoAuthError | null
|
|
17
|
+
mfaType: MfaType | null
|
|
18
|
+
|
|
19
|
+
credentials: {
|
|
20
|
+
email: string
|
|
21
|
+
password: string
|
|
22
|
+
setEmail(v: string): void
|
|
23
|
+
setPassword(v: string): void
|
|
24
|
+
onSubmit(e: FormEvent): void
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
mfa: {
|
|
28
|
+
code: string
|
|
29
|
+
setCode(v: string): void
|
|
30
|
+
onSubmit(e: FormEvent): void
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
newPassword: {
|
|
34
|
+
password: string
|
|
35
|
+
confirmPassword: string
|
|
36
|
+
requiredAttributes: string[]
|
|
37
|
+
setPassword(v: string): void
|
|
38
|
+
setConfirmPassword(v: string): void
|
|
39
|
+
onSubmit(e: FormEvent): void
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
mfaSetup: {
|
|
43
|
+
qrCodeUri: string | null
|
|
44
|
+
secretCode: string | null
|
|
45
|
+
totpCode: string
|
|
46
|
+
setTotpCode(v: string): void
|
|
47
|
+
/** Chiama setupTotpChallenge e popola qrCodeUri / secretCode */
|
|
48
|
+
start(): Promise<void>
|
|
49
|
+
/** Chiama verifyTotpChallenge poi handleSignInResult */
|
|
50
|
+
onVerify(e: FormEvent): void
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
reset(): void
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function useSignInForm(options: UseSignInFormOptions = {}): UseSignInFormReturn {
|
|
57
|
+
const {
|
|
58
|
+
signIn,
|
|
59
|
+
respondToMfaChallenge,
|
|
60
|
+
respondToNewPasswordChallenge,
|
|
61
|
+
setupTotpChallenge,
|
|
62
|
+
verifyTotpChallenge,
|
|
63
|
+
} = useAuth()
|
|
64
|
+
|
|
65
|
+
const [step, setStep] = useState<SignInStep>('credentials')
|
|
66
|
+
const [isLoading, setLoading] = useState(false)
|
|
67
|
+
const [error, setError] = useState<CognitoAuthError | null>(null)
|
|
68
|
+
|
|
69
|
+
const [email, setEmail] = useState('')
|
|
70
|
+
const [password, setPassword] = useState('')
|
|
71
|
+
|
|
72
|
+
const [mfaType, setMfaType] = useState<MfaType | null>(null)
|
|
73
|
+
const [challengeSession, setChallengeSession] = useState<string | null>(null)
|
|
74
|
+
const [mfaCode, setMfaCode] = useState('')
|
|
75
|
+
|
|
76
|
+
const [newPwd, setNewPwd] = useState('')
|
|
77
|
+
const [confirmPwd, setConfirmPwd] = useState('')
|
|
78
|
+
const [requiredAttributes, setRequiredAttributes] = useState<string[]>([])
|
|
79
|
+
|
|
80
|
+
// MFA setup during login (MFA_SETUP_REQUIRED challenge)
|
|
81
|
+
const [mfaSetupQrCodeUri, setMfaSetupQrCodeUri] = useState<string | null>(null)
|
|
82
|
+
const [mfaSetupSecretCode, setMfaSetupSecretCode] = useState<string | null>(null)
|
|
83
|
+
const [mfaSetupTotpCode, setMfaSetupTotpCode] = useState('')
|
|
84
|
+
|
|
85
|
+
const handleSignInResult = useCallback(
|
|
86
|
+
(result: SignInResult) => {
|
|
87
|
+
if (result.status === 'SUCCESS') {
|
|
88
|
+
options.onSuccess?.(result)
|
|
89
|
+
} else if (result.status === 'MFA_REQUIRED') {
|
|
90
|
+
setMfaType(result.mfaType)
|
|
91
|
+
setChallengeSession(result.challengeSession)
|
|
92
|
+
setStep('mfa')
|
|
93
|
+
} else if (result.status === 'NEW_PASSWORD_REQUIRED') {
|
|
94
|
+
setChallengeSession(result.challengeSession)
|
|
95
|
+
setRequiredAttributes(result.requiredAttributes)
|
|
96
|
+
setStep('new_password')
|
|
97
|
+
} else if (result.status === 'MFA_SETUP_REQUIRED') {
|
|
98
|
+
setChallengeSession(result.challengeSession)
|
|
99
|
+
setStep('mfa_setup')
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
[options],
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
const handleCredentialsSubmit = useCallback(
|
|
106
|
+
async (e: FormEvent) => {
|
|
107
|
+
e.preventDefault()
|
|
108
|
+
setError(null)
|
|
109
|
+
setLoading(true)
|
|
110
|
+
try {
|
|
111
|
+
const result = await signIn(email, password)
|
|
112
|
+
handleSignInResult(result)
|
|
113
|
+
} catch (err) {
|
|
114
|
+
const authErr = err as CognitoAuthError
|
|
115
|
+
setError(authErr)
|
|
116
|
+
options.onError?.(authErr)
|
|
117
|
+
} finally {
|
|
118
|
+
setLoading(false)
|
|
119
|
+
}
|
|
120
|
+
},
|
|
121
|
+
[email, password, signIn, handleSignInResult, options],
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
const handleMfaSubmit = useCallback(
|
|
125
|
+
async (e: FormEvent) => {
|
|
126
|
+
e.preventDefault()
|
|
127
|
+
if (!challengeSession || !mfaType) return
|
|
128
|
+
setError(null)
|
|
129
|
+
setLoading(true)
|
|
130
|
+
try {
|
|
131
|
+
const result = await respondToMfaChallenge(challengeSession, mfaCode, mfaType)
|
|
132
|
+
handleSignInResult(result)
|
|
133
|
+
} catch (err) {
|
|
134
|
+
const authErr = err as CognitoAuthError
|
|
135
|
+
setError(authErr)
|
|
136
|
+
options.onError?.(authErr)
|
|
137
|
+
} finally {
|
|
138
|
+
setLoading(false)
|
|
139
|
+
}
|
|
140
|
+
},
|
|
141
|
+
[challengeSession, mfaType, mfaCode, respondToMfaChallenge, handleSignInResult, options],
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
const handleNewPasswordSubmit = useCallback(
|
|
145
|
+
async (e: FormEvent) => {
|
|
146
|
+
e.preventDefault()
|
|
147
|
+
if (!challengeSession) return
|
|
148
|
+
if (newPwd !== confirmPwd) {
|
|
149
|
+
setError({ message: 'Le password non coincidono', code: 'INVALID_PASSWORD' } as CognitoAuthError)
|
|
150
|
+
return
|
|
151
|
+
}
|
|
152
|
+
setError(null)
|
|
153
|
+
setLoading(true)
|
|
154
|
+
try {
|
|
155
|
+
const result = await respondToNewPasswordChallenge(challengeSession, newPwd)
|
|
156
|
+
handleSignInResult(result)
|
|
157
|
+
} catch (err) {
|
|
158
|
+
const authErr = err as CognitoAuthError
|
|
159
|
+
setError(authErr)
|
|
160
|
+
options.onError?.(authErr)
|
|
161
|
+
} finally {
|
|
162
|
+
setLoading(false)
|
|
163
|
+
}
|
|
164
|
+
},
|
|
165
|
+
[challengeSession, newPwd, confirmPwd, respondToNewPasswordChallenge, handleSignInResult, options],
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
const handleMfaSetupStart = useCallback(async () => {
|
|
169
|
+
if (!challengeSession) return
|
|
170
|
+
setError(null)
|
|
171
|
+
setLoading(true)
|
|
172
|
+
try {
|
|
173
|
+
const result = await setupTotpChallenge(challengeSession)
|
|
174
|
+
setMfaSetupQrCodeUri(result.qrCodeUri)
|
|
175
|
+
setMfaSetupSecretCode(result.secretCode)
|
|
176
|
+
} catch (err) {
|
|
177
|
+
const authErr = err as CognitoAuthError
|
|
178
|
+
setError(authErr)
|
|
179
|
+
options.onError?.(authErr)
|
|
180
|
+
} finally {
|
|
181
|
+
setLoading(false)
|
|
182
|
+
}
|
|
183
|
+
}, [challengeSession, setupTotpChallenge, options])
|
|
184
|
+
|
|
185
|
+
const handleMfaSetupVerify = useCallback(
|
|
186
|
+
async (e: FormEvent) => {
|
|
187
|
+
e.preventDefault()
|
|
188
|
+
if (!challengeSession) return
|
|
189
|
+
setError(null)
|
|
190
|
+
setLoading(true)
|
|
191
|
+
try {
|
|
192
|
+
const result = await verifyTotpChallenge(challengeSession, mfaSetupTotpCode)
|
|
193
|
+
handleSignInResult(result)
|
|
194
|
+
} catch (err) {
|
|
195
|
+
const authErr = err as CognitoAuthError
|
|
196
|
+
setError(authErr)
|
|
197
|
+
options.onError?.(authErr)
|
|
198
|
+
} finally {
|
|
199
|
+
setLoading(false)
|
|
200
|
+
}
|
|
201
|
+
},
|
|
202
|
+
[challengeSession, mfaSetupTotpCode, verifyTotpChallenge, handleSignInResult, options],
|
|
203
|
+
)
|
|
204
|
+
|
|
205
|
+
const reset = useCallback(() => {
|
|
206
|
+
setStep('credentials')
|
|
207
|
+
setEmail('')
|
|
208
|
+
setPassword('')
|
|
209
|
+
setMfaCode('')
|
|
210
|
+
setNewPwd('')
|
|
211
|
+
setConfirmPwd('')
|
|
212
|
+
setError(null)
|
|
213
|
+
setMfaType(null)
|
|
214
|
+
setChallengeSession(null)
|
|
215
|
+
setMfaSetupQrCodeUri(null)
|
|
216
|
+
setMfaSetupSecretCode(null)
|
|
217
|
+
setMfaSetupTotpCode('')
|
|
218
|
+
}, [])
|
|
219
|
+
|
|
220
|
+
return {
|
|
221
|
+
step,
|
|
222
|
+
isLoading,
|
|
223
|
+
error,
|
|
224
|
+
mfaType,
|
|
225
|
+
credentials: { email, password, setEmail, setPassword, onSubmit: handleCredentialsSubmit },
|
|
226
|
+
mfa: { code: mfaCode, setCode: setMfaCode, onSubmit: handleMfaSubmit },
|
|
227
|
+
newPassword: {
|
|
228
|
+
password: newPwd,
|
|
229
|
+
confirmPassword: confirmPwd,
|
|
230
|
+
requiredAttributes,
|
|
231
|
+
setPassword: setNewPwd,
|
|
232
|
+
setConfirmPassword: setConfirmPwd,
|
|
233
|
+
onSubmit: handleNewPasswordSubmit,
|
|
234
|
+
},
|
|
235
|
+
mfaSetup: {
|
|
236
|
+
qrCodeUri: mfaSetupQrCodeUri,
|
|
237
|
+
secretCode: mfaSetupSecretCode,
|
|
238
|
+
totpCode: mfaSetupTotpCode,
|
|
239
|
+
setTotpCode: setMfaSetupTotpCode,
|
|
240
|
+
start: handleMfaSetupStart,
|
|
241
|
+
onVerify: handleMfaSetupVerify,
|
|
242
|
+
},
|
|
243
|
+
reset,
|
|
244
|
+
}
|
|
245
|
+
}
|
package/src/ui/index.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// Hooks headless (usa la logica senza dipendere dall'UI)
|
|
2
|
+
export { useSignInForm } from './hooks/useSignInForm'
|
|
3
|
+
export type { UseSignInFormOptions, UseSignInFormReturn, SignInStep } from './hooks/useSignInForm'
|
|
4
|
+
|
|
5
|
+
export { useRegisterForm } from './hooks/useRegisterForm'
|
|
6
|
+
export type { UseRegisterFormOptions, UseRegisterFormReturn, RegisterStep } from './hooks/useRegisterForm'
|
|
7
|
+
|
|
8
|
+
export { useForgotPasswordForm } from './hooks/useForgotPasswordForm'
|
|
9
|
+
export type { UseForgotPasswordFormOptions, UseForgotPasswordFormReturn, ForgotPasswordStep } from './hooks/useForgotPasswordForm'
|
|
10
|
+
|
|
11
|
+
export { useChangePasswordForm } from './hooks/useChangePasswordForm'
|
|
12
|
+
export type { UseChangePasswordFormOptions, UseChangePasswordFormReturn } from './hooks/useChangePasswordForm'
|
|
13
|
+
|
|
14
|
+
export { useMfaSetup } from './hooks/useMfaSetup'
|
|
15
|
+
export type { UseMfaSetupOptions, UseMfaSetupReturn, MfaSetupStep } from './hooks/useMfaSetup'
|
|
16
|
+
|
|
17
|
+
// Componenti default (HTML puro, nessuna dipendenza da design system)
|
|
18
|
+
export { SignInForm } from './components/SignInForm'
|
|
19
|
+
export type { SignInFormProps } from './components/SignInForm'
|
|
20
|
+
|
|
21
|
+
export { RegisterForm } from './components/RegisterForm'
|
|
22
|
+
export type { RegisterFormProps } from './components/RegisterForm'
|
|
23
|
+
|
|
24
|
+
export { ForgotPasswordForm } from './components/ForgotPasswordForm'
|
|
25
|
+
export type { ForgotPasswordFormProps } from './components/ForgotPasswordForm'
|
|
26
|
+
|
|
27
|
+
export { ChangePasswordForm } from './components/ChangePasswordForm'
|
|
28
|
+
export type { ChangePasswordFormProps } from './components/ChangePasswordForm'
|
|
29
|
+
|
|
30
|
+
export { MfaSetupWizard } from './components/MfaSetupWizard'
|
|
31
|
+
export type { MfaSetupWizardProps } from './components/MfaSetupWizard'
|