Skip to content
Snippets Groups Projects

Move back cookie fetch to SignInForm. Role fetched and saved into a Context. Small refactorings.

Merged Defendi Alberto requested to merge feature/role into dev
1 file
+ 6
1
Compare changes
  • Side-by-side
  • Inline
import React, { FC } from 'react';
import React, { FC, useContext } from 'react';
import axios from 'axios';
import axios from 'axios';
import { SubmitHandler, useForm } from 'react-hook-form';
import { SubmitHandler, useForm } from 'react-hook-form';
import { Button } from '@material-ui/core';
import { Button } from '@material-ui/core';
import { InputField } from 'components/AuthUser/InputField/InputField';
import { InputField } from 'components/AuthUser/InputField/InputField';
import { useHistory } from 'react-router-dom';
import { useHistory } from 'react-router-dom';
import { AuthRoutes } from 'api/routes';
import { AuthRoutes } from 'api/routes';
 
import { AuthContext } from 'components/AuthUser/AuthContext';
import { useStyles } from './useStyles';
import { useStyles } from './useStyles';
export const SignInForm: FC = () => {
export const SignInForm: FC = () => {
const history = useHistory();
const history = useHistory();
 
const { role, setRole } = useContext(AuthContext);
interface FormData {
interface FormData {
username: string;
username: string;
@@ -40,6 +42,7 @@ export const SignInForm: FC = () => {
@@ -40,6 +42,7 @@ export const SignInForm: FC = () => {
},
},
)
)
.then((response) => {
.then((response) => {
 
console.log(response);
if (response.data.status === 'fail') {
if (response.data.status === 'fail') {
setError('username', {
setError('username', {
type: 'server',
type: 'server',
@@ -50,6 +53,8 @@ export const SignInForm: FC = () => {
@@ -50,6 +53,8 @@ export const SignInForm: FC = () => {
message: 'Something went wrong with password',
message: 'Something went wrong with password',
});
});
} else if (response.data.status === 'success') {
} else if (response.data.status === 'success') {
 
localStorage.setItem('ROLE', response.data.role);
 
setRole(response.data.role);
history.replace(AuthRoutes.dashboard);
history.replace(AuthRoutes.dashboard);
}
}
});
});
Loading