125 lines
3.4 KiB
JavaScript
125 lines
3.4 KiB
JavaScript
import { useRouter, usePathname } from 'expo-router'
|
|
import { useCallback, useMemo, useState } from 'react'
|
|
import { Platform } from 'react-native'
|
|
|
|
import useSocialAuth from './useSocialAuth'
|
|
import { useAuth } from '../providers/auth-context'
|
|
import { useLoading } from '../providers/loading-context'
|
|
import { useTooltip } from '../providers/tooltip-context'
|
|
|
|
const normalizeEmail = (rawEmail) =>
|
|
typeof rawEmail === 'string' ? rawEmail.trim().toLowerCase() : ''
|
|
|
|
const normalizePassword = (rawPassword) =>
|
|
typeof rawPassword === 'string' ? rawPassword.trim() : ''
|
|
|
|
const MIN_PASSWORD_LENGTH = 6
|
|
|
|
const useRegisterForm = () => {
|
|
const router = useRouter()
|
|
const pathname = usePathname()
|
|
const { registerWithEmail, redirectUser } = useAuth()
|
|
const { withLoading, isLoading } = useLoading()
|
|
const { showTooltip } = useTooltip()
|
|
const defaultEmail = __DEV__ ? `test@${Platform.OS}.com` : ''
|
|
const defaultPassword = __DEV__ ? 'Minuit33' : ''
|
|
const [email, setEmail] = useState(defaultEmail)
|
|
const [password, setPassword] = useState(defaultPassword)
|
|
const [confirmPassword, setConfirmPassword] = useState(defaultPassword)
|
|
|
|
const { signInWithGoogle, signInWithApple, isAppleSignInAvailable } = useSocialAuth({
|
|
defaultAction: 'register',
|
|
})
|
|
|
|
const handleSubmit = useCallback(async () => {
|
|
if (isLoading) {
|
|
return
|
|
}
|
|
|
|
const trimmedEmail = normalizeEmail(email)
|
|
const trimmedPassword = normalizePassword(password)
|
|
const trimmedConfirm = normalizePassword(confirmPassword)
|
|
|
|
if (!trimmedEmail || !trimmedPassword || !trimmedConfirm) {
|
|
showTooltip({
|
|
message: 'Merci de renseigner tous les champs requis.',
|
|
type: 'error',
|
|
})
|
|
return
|
|
}
|
|
|
|
if (trimmedPassword.length < MIN_PASSWORD_LENGTH) {
|
|
showTooltip({
|
|
message: `Le mot de passe doit contenir au moins ${MIN_PASSWORD_LENGTH} caractères.`,
|
|
type: 'error',
|
|
})
|
|
return
|
|
}
|
|
|
|
if (trimmedPassword !== trimmedConfirm) {
|
|
showTooltip({
|
|
message: 'Les mots de passe ne correspondent pas.',
|
|
type: 'error',
|
|
})
|
|
return
|
|
}
|
|
|
|
try {
|
|
await withLoading(async () => {
|
|
await registerWithEmail(trimmedEmail, trimmedPassword)
|
|
})
|
|
await redirectUser({ router, pathname, isInitial: false })
|
|
showTooltip({
|
|
message: 'Compte créé ! Bienvenue chez Minuit.',
|
|
type: 'success',
|
|
})
|
|
} catch (error) {
|
|
showTooltip({
|
|
message: error?.message || 'Impossible de créer votre compte pour le moment.',
|
|
type: 'error',
|
|
})
|
|
}
|
|
}, [
|
|
confirmPassword,
|
|
email,
|
|
isLoading,
|
|
password,
|
|
redirectUser,
|
|
registerWithEmail,
|
|
router,
|
|
pathname,
|
|
showTooltip,
|
|
withLoading,
|
|
])
|
|
|
|
const submitDisabled = useMemo(() => {
|
|
const trimmedEmail = normalizeEmail(email)
|
|
const trimmedPassword = normalizePassword(password)
|
|
const trimmedConfirm = normalizePassword(confirmPassword)
|
|
return (
|
|
!trimmedEmail ||
|
|
!trimmedPassword ||
|
|
!trimmedConfirm ||
|
|
trimmedPassword.length < MIN_PASSWORD_LENGTH ||
|
|
isLoading
|
|
)
|
|
}, [confirmPassword, email, isLoading, password])
|
|
|
|
return {
|
|
email,
|
|
setEmail,
|
|
password,
|
|
setPassword,
|
|
confirmPassword,
|
|
setConfirmPassword,
|
|
isLoading,
|
|
submitDisabled,
|
|
handleSubmit,
|
|
signInWithGoogle,
|
|
signInWithApple,
|
|
isAppleSignInAvailable,
|
|
}
|
|
}
|
|
|
|
export default useRegisterForm
|