-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.js
55 lines (45 loc) · 1.54 KB
/
users.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
const router = require("express").Router();
const User = require("../models/user");
const bcrypt = require('bcrypt');
//user register
router.post("/register",async (req,res)=>{
try{
//generate new password
const salt = await bcrypt.genSalt(10);
const hashpassword = await bcrypt.hash(req.body.password,salt);
//create new user
const newUser = new User({
username:req.body.username,
email:req.body.email,
password:hashpassword,
});
//save user and send response
const user = await newUser.save();
res.status(200).json(user._id);
}catch(err){
res.status(500).json(err);
}
});
//login user
router.post("/login", async (req, res) => {
try {
// Find user
const user = await User.findOne({ username: req.body.username });
// Check if the user exists
if (!user) {
return res.status(400).json("WRONG USERNAME OR PASSWORD");
}
// Validate password
const validPassword = await bcrypt.compare(req.body.password, user.password);
// Check if the password is valid
if (!validPassword) {
return res.status(400).json("WRONG USERNAME OR PASSWORD");
}
// Send response with user information
res.status(200).json({ _id: user._id, username: user.username});
} catch (err) {
res.status(500).json(err);
}
});
//user login
module.exports = router