2020-01-17 08:23:18 +00:00
|
|
|
const { validationResult } = require('express-validator');
|
|
|
|
|
|
|
|
const userQueries = require('../data/queries/user');
|
2020-01-15 07:09:01 +00:00
|
|
|
const { hashPassword, compareHash } = require('../services/bcrypt');
|
2020-01-13 22:47:45 +00:00
|
|
|
const signToken = require('../services/signToken');
|
2020-01-15 21:52:01 +00:00
|
|
|
|
|
|
|
const checkValidationErrors = (req, res) => {
|
|
|
|
const errors = validationResult(req);
|
|
|
|
if (!errors.isEmpty()) {
|
|
|
|
return res.status(422).json({ errors: errors.array() });
|
|
|
|
}
|
|
|
|
}
|
2020-01-10 01:44:58 +00:00
|
|
|
|
2020-01-15 07:09:01 +00:00
|
|
|
const signup = async (req, res, next) => {
|
2020-01-15 21:52:01 +00:00
|
|
|
checkValidationErrors(req, res);
|
2020-01-13 22:47:45 +00:00
|
|
|
const user = req.body;
|
|
|
|
try {
|
2020-01-16 04:04:55 +00:00
|
|
|
delete user.confirmPassword;
|
2020-01-22 01:02:54 +00:00
|
|
|
const existingUser = await userQueries.findUserByNameOrEmail(user);
|
2020-01-14 22:22:42 +00:00
|
|
|
const hashedPassword = await hashPassword(user.password);
|
2020-01-17 08:23:18 +00:00
|
|
|
const secureUser = { ...user, password: hashedPassword };
|
|
|
|
if (existingUser.length) {
|
|
|
|
return res.status(409).json({errors: [{auth: 'User already exists!'}]})
|
|
|
|
}
|
2020-01-16 04:04:55 +00:00
|
|
|
|
2020-01-22 01:02:54 +00:00
|
|
|
const newUser = await userQueries.insertUser(secureUser);
|
2020-01-18 05:18:50 +00:00
|
|
|
signToken(res, newUser)
|
|
|
|
res.status(201).json({...newUser});
|
2020-01-13 22:47:45 +00:00
|
|
|
}
|
2020-01-17 08:23:18 +00:00
|
|
|
|
2020-01-13 22:47:45 +00:00
|
|
|
catch (err) {
|
2020-01-16 04:04:55 +00:00
|
|
|
res.status(500).json(err);
|
2020-01-13 22:47:45 +00:00
|
|
|
}
|
2020-01-10 01:44:58 +00:00
|
|
|
}
|
|
|
|
|
2020-01-15 07:09:01 +00:00
|
|
|
const login = async (req, res, next) => {
|
2020-01-15 21:52:01 +00:00
|
|
|
checkValidationErrors(req, res);
|
2020-01-15 07:09:01 +00:00
|
|
|
const user = req.body;
|
2020-01-24 05:25:08 +00:00
|
|
|
|
2020-01-15 07:09:01 +00:00
|
|
|
try {
|
2020-01-17 08:23:18 +00:00
|
|
|
const queryResults = await userQueries.findUserByNameOrEmail(user);
|
2020-01-15 07:09:01 +00:00
|
|
|
const savedUser = queryResults[0] || null;
|
2020-01-24 05:25:08 +00:00
|
|
|
|
2020-01-17 08:23:18 +00:00
|
|
|
if (!savedUser) {
|
2020-01-18 08:18:51 +00:00
|
|
|
return res.status(401).send({errors: 'bad credentials'});
|
2020-01-17 08:23:18 +00:00
|
|
|
}
|
2020-01-15 07:09:01 +00:00
|
|
|
|
|
|
|
const hashedPassword = savedUser.password;
|
|
|
|
const passwordMatch = await compareHash(user.password, hashedPassword);
|
2020-01-17 08:23:18 +00:00
|
|
|
|
|
|
|
if (!passwordMatch) {
|
2020-01-18 08:18:51 +00:00
|
|
|
return res.status(401).send({errors: 'bad credentials'});
|
2020-01-17 08:23:18 +00:00
|
|
|
}
|
2020-01-15 07:09:01 +00:00
|
|
|
|
|
|
|
const authorizedUser = {...savedUser};
|
|
|
|
delete authorizedUser.password;
|
|
|
|
|
|
|
|
signToken(res, authorizedUser);
|
2020-01-18 08:18:51 +00:00
|
|
|
res.send({...authorizedUser}).status(200);
|
2020-01-15 07:09:01 +00:00
|
|
|
}
|
2020-01-17 08:23:18 +00:00
|
|
|
|
2020-01-15 07:09:01 +00:00
|
|
|
catch (err) {
|
2020-01-18 08:18:51 +00:00
|
|
|
res.status(500).send({errors: err});
|
2020-01-15 07:09:01 +00:00
|
|
|
}
|
2020-01-10 01:44:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2020-01-15 07:09:01 +00:00
|
|
|
signup,
|
2020-01-10 01:44:58 +00:00
|
|
|
login
|
|
|
|
}
|