-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthcontroller.js
More file actions
41 lines (34 loc) · 1.14 KB
/
Copy pathauthcontroller.js
File metadata and controls
41 lines (34 loc) · 1.14 KB
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
const jwt = require("jsonwebtoken");
const User = require("../models/User");
const bcrypt = require("bcrypt");
const register = async (req, res) => {
const { username, password } = req.body;
try {
const userId = await User.create(username, password);
res.status(201).json({ message: "User registered successfully", userId });
} catch (error) {
console.error(error);
res.status(500).json({ error: "Failed to register user" });
}
};
const login = async (req, res) => {
const { username, password } = req.body;
try {
const user = await User.findByUsername(username);
if (!user) {
return res.status(404).json({ error: "User not found" });
}
const isMatch = await bcrypt.compare(password, user.password);
if (!isMatch) {
return res.status(401).json({ error: "Invalid password" });
}
const token = jwt.sign({ userId: user.user_id }, process.env.JWT_SECRET, {
expiresIn: "1h",
});
res.status(200).json({ message: "Login successful", token });
} catch (error) {
console.error(error);
res.status(500).json({ error: "Login failed" });
}
};
module.exports = { register, login };