Skip to content

Commit

Permalink
Changed protected routing
Browse files Browse the repository at this point in the history
  • Loading branch information
naheyansheikh committed Nov 27, 2024
1 parent 0ad68ea commit 574ee5c
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 10 deletions.
2 changes: 1 addition & 1 deletion frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"@heroicons/react": "^2.2.0",
"@mui/icons-material": "^6.1.2",
"@mui/material": "^6.1.2",
"@supabase/supabase-js": "^2.45.5",
"@supabase/supabase-js": "^2.46.1",
"axios": "^1.7.7",
"dependencies": "^0.0.1",
"pdfjs-dist": "^4.7.76",
Expand Down
13 changes: 10 additions & 3 deletions frontend/src/contexts/AuthContext.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,16 @@ export function AuthProvider({ children }) {
}

async function logout() {
const { error } = await supabase.auth.signOut();
if (error) {
console.log(error);
try {
const { error } = await supabase.auth.signOut();
if (error) throw error;
setSession(null);
} catch (error) {
if (error.message === "Auth session missing!") {
setSession(null);
return;
}
console.error("Logout error:", error);
throw error;
}
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/pages/login/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const Login = () => {
try {
setLoading(true);
await login(email, password);
navigate("/dashboard");
navigate("/home");
} catch {
alert("Failed to login: Email or Password Incorrect");
} finally {
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/utils/ProtectedRoutes.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Outlet, Navigate } from 'react-router-dom';
import { useAuth } from '../contexts/AuthContext';
import { Outlet, Navigate } from "react-router-dom";
import { useAuth } from "../contexts/AuthContext";

export const ProtectedRoutes = () => {
const { session } = useAuth();
return session ? <Outlet /> : <Navigate to='/home' />;
}
return session ? <Outlet /> : <Navigate to="/login" replace />;
};

0 comments on commit 574ee5c

Please sign in to comment.