import React, { useState } from "react"; import * as Yup from 'yup'; import Link from 'next/link'; import { useRouter } from 'next/router'; import { useAuth } from 'providers/Auth'; import IdentityApi from 'api/identity'; import { useFormik } from 'formik'; import StyledWrapper from './StyledWrapper'; const Login = () => { const router = useRouter(); const [authState, authDispatch] = useAuth(); const { currentUser } = authState; const [loggingIn, setLoggingIn] = useState(false); const [loginError, setLoginError] = useState(false); const formik = useFormik({ initialValues: { email: '', password: '', }, validationSchema: Yup.object({ email: Yup.string() .required('Email is required'), password: Yup.string() .required('Password is required') }), onSubmit: (values, { resetForm }) => { setLoggingIn(true); IdentityApi .login({ email: values.email, password: values.password }) .then((response) => { authDispatch({ type: 'LOGIN_SUCCESS', user: response.data }); }) .catch((error) => { setLoggingIn(false); setLoginError(true); }); }, }); if(authState.isLoading) { return null; }; if(currentUser) { router.push('/home'); return null; }; return (
grafnode
Opensource API Collection Collaboration
Login
setLoginError(false)} onBlur={formik.handleBlur} value={formik.values.email} /> {formik.touched.email && formik.errors.email ? (
{formik.errors.email}
) : null}
setLoginError(false)} onBlur={formik.handleBlur} value={formik.values.password} /> {formik.touched.password && formik.errors.password ? (
{formik.errors.password}
) : null}
{ loggingIn ? ( ) :
} {loginError ? (
Invalid Credentials
) : null}
No account? Create one!
) }; export default Login;