Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auth Refactor Routing #151

Merged
merged 3 commits into from
Sep 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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