-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
200 lines (166 loc) · 4.63 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
const express = require("express");
const mongoose = require("mongoose");
const bcrypt = require("bcrypt");
const jwt = require("jsonwebtoken");
const dotenv = require("dotenv");
const cookieParser = require("cookie-parser");
dotenv.config();
const port = process.env.PORT || 3000;
const url = process.env.MONGO_URI||"mongodb://localhost:27017/userAuthDB";
mongoose
.connect(url)
.then(() => {
console.log("database connected");
})
.catch((err) => {
console.log(err);
})
.finally(() => {
app.listen(port, () => {
console.log(`listening on ${port}`);
});
});
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.set("view engine", "ejs");
const userSchema = new mongoose.Schema({
username: String,
password: String,
role: {
type: String,
default: "user",
},
});
const User = mongoose.model("User", userSchema);
const isAuthenticated = (req, res, next) => {
try {
const token = req.cookies ? req.cookies.token : null;
if (!token) {
return res.redirect("/login");
}
const decoded = jwt.verify(token, "anykey");
req.userData = decoded;
next();
} catch (err) {
return res.redirect("/login");
}
};
app.get("/", (req, res) => {
res.render("home");
});
app.get("/login", (req, res) => {
res.render("login");
});
app.get("/register", (req, res) => {
res.render("register");
});
const validateUsername = (username) => {
const usernameRegex = /^[a-zA-Z_]{4,}$/;
return usernameRegex.test(username);
};
const validatePassword = (password) => {
const passwordRegex =
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/;
return passwordRegex.test(password);
};
app.post("/register", async (req, res) => {
const { username, password } = req.body;
if (!validateUsername(username)) {
return res.status(400).send("Username did not meet criteria");
}
if (!validatePassword(password)) {
return res.status(400).send("invalid password");
}
const existingUser = await User.findOne({ username });
if (existingUser) {
return res.status(400).send("Username already exists");
}
const hashedPassword = await bcrypt.hash(password, 10);
await User.create({
username,
password: hashedPassword,
});
res.redirect("/login");
});
app.post("/login", async (req, res) => {
const { username, password } = req.body;
// Find the user in the db
const userFound = await User.findOne({
username,
});
if (userFound && (await bcrypt.compare(password, userFound.password))) {
const token = jwt.sign(
{
username: userFound.username,
role: userFound.role,
},
"anykey",
{
expiresIn: "3d",
}
);
res.cookie("token", token, {
maxAge: 3 * 24 * 60 * 60 * 1000,
httpOnly: true,
});
res.redirect("/dashboard");
} else {
res.send("Invalid login credentials");
}
});
app.get("/dashboard", isAuthenticated, (req, res) => {
const username = req.userData ? req.userData.username : null;
if (username) {
res.render("dashboard", { username });
}
res.redirect("/login");
});
app.get("/logout", (req, res) => {
res.clearCookie("token");
res.redirect("/login");
});
app.get("/forgot-password", (req, res) => {
res.render("forgot-password");
});
app.post("/forgot-password", async (req, res) => {
const { username } = req.body;
const user = await User.findOne({ username });
if (!user) {
return res.send("User not found");
}
const resetToken = jwt.sign({ username: user.username }, "reset-secret-key", {
expiresIn: "1h",
});
res.redirect(`/reset-password/${resetToken}`);
});
app.get("/reset-password/:token", (req, res) => {
const { token } = req.params;
res.render("reset-password", { token });
});
app.post("/reset-password/:token", async (req, res) => {
const { token } = req.params;
const { newPassword, confirmPassword } = req.body;
if (newPassword !== confirmPassword) {
return res.status(400).send("Passwords do not match");
}
if (!validatePassword(newPassword)) {
return res.status(400).send("error password did not meet the criteria");
}
try {
const decoded = jwt.verify(token, "reset-secret-key");
const user = await User.findOne({ username: decoded.username });
if (!user) {
return res.send("Invalid reset token");
}
const hashedPassword = await bcrypt.hash(newPassword, 10);
await User.updateOne(
{ username: decoded.username },
{ password: hashedPassword }
);
res.redirect("/login");
} catch (error) {
res.send("Invalid or expired reset token");
}
});
app.listen(port, console.log("The server is running"));