forked from AbhiramNS1/miniproject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
87 lines (74 loc) · 2.26 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const express = require("express");
const mysql = require("mysql2");
const bodyParser = require("body-parser");
const session = require("express-session");
const bcrypt = require("bcrypt");
const path = require("path");
const app = express();
// Parse JSON request bodies
app.use(bodyParser.json());
// Create a MySQL connection pool
const pool = mysql.createPool({
connectionLimit: 10,
host: "localhost",
user: "root",
password: "0000",
database: "project",
});
// Serve static files in the public directory
app.use(express.static("public"));
// Set up session management
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: true,
cookie: { secure: process.env.COOKIE_SECURE === "true" },
})
);
// Middleware function to check if user is authenticated
const requireAuth = (req, res, next) => {
if (!req.session.userId) {
// Redirect to login page if not authenticated
res.redirect("/login.html");
return;
}
next();
};
// Handle the login request on a POST request to the /login endpoint
app.post("/login", (req, res) => {
const { username, password } = req.body;
if (!username || !password) {
// Return error if either field is empty
res
.status(400)
.json({ error: "Please enter both a username and password" });
return;
}
// Check if username and password are correct
if (username === "user" && password === "password") {
req.session.userId = username;
res.status(200).json({ success: true, redirectUrl: "/dashboard.html" });
} else {
res.status(401).json({ success: false, error: "Invalid credentials" });
}
});
// Serve the login page on a GET request to the /login endpoint
app.get("/login", (req, res) => {
res.sendFile(path.join(__dirname, "public", "login.html"));
});
// Handle logout requests
app.get("/logout", (req, res) => {
// Clear user ID from session and redirect to login page
req.session.userId = undefined;
res.redirect("/login.html");
});
// Handle requests for main page
app.get("/dashboard.html", requireAuth, (req, res) => {
res.sendFile(path.join(__dirname, "public", "dashboard.html"));
});
// Start server
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});