Skip to content
Snippets Groups Projects
Verified Commit fb9d3419 authored by Defendi Alberto's avatar Defendi Alberto
Browse files

Save user role.

parent 2d3fee54
No related branches found
No related tags found
2 merge requests!56Refined auth flow and new website pages.,!43Move back cookie fetch to SignInForm. Role fetched and saved into a Context. Small refactorings.
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);
} }
}); });
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment