2021-01-17 12:54:49 -08:00
|
|
|
import React from 'react';
|
2024-01-03 17:16:16 -05:00
|
|
|
import { Switch, Route, RouteProps } from 'react-router-dom';
|
2021-01-17 12:54:49 -08:00
|
|
|
import { useReactiveVar } from '@apollo/client';
|
|
|
|
import { LogIn } from './auth/LogIn';
|
2022-06-08 21:13:22 -04:00
|
|
|
import { SignUp } from './auth/SignUp';
|
|
|
|
import { ResetCredentials } from './auth/ResetCredentials';
|
2021-01-17 12:54:49 -08:00
|
|
|
import { NoPageFound } from './shared/NoPageFound';
|
|
|
|
import { PageRoutes } from '../conf/Global';
|
2021-03-11 13:38:35 -08:00
|
|
|
import { isLoggedInVar } from './auth/checkAuthStatus';
|
2021-05-11 15:41:42 -07:00
|
|
|
import { useTrackPageView } from './analytics';
|
2021-09-02 19:05:13 -07:00
|
|
|
import { ProtectedRoutes } from './ProtectedRoutes';
|
2021-01-17 12:54:49 -08:00
|
|
|
|
|
|
|
const ProtectedRoute = ({
|
|
|
|
isLoggedIn,
|
|
|
|
...props
|
|
|
|
}: {
|
|
|
|
isLoggedIn: boolean;
|
|
|
|
} & RouteProps) => {
|
2021-08-04 11:55:03 -07:00
|
|
|
const currentPath = window.location.pathname + window.location.search;
|
2021-01-17 12:54:49 -08:00
|
|
|
if (!isLoggedIn) {
|
2021-08-04 11:55:03 -07:00
|
|
|
window.location.replace(`${PageRoutes.AUTHENTICATE}?redirect_uri=${encodeURIComponent(currentPath)}`);
|
2021-03-11 13:38:35 -08:00
|
|
|
return null;
|
2021-01-17 12:54:49 -08:00
|
|
|
}
|
|
|
|
return <Route {...props} />;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2021-09-02 19:05:13 -07:00
|
|
|
* Container for all top-level routes.
|
2021-01-17 12:54:49 -08:00
|
|
|
*/
|
|
|
|
export const Routes = (): JSX.Element => {
|
2021-05-11 15:41:42 -07:00
|
|
|
useTrackPageView();
|
2021-01-17 12:54:49 -08:00
|
|
|
const isLoggedIn = useReactiveVar(isLoggedInVar);
|
2021-02-03 11:49:51 -08:00
|
|
|
|
2021-01-17 12:54:49 -08:00
|
|
|
return (
|
2021-09-02 19:05:13 -07:00
|
|
|
<Switch>
|
|
|
|
<Route path={PageRoutes.LOG_IN} component={LogIn} />
|
2022-06-08 21:13:22 -04:00
|
|
|
<Route path={PageRoutes.SIGN_UP} component={SignUp} />
|
|
|
|
<Route path={PageRoutes.RESET_CREDENTIALS} component={ResetCredentials} />
|
2021-09-02 19:05:13 -07:00
|
|
|
<ProtectedRoute isLoggedIn={isLoggedIn} render={() => <ProtectedRoutes />} />
|
2022-06-23 00:04:53 +05:30
|
|
|
<Route path="/*" component={NoPageFound} />
|
2021-09-02 19:05:13 -07:00
|
|
|
</Switch>
|
2021-01-17 12:54:49 -08:00
|
|
|
);
|
|
|
|
};
|