const regex = require('../regex'); const User = require('../../models/user.model') const fieldCheck = async(req,res,next) => { try{ if(!req.body.email){ return res.status(400).send({ error_code : 400, message : 'Email not provided' }) } if(!req.body.password){ return res.status(400).send({ error_code : 400, message : 'Password not provided' }) } if(!regex.emailRegex.test(req.body.email)){ return res.status(400).send({ error_code : 400, message : 'Email format is Incorrect' }) } if(!regex.passRegex.test(req.body.password)){ return res.status(400).send({ error_code : 400, message : 'Password format is Incorrect' }) } next(); }catch(err){ console.log('Error inside auth Middelware fieldCheck',err); return res.status(500).send({ error_code : 500, message : 'Internal Error' }) } } const uniqueEmail = async(req,res,next) => { try{ const user = await User.findOne({email : req.body.email}); if(user){ return res.status(400).send({ error_code : 400, message : 'Email already present' }) } next(); }catch(err){ console.log('Error insdie auth mid email Present',err); return res.status(500).send({ error_code : 500, message : 'Internal Error' }) } } const userCheckEmail = async (req,res,next) => { try{ const user = await User.findOne({email : req.body.email}); if(!user){ return res.status(400).send({ error_code : 400, message : 'User not present' }) } next(); }catch(err){ return res.status(500).send({ error_code : 500, message : 'Error inside userCheckEmail' }) } } module.exports = { fieldCheck, uniqueEmail, userCheckEmail }