andela/team-odd-bn-backend

View on GitHub
src/middlewares/validateCredentials.js

Summary

Maintainability
A
0 mins
Test Coverage
import Response from '../helpers/Response';
import { users } from '../database/models';
import HashPassword from '../helpers/HashPassword';

const validateCredentials = async (req, res, next) => {
  const { email, password } = req.body;
  const result = await users.findOne({ where: { email } });
  let status;

  if (!result) {
    status = 400;
    return Response.errorMessage(req, res, 'Email not found', status);
  }

  const isPasswordMatch = HashPassword.matchingPassword(password, result);

  if (!isPasswordMatch) {
    status = 400;
    return Response.errorMessage(req, res, 'Email and password not match', status);
  }

  req.result = result;
  next();
};

export default validateCredentials;