This is user model and I want to use verifyPassword instance function from user model in login controller
const {Sequelize,DataTypes} =require('sequelize')
const Db =require('../config/mysql_connecton');
const bcrypt = require('bcrypt');
const User= Db.define('User', {
firstName:{
type:DataTypes.STRING,
allowNull:false,
},
midName:{
type:DataTypes.STRING,
},
lastName:{
type:DataTypes.STRING,
},
email:{
type:DataTypes.STRING,
allowNull:false,
unique: true,
validate:{
isEmail:true
}
},
contactNo:{
type:DataTypes.STRING,
allowNull:false,
unique: true
},
username:{
type:DataTypes.STRING,
allowNull:false,
unique: true
},
password:{
type:DataTypes.STRING,
allowNull:false,
set(value){
this.setDataValue('password',bcrypt.hashSync(value, 10))
}
}
}, {
timestamps: true,
instanceMethods: {
verifyPassword: function(password) {
return bcrypt.compareSync(password, this.password);
}
}
});
Db.sync()
module.exports = User;
login controller
here I am using verifyPassword instance method to verify password but getting error like shown below.
const User = require('../models/user');
module.exports.login = async (req,res)=>{
try{
//password - confirm password check at client side
const user = await User.findOne({where:{username:req.body.username}});
if(user.verifyPassword(user.password)){
console.log("kk");
}
return res.sendStatus(200);
}catch(err){
console.error(`Error in logging user: ${err}`);
return res.sendStatus(403);
}
}
I am getting the below error
error: TypeError: user.verifyPassword is not a function
Changed the model format... Let me know if this works.