Skip to content

Commit fe13740

Browse files
committed
add alert when accessing pages before authentication
1 parent a09176d commit fe13740

File tree

3 files changed

+174
-2
lines changed

3 files changed

+174
-2
lines changed

frontend/package-lock.json

Lines changed: 154 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/src/auth/AuthGuard.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface AuthGuardProps {
99
export default function AuthGuard({ children }: AuthGuardProps) {
1010
const { user } = useAuth();
1111
if (!user) {
12-
return <Navigate to="/login" />;
12+
return <Navigate to="/login" state={{ showAlert: true }} />;
1313
}
1414
return <>{children}</>;
1515
}

frontend/src/pages/login.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,37 @@
11
import { useState } from "react";
2-
import { Box, Button, CssBaseline, TextField, Typography } from "@mui/material";
2+
import {
3+
Alert,
4+
Box,
5+
Button,
6+
Collapse,
7+
CssBaseline,
8+
TextField,
9+
Typography,
10+
} from "@mui/material";
311
import CenteredContainer from "../components/CenteredContainer";
412
import Navbar from "../components/Navbar";
513
import PasswordField from "../components/PasswordField";
614
import { useAuth } from "../auth/auth.context";
15+
import { useLocation } from "react-router-dom";
716

817
export default function Login() {
918
const { error, login } = useAuth();
1019
const [email, setEmail] = useState("");
1120
const [password, setPassword] = useState("");
21+
const { state } = useLocation();
22+
const [show, setShow] = useState(true);
1223

1324
const handleClickLogin = () => login(email, password);
1425

1526
return (
1627
<Box height="100vh">
28+
{state?.showAlert && (
29+
<Collapse in={show}>
30+
<Alert onClose={() => setShow(false)} severity="warning">
31+
You are not logged in. Please log in to continue.
32+
</Alert>
33+
</Collapse>
34+
)}
1735
<CssBaseline />
1836
<Navbar />
1937
<CenteredContainer>

0 commit comments

Comments
 (0)