Added 'Unauthorized' Page when accessing / without being logged in
This commit is contained in:
+24
-5
@@ -111,11 +111,30 @@ app.post("/login", (request, response) => {
|
|||||||
response.json({username, token});
|
response.json({username, token});
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get("/", authenticateToken, (request, response) => {
|
app.get("/", (req, res) => {
|
||||||
/* #swagger.security = [{
|
const token =
|
||||||
"bearerAuth": []
|
req.headers.authorization?.split(" ")[1] ||
|
||||||
}] */
|
req.cookies.authorization;
|
||||||
response.sendFile(path.join(__dirname, "./public/home.html"));
|
|
||||||
|
if (!token) {
|
||||||
|
return res.sendFile(path.join(__dirname, "./public/unauthorized.html"));
|
||||||
|
}
|
||||||
|
|
||||||
|
jwt.verify(token, process.env.TOKEN_SECRET, (err, user) => {
|
||||||
|
if (err) {
|
||||||
|
return res.sendFile(path.join(__dirname, "./public/unauthorized.html"));
|
||||||
|
}
|
||||||
|
|
||||||
|
const users = get_data();
|
||||||
|
const foundUser = users.find(u => u.username === user.username);
|
||||||
|
|
||||||
|
if (!foundUser || foundUser.token !== token) {
|
||||||
|
return res.sendFile(path.join(__dirname, "./public/unauthorized.html"));
|
||||||
|
}
|
||||||
|
|
||||||
|
req.user = user;
|
||||||
|
res.sendFile(path.join(__dirname, "./public/home.html"));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get("/balance", authenticateToken, (request, response) => {
|
app.get("/balance", authenticateToken, (request, response) => {
|
||||||
|
|||||||
@@ -0,0 +1,39 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Access Denied</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
background-color: #111;
|
||||||
|
color: #0f0;
|
||||||
|
font-family: Arial, sans-serif;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
height: 100vh;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
a.button {
|
||||||
|
margin-top: 20px;
|
||||||
|
padding: 12px 24px;
|
||||||
|
background-color: #0f0;
|
||||||
|
color: #000;
|
||||||
|
font-weight: bold;
|
||||||
|
text-decoration: none;
|
||||||
|
border-radius: 8px;
|
||||||
|
box-shadow: 0 0 10px #0f0, 0 0 5px #0f0 inset;
|
||||||
|
}
|
||||||
|
|
||||||
|
a.button:hover {
|
||||||
|
background-color: #1f1;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>You must be logged in to access this page.</h1>
|
||||||
|
<a href="/login" class="button">Go to Login</a>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user