authorizer/app/src/Root.tsx

43 lines
882 B
TypeScript
Raw Normal View History

2021-08-06 13:47:52 +00:00
import React, { useEffect } from 'react';
import { Switch, Route } from 'react-router-dom';
import { useAuthorizer } from '@authorizerdev/authorizer-react';
import Dashboard from './pages/dashboard';
import Login from './pages/login';
import ResetPassword from './pages/rest-password';
export default function Root() {
2021-08-06 13:47:52 +00:00
const { token, loading, config } = useAuthorizer();
2021-08-06 13:47:52 +00:00
useEffect(() => {
if (token && config.redirectURL !== window.location.toString()) {
window.location.href = config.redirectURL;
}
return () => {};
}, [token]);
2021-08-06 13:47:52 +00:00
if (loading) {
return <h1>Loading...</h1>;
}
2021-08-06 13:47:52 +00:00
if (token) {
return (
<Switch>
<Route path="/app" exact>
<Dashboard />
</Route>
</Switch>
);
}
return (
<Switch>
<Route path="/app" exact>
<Login />
</Route>
<Route path="/app/reset-password">
<ResetPassword />
</Route>
</Switch>
);
}