-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.controller.js
More file actions
47 lines (37 loc) · 1.29 KB
/
Copy pathuser.controller.js
File metadata and controls
47 lines (37 loc) · 1.29 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
const Users = require('../models/User')
const login = async (req, res) => {
let { username, password } = req.body;
try {
// Use findOne() to find a single document that matches the criteria
let existingUser = await Users.findOne({ username, password });
if (!existingUser) {
// Send a 401 Unauthorized response
// res.status(401).json({
// success: false,
// error: "Wrong details, please check again"
// });
console.error('Error fetching products:', error.message);
res.status(401).json({success:false, error: 'Wrong details, please check again' });
} else {
// Send a 200 OK response
res.status(200).json({
success: true,
data: {
userId: existingUser.id,
email: existingUser.email,
role: existingUser.role
},
});
}
} catch (err) {
console.error(err);
// Send a 500 Internal Server Error response
res.status(500).json({
success: false,
error: "Error! Something went wrong."
});
}
};
module.exports = {
login
};