Skip to content
Snippets Groups Projects

Polished auth flow including ReservedRoute and role selection.

Merged Defendi Alberto requested to merge feature/privateroute/role into dev
1 file
+ 31
24
Compare changes
  • Side-by-side
  • Inline
import React, { FC, useContext, useEffect, useState } from 'react';
import React, { FC, useEffect, useState } from 'react';
import axios from 'axios';
import Button from '@material-ui/core/Button';
import Button from '@material-ui/core/Button';
import { AuthContext } from '../AuthContext';
import { getRoles } from 'api/getRoles';
import { setRole } from 'api/setRole';
const choseAndForward = (e: unknown): void => {
import { useHistory } from 'react-router-dom';
// Do nothing.
import { AuthRoutes } from 'api/routes';
};
/**
/**
* Screen that let's users decide role between available roles.
* Page that let's users decide role between available roles.
* This is intended to use when a user has more than one role.
* This is intended to use when a user has more than one role.
* @returns ChoseRole component.
* @returns ChoseRole component.
*/
*/
export const ChoseRole: FC = () => {
export const ChoseRole: FC = () => {
const [userRoles, setUserRoles] = useState<Array<string>>(['']);
const history = useHistory();
 
const [userRoles, setUserRoles] = useState<string[]>(['hello', 'world']);
 
 
const choseAndForward = (role: string): void => {
 
// Set role in the server.
 
setRole(role);
 
// Push to homepage.
 
history.push(`${AuthRoutes.dashboard}${AuthRoutes.home}`);
 
};
useEffect(() => {
useEffect(() => {
const getUserRoles = async (): Promise<unknown> => {
getRoles().then((fetchedRoles) => setUserRoles(fetchedRoles));
const response = await axios('/api/web/get_role');
}, []);
setUserRoles(response.data.token);
return null;
};
getUserRoles();
}, [userRoles]);
const { role } = useContext(AuthContext);
return (
return (
<div data-testid="ChoseRole">
<div data-testid="ChoseRole">
<Button
{userRoles ? (
variant="outlined"
userRoles.map((role) => (
color="default"
<Button
onClick={(e) => choseAndForward(e)}
variant="outlined"
>
color="default"
{role}
key={role}
</Button>
onClick={() => choseAndForward(role)}
 
>
 
{role}
 
</Button>
 
))
 
) : (
 
<h1>No roles were returned.</h1>
 
)}
</div>
</div>
);
);
};
};
Loading