diff --git a/src/pages/Auth/Login/index.tsx b/src/pages/Auth/Login/index.tsx index b38e6ba..9ae9151 100644 --- a/src/pages/Auth/Login/index.tsx +++ b/src/pages/Auth/Login/index.tsx @@ -1,9 +1,69 @@ +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 { Google, Login as LoginIllustration, Logo } from '../../../assets'; -import { Box, Button, Input, Link, Text } from '../../../components'; +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); + 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 ( { - - astrobuild - - - Login - - {}} /> - {}} - /> - - Forgot Password - - -