-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.js
69 lines (68 loc) · 2.05 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
const express = require("express");
const router = express.Router();
const mongoose = require("mongoose");
const User = mongoose.model("User");
const bcrypt = require("bcryptjs");
const jwt = require("jsonwebtoken");
const { JWTKEY } = require("../keys");
const requireLogin = require("../middleware/requireLogin");
router.post("/signup", (req, res) => {
const { name, email, password } = req.body;
if (!email || !name) {
return res.status(422).json({ error: "please enter all fields" });
}
User.findOne({ email: email })
.then((userisaved) => {
if (userisaved) {
return res.status(422).json({ error: "user already exists" });
}
bcrypt.hash(password, 12).then((hashedpassword) => {
const user = new User({
email,
password: hashedpassword,
name,
});
user
.save()
.then((user) => {
res.json({ message: "saved succesfully" });
})
.catch((err) => {
console.log(err);
});
});
})
.catch((err) => {
console.log(err);
});
});
router.post("/signin", (req, res) => {
const { email, password } = req.body;
if (!email || !password) {
return res.status(422).json({ error: "please add email or password" });
}
User.findOne({ email: email }).then((savedUser) => {
if (!savedUser) {
return res.status(422).json({ error: "Invalid Email or password" });
}
bcrypt
.compare(password, savedUser.password)
.then((isMatch) => {
if (isMatch) {
// res.json({ message: "successfully signed in" });
const token = jwt.sign({ _id: savedUser._id }, JWTKEY);
const { _id, name, email, followers, following, pic } = savedUser;
res.json({
token,
user: { _id, name, email, followers, following, pic },
});
} else {
return res.status(422).json({ error: "Invalid Email or password" });
}
})
.catch((err) => {
console.log(err);
});
});
});
module.exports = router;