-
Notifications
You must be signed in to change notification settings - Fork 273
Expand file tree
/
Copy pathuser-contoller.js
More file actions
66 lines (56 loc) · 1.84 KB
/
user-contoller.js
File metadata and controls
66 lines (56 loc) · 1.84 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
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
const User = require("../model/User");
const bcrypt = require("bcryptjs");
// ✅ Get All Users
const getAllUser = async (req, res) => {
try {
const users = await User.find();
if (!users || users.length === 0) {
return res.status(404).json({ message: "Users not found" });
}
return res.status(200).json({ users, message: "Users fetched successfully" });
} catch (err) {
console.error(err);
return res.status(500).json({ message: "Server error while fetching users" });
}
};
// ✅ Signup
const signUp = async (req, res) => {
const { name, email, password } = req.body;
try {
const existingUser = await User.findOne({ email });
if (existingUser) {
return res.status(400).json({ message: "User already exists" });
}
const hashedPassword = bcrypt.hashSync(password, 10);
const user = new User({
name,
email,
password: hashedPassword,
blogs: []
});
await user.save();
return res.status(201).json({ user, message: "User registered successfully" });
} catch (e) {
console.error(e);
return res.status(500).json({ message: "Server error while signing up" });
}
};
// ✅ Login
const logIn = async (req, res) => {
const { email, password } = req.body;
try {
const existingUser = await User.findOne({ email });
if (!existingUser) {
return res.status(404).json({ message: "User not found" });
}
const isPasswordCorrect = bcrypt.compareSync(password, existingUser.password);
if (!isPasswordCorrect) {
return res.status(400).json({ message: "Incorrect Password" });
}
return res.status(200).json({ user: existingUser, message: "Login successful" });
} catch (err) {
console.error(err);
return res.status(500).json({ message: "Server error while logging in" });
}
};
module.exports = { getAllUser, signUp, logIn };