-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
152 lines (132 loc) · 4.06 KB
/
index.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const express = require('express');
const bcrypt = require('bcrypt');
const session = require('express-session');
const { LoginModel, SignupModel, CarModel } = require("./config");
require('dotenv').config();
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(session({
secret: process.env.SESSION_SECRET || "275101", // Use environment variable for secret
resave: false,
saveUninitialized: false,
}));
app.set('view engine', 'ejs');
app.use(express.static("public"));
// Prevent caching
app.use((req, res, next) => {
res.set('Cache-Control', 'no-store');
next();
});
// Middleware to protect routes
function checkAuthenticated(req, res, next) {
if (req.session.userId) {
return next();
} else {
res.redirect('/Login');
}
}
// Routes
app.get("/", (req, res) => {
res.redirect("/Login");
});
app.get("/Signup", (req, res) => {
res.render("Signup");
});
app.post("/Signup", async (req, res) => {
try {
if (!req.body.username || !req.body.password) {
return res.status(400).send("Username and password are required");
}
const hashedPassword = await bcrypt.hash(req.body.password, 10);
const data = {
name: req.body.username,
password: hashedPassword
};
await SignupModel.insertMany([data]);
res.redirect("/Login");
} catch (error) {
console.error("Signup error:", error);
res.status(500).send("Error signing up");
}
});
app.get("/Login", (req, res) => {
res.render("Login");
});
app.post("/Login", async (req, res) => {
try {
const user = await SignupModel.findOne({ name: req.body.username });
if (user && await bcrypt.compare(req.body.password, user.password)) {
req.session.userId = user._id;
res.redirect("/Home");
} else {
res.send("Invalid username or password");
}
} catch (error) {
console.error(error);
res.status(500).send("Error logging in");
}
});
app.get('/Logout', checkAuthenticated, (req, res) => {
req.session.destroy(err => {
if (err) {
console.log("Error during logout", err);
return res.status(500).send('Error logging out');
}
res.redirect('/Login');
});
});
app.get("/Home", checkAuthenticated, async (req, res) => {
try {
const cars = await CarModel.find();
res.render("Home", { cars });
} catch (error) {
console.error("Error fetching car data:", error);
res.status(500).send("Error fetching car data");
}
});
app.post("/Home", checkAuthenticated, async (req, res) => {
try {
const data = {
name: req.body.name,
model: req.body.model,
price: req.body.price,
manufactureYear: req.body.manufactureYear
};
await CarModel.insertMany([data]);
res.redirect("/Home");
} catch (error) {
console.error("Car error:", error);
res.status(500).send("Error adding car");
}
});
app.post("/updateCar/:id", checkAuthenticated, async (req, res) => {
try {
const carId = req.params.id;
const updatedData = {
name: req.body.name,
model: req.body.model,
price: req.body.price,
manufactureYear: req.body.manufactureYear
};
await CarModel.findByIdAndUpdate(carId, updatedData);
res.redirect("/Home");
} catch (error) {
console.error("Error updating car data:", error);
res.status(500).send("Error updating car data");
}
});
app.post("/deleteCar/:id", checkAuthenticated, async (req, res) => {
try {
const carId = req.params.id;
await CarModel.findByIdAndDelete(carId);
res.redirect("/Home");
} catch (error) {
console.error("Error deleting car data:", error);
res.status(500).send("Error deleting car data");
}
});
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Server running on port: ${port}`);
});