Registration and login authentication in express js

UserController.js


import userModel from "../models/User.js";
import bcrypt from 'bcrypt'

class userController {
  static home = (req, res) => {
    res.render("index");
  };

  static registration = (req, res) => {
    res.render("registration");
  };

  static createUserDoc = async (req, res) => {
    const hashPassword = await bcrypt.hash(req.body.password,10)
    console.log(req.body)
    try {
      const doc = new userModel({
        name: req.body.name,
        email: req.body.email,
        password: hashPassword,
      });

      await doc.save();
      res.redirect("/login");
    } catch (error) {
      console.log(error);
    }
  };

  static login = (req, res) => {
    res.render("login");
  };

 
  static userVerify = async (req,res)=>{
     try{
        const {email,password} = req.body
        //yha email and password ka name form wala hota hai.
        const result = await userModel.findOne({email:email})
        //console.log(result);
       if(result != null){
        const isMatch = await bcrypt.compare(password,result.password)
        if(result.email == email && isMatch ){
            res.send(`<h1> ${result} </h1>`)
        }else{
            res.send(`<h1>Email or password is not match. </h1>`)
        }

       }else{

        res.send(`<h1>You are not registerd User. </h1>`)
       }
     }catch(error){
        console.log(error);
     }
 }



//  static userVerify = async (req,res)=>{
//     try{
//        const {email,password} = req.body
//        const result = await userModel.findOne({email:email})
//        //console.log(result);
//       if(result != null){
//        if(result.email == email && result.password == password ){
//            res.send(`<h1> ${result} </h1>`)
//        }else{
//            res.send(`<h1>Email or password is not match. </h1>`)
//        }

//       }else{

//        res.send(`<h1>You are not registerd User. </h1>`)
//       }
//     }catch(error){
//        console.log(error);
//     }
// }


}


export default userController;

Comments

Popular posts from this blog

My Sql Query ..

Interview question laravel.