Skip to content

Commit d5d210b

Browse files
added /login
1 parent 67c2f1d commit d5d210b

File tree

7 files changed

+232
-5
lines changed

7 files changed

+232
-5
lines changed

.env.example

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
DB_HOST=
1+
DB_HOST=
2+
SECRET_KEY=

controllers/users/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const signup = require('./signup');
2-
2+
const login = require('./login');
33

44
module.exports = {
5-
signup
6-
}
5+
signup,
6+
login,
7+
};

controllers/users/login.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const bcrypt = require('bcrypt');
2+
const jwt = require('jsonwebtoken');
3+
const { User } = require('../../models');
4+
const { createError } = require('../../utils');
5+
const { joiLoginSchema } = require('../../models/user');
6+
const { SECRET_KEY } = process.env;
7+
8+
const login = async (req, res) => {
9+
const { error } = joiLoginSchema.validate(req.body);
10+
if (error) throw createError(400, error.message);
11+
12+
const { email, password } = req.body;
13+
const user = await User.findOne({ email });
14+
15+
const comparePassword = bcrypt.compareSync(password, user.password);
16+
if (!user || !comparePassword) {
17+
throw createError(401, 'Email or password is wrong');
18+
}
19+
20+
const payload = {
21+
id: user._id,
22+
};
23+
24+
const token = jwt.sign(payload, SECRET_KEY, { expiresIn: '1h' });
25+
26+
res.json({
27+
status: 'success',
28+
code: 200,
29+
data: {
30+
token,
31+
},
32+
});
33+
};
34+
35+
module.exports = login;

controllers/users/signup.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const signup = async (req, res) => {
1010
const { email, password, subscription } = req.body;
1111

1212
const user = await User.findOne({ email });
13-
if (user) throw createError(409, `Email in use`);
13+
if (user) throw createError(409, `Email: '${email}' in use`);
1414

1515
const hashPassword = bcrypt.hashSync(password, bcrypt.genSaltSync(10));
1616
const result = await User.create({

package-lock.json

Lines changed: 188 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"dotenv": "^16.0.3",
1616
"express": "4.17.1",
1717
"joi": "^17.6.4",
18+
"jsonwebtoken": "^8.5.1",
1819
"mongoose": "^6.7.1",
1920
"morgan": "1.10.0",
2021
"uuid": "^9.0.0"

routes/api/users.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ const { validation, ctrlWrapper } = require('../../middlewares');
55
const { joiSignupSchema, joiLoginSchema } = require('../../models/user');
66

77
router.post('/signup', validation(joiSignupSchema), ctrlWrapper(ctrl.signup));
8+
router.post('/login', validation(joiLoginSchema), ctrlWrapper(ctrl.login));
89

910
module.exports = router;

0 commit comments

Comments
 (0)