import {useState, useMemo, useEffect} from 'react'; import {useRouter} from 'next/router'; import RouterLink from 'next/link'; import {makeStyles} from '@material-ui/core/styles'; import TextField from '@material-ui/core/TextField'; import Button from '@material-ui/core/Button'; import Link from '@material-ui/core/Link'; import Typography from '@material-ui/core/Typography'; import CardContent from '@material-ui/core/CardContent'; import CircularProgress from '@material-ui/core/CircularProgress'; import FormHelperText from '@material-ui/core/FormHelperText'; import CardActions from '@material-ui/core/CardActions'; import {useTranslation} from 'react-i18next'; import useToastsStore from '../../stores/useToastStore'; import useLoginWithProvider from '../../hooks/useLoginWithProvider'; import useLoginForm from '../../hooks/useLoginForm'; import useAddToEvents from '../../hooks/useAddToEvents'; const SignIn = () => { const {t} = useTranslation(); const router = useRouter(); const {loginWithProvider} = useLoginWithProvider(); const [error, setError] = useState(''); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const addToast = useToastsStore(s => s.addToast); const {login, loading} = useLoginForm(email, password); const {saveStoredEvents} = useAddToEvents(); const classes = useStyles(); const canSubmit = useMemo( () => [email, password].filter(s => s.length < 4).length === 0, [email, password] ); const onSubmit = async e => { e.preventDefault?.(); try { await login(); saveStoredEvents(); router.push('/'); } catch (error) { handleAuthError(error); } return false; }; useEffect(() => { const authWithGoogle = async search => { try { await loginWithProvider('google', search); } catch (error) { handleAuthError(error); } }; const search = getURLSearch(router); if (search) authWithGoogle(search); }, [router.route]); // eslint-disable-line react-hooks/exhaustive-deps const handleAuthError = error => { const strapiError = error.message; console.error({strapiError}); if (strapiError === 'Invalid identifier or password') { setError(t('signin.errors')); addToast(t('signin.errors')); } else if (strapiError === 'Auth.form.error.confirmed') { setError(t('signin.unconfirmed')); addToast(t('signin.unconfirmed')); } }; return (
{t('signin.title')} {error && {error}} setEmail(value)} id="SignInEmail" name="email" type="email" error={!!error} /> setPassword(value)} id="SignInEmail" name="password" type="password" error={!!error} /> {t('lost_password.message')}
); }; const getURLSearch = router => router.asPath.replace(router.route, ''); const useStyles = makeStyles(theme => ({ content: { display: 'flex', flexDirection: 'column', }, loader: { marginLeft: '14px', color: theme.palette.background.paper, }, actions: { justifyContent: 'center', marginBottom: theme.spacing(2), }, })); export default SignIn;