// Create a a file named helper.js to convert plain password into hashed password as well compare it at the time of login -
//helper.js
import bcrypt from 'bcryptjs';
function hashPassword(password){
const salt = bcrypt.genSaltSync();
return bcrypt.hashSync(password, salt);
}
function comparePassword(raw, hash){
return bcrypt.compareSync(raw, hash)
}
// here above in compare password function first argument is raw pass means plain pass which you have entered and second hash is that already saved hashed password , we will compare it and get user login
export {hashPassword, comparePassword }
// End Helper ======================================================
// now at your IndexModel or User Model import that functions which were created in helper.js as well RegisterSchema also
import RegisterSchemaModel from '../schema/RegisterSchema.js';
import {hashPassword, comparePassword} from '../schema/helper.js';
class IndexModel{
// to Register User with Hashed Password
registerUserModel(userDetails){
return new Promise((resolve,reject)=>{
userDetails.password = hashPassword(userDetails.password);
var obj = new RegisterSchemaModel(userDetails);
obj.save((err,result)=>{
err ? reject(err) : resolve(result) ;
});
});
}
// For User Login with plain password by doing comparison with already saved your hashed pass
fetchUsers(userDetails) {
return new Promise((resolve,reject)=>{
const {email,password,status} = userDetails;
RegisterSchemaModel.find({email,status},(err,result)=>{
const isValid = comparePassword(userDetails.password, result[0].password);
// here above in compare password function first argument is raw pass means plain pass which you have entered and second result[0].pass is that already saved hashed password , we will compare it and get user login
var userDBpass = result[0].password
if(isValid)
resolve(result)
else
reject(err);
//!isValid ? reject(err) : resolve(result);
})
})
}
}
End
==============================================================
// For Those Users who don't follow MVC architecture - or use Single file to code
//create Schema first just like above and import it
const express = require('express'); // use import if you use ES6 Module
const router = express.Router();
const registerSchemaModel = require('../model/user');
// Signup API with hashed password ---------
router.post('/signup', (req, res, next) => {
// Creating empty user object
let newUser = new registerSchemaModel();
newUser.name = req.body.name,
newUser.email = req.body.email,
newUser.password=req.body.password
req.body.password = hashPassword( req.body.password);
// Save object to DB
newUser.save((err, User) => {
if (err) {
return res.status(400).send({
message : "Failed to add user."
});
}
else {
return res.status(201).send({
message : "User registered successfully."
}); }
});
});
// For Login API -------------
router.post('/login', (req, res) => {
// Find user with requested email
registerSchemaModel.findOne({ email : req.body.email }, function(err, user) {
const isValid = comparePassword(req.body.password, user.password);
if(isValid)
return res.status(201).send({
message : "User Logged In",
})
else
return res.status(400).send({
message : "Wrong Password"
});
}
});
});
Comments
Post a Comment