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

Cookie fetch and set role to AuthContext.

parent bbb9b6c9
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, useContext } from 'react'; import React, { FC, useContext, useEffect, useState } 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';
...@@ -8,9 +8,29 @@ import { AuthRoutes } from 'api/routes'; ...@@ -8,9 +8,29 @@ import { AuthRoutes } from 'api/routes';
import { AuthContext } from 'components/AuthUser/AuthContext'; import { AuthContext } from 'components/AuthUser/AuthContext';
import { useStyles } from './useStyles'; import { useStyles } from './useStyles';
const configDjangoCookieName = (): void => {
axios.defaults.xsrfHeaderName = 'X-CSRFTOKEN';
axios.defaults.xsrfCookieName = 'csrftoken';
axios.defaults.withCredentials = true;
};
export const SignInForm: FC = () => { export const SignInForm: FC = () => {
const [isCookieFetched, setisCookieFetched] = useState<string>('');
configDjangoCookieName();
useEffect(() => {
const fetchCookie = async (): Promise<unknown> => {
const response = await axios('/api/web/csrf');
setisCookieFetched(response.data.token);
return null;
};
if (!isCookieFetched) fetchCookie();
}, [isCookieFetched]);
console.log(isCookieFetched);
const history = useHistory(); const history = useHistory();
const { role, setRole } = useContext(AuthContext); const { setRole } = useContext(AuthContext);
interface FormData { interface FormData {
username: string; username: string;
...@@ -33,7 +53,7 @@ export const SignInForm: FC = () => { ...@@ -33,7 +53,7 @@ export const SignInForm: FC = () => {
{ {
username: values.username, username: values.username,
password: values.password, password: values.password,
csrfmiddlewaretoken: sessionStorage.getItem('X-CSRFTOKEN'), csrfmiddlewaretoken: isCookieFetched,
}, },
{ {
headers: { headers: {
...@@ -42,7 +62,6 @@ export const SignInForm: FC = () => { ...@@ -42,7 +62,6 @@ 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',
...@@ -53,7 +72,6 @@ export const SignInForm: FC = () => { ...@@ -53,7 +72,6 @@ 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); 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