-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.js
84 lines (74 loc) · 2.59 KB
/
auth.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
const jwt = require("jsonwebtoken");
const passport = require("passport");
const bcrypt = require("bcrypt");
const { User } = require("./models"); // Adjust this import based on your project structure
require("./passport");
require("dotenv").config();
let generateJWTToken = (user) => {
return jwt.sign(
{ id: user._id, username: user.Username },
process.env.JWT_SECRET,
{
subject: user.Username,
expiresIn: "365d",
algorithm: "HS256",
}
);
};
module.exports = (router) => {
router.post("/login", (req, res) => {
console.log("Login attempt received:", req.body);
if (!req.body.Username || !req.body.Password) {
console.log("Missing username or password in request");
return res.status(400).json({
message: "Both username and password are required",
});
}
User.findOne({ Username: req.body.Username })
.then((user) => {
if (!user) {
console.log("No user found with username:", req.body.Username);
return res.status(401).json({
message: "Invalid username or password",
});
}
console.log("User found:", user.Username);
console.log("Stored hashed password:", user.Password);
console.log("Provided password:", req.body.Password);
bcrypt.compare(req.body.Password, user.Password, (err, isMatch) => {
if (err) {
console.error("bcrypt compare error:", err);
return res.status(500).json({
message: "Error comparing passwords",
error: err.message,
});
}
if (!isMatch) {
console.log("Password does not match for user:", user.Username);
return res.status(401).json({
message: "Invalid username or password",
});
}
console.log("Password matches, generating token for:", user.Username);
let token = generateJWTToken(user);
// Remove sensitive information before sending the user object
const userResponse = {
_id: user._id,
Username: user.Username,
Email: user.Email,
Birthday: user.Birthday,
FavoriteMovies: user.FavoriteMovies,
};
console.log("Login successful, sending response");
return res.json({ user: userResponse, token });
});
})
.catch((error) => {
console.error("Database error during login:", error);
return res.status(500).json({
message: "Error during authentication",
error: error.message,
});
});
});
};