Skip to content

Commit

Permalink
fleshed out signup and login handlers.
Browse files Browse the repository at this point in the history
  • Loading branch information
hmalik88 committed Nov 4, 2020
1 parent b2881ef commit 4c904cf
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion controllers/user.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken');
const { validateSignup, validateLogin, validateToken } = require('../validators/userValidations');
const { isEmpty } = require('lodash');
const User = require('../models/User');

Expand All @@ -8,9 +9,29 @@ const generateHash = function(password) {
}

exports.signup = async function(req, res, next) {
const errors = await validateSignup({}, req);
if (!isEmpty(errors)) next(errors);
else {
const newUser = await User.create({
email: req.body.email,
password: generateHash(req.body.password),
firstName: req.body.firstName,
lastName: req.body.lastName,
ethAddress: req.body.ethAddress,
userName: req.body.userName

});
const jwtToken = jwt.sign({id: newUser.id}, process.env.ACCESS_TOKEN_SECRET);
res.status(200).send({ token: jwtToken });
}
}

exports.login = async function(req, res, next) {

const errors = await validateLogin({}, req);
if (!isEmpty(errors)) next(errors);
else {
const user = await models.User.findOne({where: {email: req.body.email}});
const jwtToken = jwt.sign({id: user.id}, process.env.ACCESS_TOKEN_SECRET);
res.status(200).send({ token: jwtToken });
}
}

0 comments on commit 4c904cf

Please sign in to comment.