import React, { useState } from "react"; import Link from 'next/link'; import StyledWrapper from './StyledWrapper'; import AuthApi from 'api/auth'; import { useFormik } from 'formik'; import * as Yup from 'yup'; import { useRouter } from 'next/router'; import { useAuth } from 'providers/Auth'; const SignUp = () => { const router = useRouter(); const [authState, authDispatch] = useAuth(); const [errorSigningUp, setErrorSigningUp] = useState(false); const [errorMsg, setErrorMsg] = useState(''); const [signingUp, setSigningUp] = useState(false); const { currentUser } = authState; const formik = useFormik({ initialValues: { name: '', email: '', password: '' }, validationSchema: Yup.object({ name: Yup.string() .min(3, 'Must be atleast 3 characters') .max(50, 'Must be 50 characters or less') .required('Required'), email: Yup.string() .email('Invalid email address') .required('Required'), password: Yup.string() .min(8, 'Must be atleast 8 characters') .max(50, 'Must be 50 characters or less') .required('Required') }), onSubmit: (values, { resetForm }) => { setSigningUp(true); AuthApi .signup({ name: values.name, email: values.email, password: values.password }) .then((response) => { authDispatch({ type: 'LOGIN_SUCCESS', user: response.data }); }) .catch((error) => { setSigningUp(false); setErrorSigningUp(true); setErrorMsg(error.message || 'An error occured during signup') }); setSigningUp(false); }, }); if(authState.isLoading) { return null; }; if(currentUser) { router.push('/'); return null; }; return (
grafnode
Opensource API Collection Collaboration
Create Account
setErrorSigningUp(false)} /> {formik.touched.name && formik.errors.name ? (
{formik.errors.name}
) : null}
setErrorSigningUp(false)} /> {formik.touched.email && formik.errors.email ? (
{formik.errors.email}
) : null}
setErrorSigningUp(false)} /> {formik.touched.password && formik.errors.password ? (
{formik.errors.password}
) : null}
By signing in you are agreeing to our Terms of Use and our Privacy Policy.
{ signingUp ? (
) :
{errorSigningUp ? (
{errorMsg}
) : null}
}
Already have an account? Log in
) }; export default SignUp;