How to Hash Password in React JS MERN

How to Hash Password in React JS MERN

Install a package –

npm i bcryptjs

import it in auth.controller.js –

import bcryptjs from ‘bcryptjs’

Add marked two line of code for hash the password –

hashpassword

Complete code for auth.controller.js:

import User from ‘../models/user.model.js’

import bcryptjs from ‘bcryptjs’

export const signup=async(req, res)=>{

const {username, email, password}=req.body;

if(!username || !email || !password || username===” || email===” || password===”){

return res.status(400).json({message:”All Fields are Required!”})

}

const hashedPassword=bcryptjs.hashSync(password, 10);

const newUser=new User({

username,

email,

password:hashedPassword

})

try {

await newUser.save();

res.json(“Signup Successfull”);

} catch (error) {

res.status(500).json({message:error.message})

}

}

 


Video Tutorial: