andela/magma-backend

View on GitHub
src/middlewares/userValidations.js

Summary

Maintainability
A
0 mins
Test Coverage
import models from '../database/models';
import Responses from '../utils/Responses';
import Helper from '../utils/Helper';

/**
 * @function
 * @description Check if email is already exists
 * @param {object} req - Resquest object
 * @param {object} res - Response object
 * @param {object} next
 * @returns {object} JSON response
 */
const emailExists = (req, res, next) => {
  let { email } = req.body;
  email = email.trim().toLowerCase();
  models.User.findOne({ where: { email } }).then(data => {
    if (data) {
      Responses.setError(409, 'email already in use');
      return Responses.send(res);
    }
    next();
  }).catch(() => {
    Responses.setError(500, 'database error');
    return Responses.send(res);
  });
};

/**
 * @function
 * @description Check if user email exist, password correct and verified
 * @param {object} req - Resquest object
 * @param {object} res - Response object
 * @param {object} next
 * @returns {object} JSON response
 */
const validateLogin = (req, res, next) => {
  let { email } = req.body;
  const { password } = req.body;
  email = email.trim().toLowerCase();
  models.User.findOne({ where: { email } }).then(response => {
    if (!response) {
      Responses.setError(404, 'Your email cannot be found in our database.');
      return Responses.send(res);
    }
    const correctPassword = Helper.comparePassword(password, response.password);
    if (!correctPassword) {
      Responses.setError(401, 'Your password is incorrect.');
      return Responses.send(res);
    }
    if (response.isVerified === false) {
      Responses.setError(401, 'Your email is not verified.');
      return Responses.send(res);
    }
    next();
  });
};

export default {
  emailExists, validateLogin
};