diff --git a/src/components/AuthUser/SignInForm/SignInForm.tsx b/src/components/AuthUser/SignInForm/SignInForm.tsx index 765d9a5861f2817901109632808342ae51907092..0e63a94b93b648f823be50ea0af4616e3a5c7a4a 100644 --- a/src/components/AuthUser/SignInForm/SignInForm.tsx +++ b/src/components/AuthUser/SignInForm/SignInForm.tsx @@ -1,28 +1,11 @@ import React, { FC } from 'react'; import axios from 'axios'; import { SubmitHandler, useForm } from 'react-hook-form'; -import { createStyles, makeStyles, Theme } from '@material-ui/core/styles'; import { Button } from '@material-ui/core'; import { InputField } from 'components/AuthUser/SignInForm/InputField/InputField'; import { useHistory } from 'react-router-dom'; import { AuthRoutes } from 'components/api/routes'; - -const useStyles = makeStyles((theme: Theme) => - createStyles({ - root: { - '& > *': { - margin: theme.spacing(0), - }, - }, - form: { - width: '100%', // Fix IE 11 issue. - marginTop: theme.spacing(1), - }, - submit: { - margin: theme.spacing(3, 0, 2), - }, - }), -); +import { useStyles } from './useStyles'; export const SignInForm: FC = () => { const history = useHistory(); @@ -37,7 +20,7 @@ export const SignInForm: FC = () => { password: '', }; - const { control, errors, handleSubmit } = useForm<FormData>({ + const { control, errors, setError, handleSubmit } = useForm<FormData>({ defaultValues, }); @@ -55,8 +38,19 @@ export const SignInForm: FC = () => { }, }, ) - .then(() => { - history.replace(AuthRoutes.dashboard); + .then((response) => { + if (response.data.status === 'fail') { + setError('email', { + type: 'server', + message: 'Something went wrong with email', + }); + setError('password', { + type: 'server', + message: 'Something went wrong with password', + }); + } else if (response.data.status === 'success') { + history.replace(AuthRoutes.dashboard); + } }); }; diff --git a/src/components/AuthUser/SignInForm/useStyles.ts b/src/components/AuthUser/SignInForm/useStyles.ts new file mode 100644 index 0000000000000000000000000000000000000000..4a510bf92bf7cffee0efce3aebfd00998f44a3be --- /dev/null +++ b/src/components/AuthUser/SignInForm/useStyles.ts @@ -0,0 +1,18 @@ +import { createStyles, makeStyles, Theme } from '@material-ui/core/styles'; + +export const useStyles = makeStyles((theme: Theme) => + createStyles({ + root: { + '& > *': { + margin: theme.spacing(0), + }, + }, + form: { + width: '100%', // Fix IE 11 issue. + marginTop: theme.spacing(1), + }, + submit: { + margin: theme.spacing(3, 0, 2), + }, + }), +); diff --git a/src/components/api/PrivateRoute/PrivateRoute.tsx b/src/components/api/PrivateRoute/PrivateRoute.tsx index 60332f89ed1feae362f5fa41b3ea0abf03f31123..e9e431cbf88b9a481bc95016c3729a2f30a0c9e9 100644 --- a/src/components/api/PrivateRoute/PrivateRoute.tsx +++ b/src/components/api/PrivateRoute/PrivateRoute.tsx @@ -25,12 +25,10 @@ export const PrivateRoute = ({ useEffect(() => { const fetch = async (): Promise<unknown> => { const result = await axios('/api/web/login/is_authenticated'); - // FIX: Remove negation and use true server data - setAuth(!result.data.is_authenticated); + setAuth(result.data.is_authenticated); setLoading(true); return null; }; - fetch(); }, []);