Skip to content

Commit

Permalink
feat: clean up auth flow
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinWu098 committed Oct 14, 2024
1 parent ebcdcde commit 27bd4af
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
16 changes: 15 additions & 1 deletion client/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { CookiesProvider } from "react-cookie";
import { Route, BrowserRouter as Router, Routes } from "react-router-dom";
import {
Navigate,
Route,
BrowserRouter as Router,
Routes,
} from "react-router-dom";

import { Admin } from "./components/admin/Admin";
import { CatchAll } from "./components/CatchAll";
Expand Down Expand Up @@ -41,6 +46,15 @@ const App = () => {
}
/>

<Route
path="/"
element={
<Navigate
to="/login"
replace
/>
}
/>
<Route
path="*"
element={<ProtectedRoute element={<CatchAll />} />}
Expand Down
8 changes: 7 additions & 1 deletion client/src/components/ProtectedRoute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ export const ProtectedRoute = ({

const roles = Array.isArray(allowedRoles) ? allowedRoles : [allowedRoles];
const isValidRole = getIsValidRole(roles, role);
return currentUser && isValidRole ? element : <Navigate to={"/login"} />;
return currentUser && isValidRole ? (
element
) : currentUser ? (
<Navigate to={"dashboard"} />
) : (
<Navigate to={"/"} />
);
};

/**
Expand Down
17 changes: 16 additions & 1 deletion client/src/components/dashboard/Dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useEffect, useState } from "react";

import {
Button,
Link as ChakraLink,
Heading,
Table,
TableCaption,
Expand All @@ -15,6 +16,8 @@ import {
VStack,
} from "@chakra-ui/react";

import { Link } from "react-router-dom";

import { useAuthContext } from "../../contexts/hooks/useAuthContext";
import { useBackendContext } from "../../contexts/hooks/useBackendContext";
import { useRoleContext } from "../../contexts/hooks/useRoleContext";
Expand Down Expand Up @@ -49,7 +52,19 @@ export const Dashboard = () => {
<Heading>Dashboard</Heading>

<VStack>
<Text> Signed in as {currentUser?.email}</Text>
<Text>
Signed in as {currentUser?.email} (
{role === "admin" ? "Admin" : "User"})
</Text>

{role === "admin" ? (
<ChakraLink
as={Link}
to={"/admin"}
>
Go to Admin Page
</ChakraLink>
) : null}
<Button onClick={logout}>Sign out</Button>
</VStack>

Expand Down
2 changes: 2 additions & 0 deletions client/src/components/login/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ export const Login = () => {
email: data.email,
password: data.password,
});

navigate("/dashboard");
} catch (err) {
const errorCode = err.code;
const firebaseErrorMsg = err.message;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
},
"packageManager": "yarn@1.22.22",
"engines": {
"yarn": "^1.22.22",
"yarn": "^1.22.0",
"node": ">=18"
}
}

0 comments on commit 27bd4af

Please sign in to comment.