Spaces:
Sleeping
Sleeping
File size: 1,736 Bytes
cd99321 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | import { useState } from 'react'
import { useNavigate } from 'react-router-dom'
import { useAuthStore } from '../../store/authStore'
import { useTranslation } from '../../i18n'
/**
* Register data hook — owns the form state, the client-side validation and the
* register → redirect flow. RegisterPage is a pure wiring container. Behaviour
* is identical to the previous in-component logic.
*/
export function useRegister() {
const { t } = useTranslation()
const { register } = useAuthStore()
const navigate = useNavigate()
const [username, setUsername] = useState<string>('')
const [email, setEmail] = useState<string>('')
const [password, setPassword] = useState<string>('')
const [confirmPassword, setConfirmPassword] = useState<string>('')
const [showPassword, setShowPassword] = useState<boolean>(false)
const [isLoading, setIsLoading] = useState<boolean>(false)
const [error, setError] = useState<string>('')
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>): Promise<void> => {
e.preventDefault()
setError('')
if (password !== confirmPassword) {
setError(t('register.passwordMismatch'))
return
}
if (password.length < 8) {
setError(t('register.passwordTooShort'))
return
}
setIsLoading(true)
try {
await register(username, email, password)
navigate('/dashboard')
} catch (err: unknown) {
setError(err instanceof Error ? err.message : t('register.failed'))
} finally {
setIsLoading(false)
}
}
return {
username, setUsername, email, setEmail, password, setPassword,
confirmPassword, setConfirmPassword, showPassword, setShowPassword,
isLoading, error, handleSubmit,
}
}
|