import { useState, useEffect } from 'react'; import { useSelector } from 'react-redux'; import useRegister from 'hooks/useRegister'; import Turnstile from 'react-turnstile'; import { useSearchParams } from 'react-router-dom'; // material-ui import { useTheme } from '@mui/material/styles'; import { Box, Button, CircularProgress, FormControl, FormHelperText, Grid, IconButton, InputAdornment, InputLabel, OutlinedInput, Typography } from '@mui/material'; // third party import * as Yup from 'yup'; import { Formik } from 'formik'; // project imports import AnimateButton from 'ui-component/extended/AnimateButton'; import { strengthColor, strengthIndicator } from 'utils/password-strength'; // assets import Visibility from '@mui/icons-material/Visibility'; import VisibilityOff from '@mui/icons-material/VisibilityOff'; import { showError, showInfo } from 'utils/common'; // ===========================|| FIREBASE - REGISTER ||=========================== // const RegisterForm = ({ ...others }) => { const theme = useTheme(); const { register, sendVerificationCode } = useRegister(); const siteInfo = useSelector((state) => state.siteInfo); const [showPassword, setShowPassword] = useState(false); const [searchParams] = useSearchParams(); const [showEmailVerification, setShowEmailVerification] = useState(false); const [turnstileEnabled, setTurnstileEnabled] = useState(false); const [turnstileSiteKey, setTurnstileSiteKey] = useState(''); const [turnstileToken, setTurnstileToken] = useState(''); const [strength, setStrength] = useState(0); const [level, setLevel] = useState(); const [timer, setTimer] = useState(0); const [loading, setLoading] = useState(false); const handleClickShowPassword = () => { setShowPassword(!showPassword); }; const handleMouseDownPassword = (event) => { event.preventDefault(); }; const changePassword = (value) => { const temp = strengthIndicator(value); setStrength(temp); setLevel(strengthColor(temp)); }; const handleSendCode = async (email) => { if (email === '') { showError('请输入邮箱'); return; } if (turnstileEnabled && turnstileToken === '') { showError('请稍后几秒重试,Turnstile 正在检查用户环境!'); return; } setLoading(true); // Start loading const { success, message } = await sendVerificationCode(email, turnstileToken); setLoading(false); // Stop loading if (!success) { showError(message); return; } setTimer(60); // Start the 60-second timer }; useEffect(() => { let affCode = searchParams.get('aff'); if (affCode) { localStorage.setItem('aff', affCode); } setShowEmailVerification(siteInfo.email_verification); if (siteInfo.turnstile_check) { setTurnstileEnabled(true); setTurnstileSiteKey(siteInfo.turnstile_site_key); } }, [siteInfo]); useEffect(() => { let interval; if (timer > 0) { interval = setInterval(() => { setTimer((prevTimer) => prevTimer - 1); }, 1000); } return () => clearInterval(interval); }, [timer]); return ( <> { if (turnstileEnabled && turnstileToken === '') { showInfo('请稍后几秒重试,Turnstile 正在检查用户环境!'); setSubmitting(false); return; } const { success, message } = await register(values, turnstileToken); if (success) { setStatus({ success: true }); } else { setStatus({ success: false }); if (message) { setErrors({ submit: message }); } } }} > {({ errors, handleBlur, handleChange, handleSubmit, isSubmitting, touched, values }) => (
用户名 {touched.username && errors.username && ( {errors.username} )} 密码 { handleChange(e); changePassword(e.target.value); }} endAdornment={ {showPassword ? : } } inputProps={{}} /> {touched.password && errors.password && ( {errors.password} )} 确认密码 {touched.confirmPassword && errors.confirmPassword && ( {errors.confirmPassword} )} {strength !== 0 && ( {level?.label} )} {showEmailVerification && ( <> Email } inputProps={{}} /> {touched.email && errors.email && ( {errors.email} )} 验证码 {touched.verification_code && errors.verification_code && ( {errors.verification_code} )} )} {errors.submit && ( {errors.submit} )} {turnstileEnabled ? ( { setTurnstileToken(token); }} /> ) : ( <> )} )}
); }; export default RegisterForm;