2020-10-21 01:52:09 +02:00
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
import { Switch, Route, useHistory } from 'react-router-dom';
|
2020-04-10 04:40:22 +02:00
|
|
|
import * as H from 'history';
|
|
|
|
|
2020-04-10 18:31:29 +02:00
|
|
|
import Dashboard from 'Dashboard';
|
2020-06-23 22:20:53 +02:00
|
|
|
import Admin from 'Admin';
|
2020-12-31 03:56:59 +01:00
|
|
|
import MyTasks from 'MyTasks';
|
2020-10-21 01:52:09 +02:00
|
|
|
import Confirm from 'Confirm';
|
2020-04-10 04:40:22 +02:00
|
|
|
import Projects from 'Projects';
|
|
|
|
import Project from 'Projects/Project';
|
2020-06-23 22:20:53 +02:00
|
|
|
import Teams from 'Teams';
|
2020-04-10 04:40:22 +02:00
|
|
|
import Login from 'Auth';
|
2020-10-21 01:52:09 +02:00
|
|
|
import Register from 'Register';
|
2020-06-13 00:21:58 +02:00
|
|
|
import Profile from 'Profile';
|
|
|
|
import styled from 'styled-components';
|
2020-10-21 01:52:09 +02:00
|
|
|
import JwtDecode from 'jwt-decode';
|
|
|
|
import { setAccessToken } from 'shared/utils/accessToken';
|
|
|
|
import { useCurrentUser } from 'App/context';
|
2020-04-10 04:40:22 +02:00
|
|
|
|
2020-06-13 00:21:58 +02:00
|
|
|
const MainContent = styled.div`
|
2020-06-23 22:20:53 +02:00
|
|
|
padding: 0 0 0 0;
|
2020-06-13 00:21:58 +02:00
|
|
|
background: #262c49;
|
|
|
|
height: 100%;
|
2020-06-19 01:12:15 +02:00
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
flex-grow: 1;
|
2020-06-13 00:21:58 +02:00
|
|
|
`;
|
2020-08-23 19:27:56 +02:00
|
|
|
|
2020-12-19 03:34:35 +01:00
|
|
|
type RefreshTokenResponse = {
|
|
|
|
accessToken: string;
|
|
|
|
setup?: null | { confirmToken: string };
|
|
|
|
};
|
|
|
|
|
2020-10-21 01:52:09 +02:00
|
|
|
const AuthorizedRoutes = () => {
|
|
|
|
const history = useHistory();
|
|
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
const { setUser } = useCurrentUser();
|
|
|
|
useEffect(() => {
|
|
|
|
fetch('/auth/refresh_token', {
|
|
|
|
method: 'POST',
|
|
|
|
credentials: 'include',
|
|
|
|
}).then(async x => {
|
|
|
|
const { status } = x;
|
|
|
|
if (status === 400) {
|
|
|
|
history.replace('/login');
|
|
|
|
} else {
|
|
|
|
const response: RefreshTokenResponse = await x.json();
|
|
|
|
const { accessToken, setup } = response;
|
|
|
|
if (setup) {
|
|
|
|
history.replace(`/register?confirmToken=${setup.confirmToken}`);
|
|
|
|
} else {
|
|
|
|
const claims: JWTToken = JwtDecode(accessToken);
|
|
|
|
const currentUser = {
|
|
|
|
id: claims.userId,
|
|
|
|
roles: { org: claims.orgRole, teams: new Map<string, string>(), projects: new Map<string, string>() },
|
|
|
|
};
|
|
|
|
setUser(currentUser);
|
|
|
|
setAccessToken(accessToken);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setLoading(false);
|
|
|
|
});
|
|
|
|
}, []);
|
|
|
|
return loading ? null : (
|
|
|
|
<Switch>
|
|
|
|
<MainContent>
|
|
|
|
<Route exact path="/" component={Dashboard} />
|
|
|
|
<Route exact path="/projects" component={Projects} />
|
|
|
|
<Route path="/projects/:projectID" component={Project} />
|
|
|
|
<Route path="/teams/:teamID" component={Teams} />
|
|
|
|
<Route path="/profile" component={Profile} />
|
|
|
|
<Route path="/admin" component={Admin} />
|
2020-12-31 03:56:59 +01:00
|
|
|
<Route path="/tasks" component={MyTasks} />
|
2020-10-21 01:52:09 +02:00
|
|
|
</MainContent>
|
|
|
|
</Switch>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-04-10 04:40:22 +02:00
|
|
|
type RoutesProps = {
|
|
|
|
history: H.History;
|
|
|
|
};
|
|
|
|
|
2020-08-23 19:27:56 +02:00
|
|
|
const Routes: React.FC<RoutesProps> = () => (
|
2020-04-20 05:02:55 +02:00
|
|
|
<Switch>
|
|
|
|
<Route exact path="/login" component={Login} />
|
2020-10-21 01:52:09 +02:00
|
|
|
<Route exact path="/register" component={Register} />
|
|
|
|
<Route exact path="/confirm" component={Confirm} />
|
|
|
|
<AuthorizedRoutes />
|
2020-04-20 05:02:55 +02:00
|
|
|
</Switch>
|
2020-04-10 04:40:22 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
export default Routes;
|