Skip to content

Commit

Permalink
Auth Refactor Routing (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
williamputraintan authored Sep 21, 2022
1 parent 05d514f commit e968e7a
Showing 1 changed file with 26 additions and 28 deletions.
54 changes: 26 additions & 28 deletions src/routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,38 @@ import LIMSPage from '../pages/LIMS';
import MenuBar from '../layouts/MenuBar';

function Routes() {
return (
<RouterRoutes>
<Route path='/' element={<ProtectedRoute />}>
{/* NoPath redirects to HomePage */}
<Route index element={<HomePage />} />

<Route path='/signIn' element={<SignInPage />} />
<Route path='/metadata' element={<MetadataPage />} />
<Route path='/lims' element={<LIMSPage />} />
const isUserSignedIn = useUserContext().isAuth;

if (!isUserSignedIn) {
return (
<RouterRoutes>
<Route path='/signIn' element={<SignInPage />} />
<Route path='/metadata' element={<MetadataPage />} />
<Route path='/lims' element={<LIMSPage />} />

{/* Complicated routing or more than one routing will be split into their own component. */}
<Route path='/subjects/*' element={<SubjectRoutes />} />

{/* Non matching page redirect to NotFound */}
<Route path='*' element={<h1>NotFoundPage</h1>}></Route>
</Route>
<Route path='/signIn' element={<SignInPage />}></Route>
</RouterRoutes>
);
<Route path='*' element={<Navigate replace to='signIn' />} />
</RouterRoutes>
);
} else {
return (
<RouterRoutes>
<Route path='/' element={<SignedInLayout />}>
<Route index element={<HomePage />} />
<Route path='/metadata' element={<MetadataPage />} />
<Route path='/lims' element={<LIMSPage />} />

{/* More than one routing for the same prefix will be split into their own component. */}
<Route path='/subjects/*' element={<SubjectRoutes />} />

{/* Non matching page redirect to NotFound */}
<Route path='*' element={<h1>NotFoundPage</h1>}></Route>
</Route>
<Route path='/signIn' element={<Navigate replace to='/' />}></Route>
</RouterRoutes>
);
}
}

export default Routes;

function ProtectedRoute() {
// If not signedIn redirect to `/signIn` page
const isUserSignedIn = useUserContext().isAuth;
if (!isUserSignedIn) {
return <Navigate replace to='signIn' />;
}

function SignedInLayout() {
// Add layout componet for SignedIn page
return (
<>
Expand Down

0 comments on commit e968e7a

Please sign in to comment.