import * as Yup from 'yup'; import { useFormik } from 'formik'; import { useMutation } from '@apollo/client'; import { useHistory } from 'react-router'; import { useState } from 'react'; import { tokenVar, roleVar, userVar } from '../../../graphql/state'; import { Login as LoginIllustration, Logo } from '../../../assets'; import { Alert, Box, Button, Input, Link, Text } from '../../../components'; import { theme } from '../../../themes'; import { Wrapper } from './styles'; import { LOGIN } from '../../../graphql/auth.api'; import { LoginMutation, LoginMutationVariables } from '../../../graphql/types'; const Login = () => { const history = useHistory(); const [error, setError] = useState(''); const [login, { loading }] = useMutation< LoginMutation, LoginMutationVariables >(LOGIN, { onCompleted({ login: { user, token } }) { switch (user.role) { case 'Client': roleVar('client'); break; case 'ProductOwner': roleVar('productOwner'); break; case 'Developer': roleVar('developer'); break; case 'Admin': roleVar('admin'); break; default: break; } tokenVar(token); userVar(user); localStorage.setItem('token', token); history.push('/'); }, onError({ graphQLErrors }) { setError(graphQLErrors[0]?.extensions?.info); setTimeout(() => setError(''), 3000); }, }); const form = useFormik({ initialValues: { email: '', password: '', }, validationSchema: Yup.object().shape({ email: Yup.string() .required('Email is required') .email('Email is invalid'), password: Yup.string() .required('Password is required') .min(6, 'Password is 6 characters minimum'), }), onSubmit: ({ email, password }) => { login({ variables: { email, password } }); }, }); return ( Login {error && }
Forgot Password