TypeError: dbUser.validPassword is not a function
at E:\pocnode\src\controllers\admin\auth\passport.js:39:26
at runCallback (timers.js:705:18)
at tryOnImmediate (timers.js:676:5)
at processImmediate (timers.js:658:5)
From previous event:
at E:\pocnode\src\controllers\admin\auth\passport.js:33:53
at process._tickCallback (internal/process/next_tick.js:61:11)
Below i have mention the code
'use strict';
var bcrypt = require('bcrypt-nodejs');
module.exports = (sequelize, DataTypes) => {
var user = sequelize.define('user', {
email: DataTypes.STRING,
password: DataTypes.STRING,
status: DataTypes.INTEGER
}, {});
user.associate = function(models) {
// associations can be defined here
};
user.prototype.validPassword = function (password) {
return bcrypt.compareSync(password, this.password);
};
I am getting this error in my code please help me to solve this issue.
This model code.
'use strict';
var bcrypt = require('bcrypt-nodejs');
module.exports = (sequelize, DataTypes) => {
var user = sequelize.define('user', {
email: DataTypes.STRING,
password: DataTypes.STRING,
status: DataTypes.INTEGER
}, {});
user.associate = function(models) {
// associations can be defined here
};
user.prototype.validPassword = function (password) {
return bcrypt.compareSync(password, this.password);
};
Passport.js code.
var User = require('../../../models/').user;
const LocalStrategy = require('passport-local').Strategy;
var bcrypt = require('bcrypt-nodejs');
module.exports = function (passport) {
passport.serializeUser(function (user, done) {
done(null, user.id);
});
// used to deserialize the user
passport.deserializeUser(function (id, done) {
User.findById(id).then(function(user) {
return done(null, user);
}).catch(function (err) {
return done(err),null;
});
});
passport.use('local-login', new LocalStrategy({
// by default, local strategy uses username and password, we will override with email
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true // allows us to pass in the req from our route (lets us check if a user is logged in or not)
},
function (req, email, password, done) {
if (email)
// asynchronous
process.nextTick(function () {
User.findAll({where: {email: email,status: 1}}).then( function (user) {
if (!user && user.length==0){
return done(null, false , req.flash('message','All fields are required.'));
}else if (!User.validPassword(password)) {
return done(null, false, req.flash('message','Invalid username or password.'));
}
return done(null, user);
})
.catch(function (error) {
return done(error);
});
});
}));
};
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
TypeError: dbUser.validPassword is not a function
at E:\pocnode\src\controllers\admin\auth\passport.js:39:26
at runCallback (timers.js:705:18)
at tryOnImmediate (timers.js:676:5)
at processImmediate (timers.js:658:5)
From previous event:
at E:\pocnode\src\controllers\admin\auth\passport.js:33:53
at process._tickCallback (internal/process/next_tick.js:61:11)
Below i have mention the code
'use strict';
var bcrypt = require('bcrypt-nodejs');
module.exports = (sequelize, DataTypes) => {
var user = sequelize.define('user', {
email: DataTypes.STRING,
password: DataTypes.STRING,
status: DataTypes.INTEGER
}, {});
user.associate = function(models) {
// associations can be defined here
};
user.prototype.validPassword = function (password) {
return bcrypt.compareSync(password, this.password);
};
/* generateHash(password) {
return bcrypt.hash(password, bcrypt.genSaltSync(8));
},
validPassword(password) {
return bcrypt.compare(password, this.password);
}*/
return user;
};
I am getting this error in my code please help me to solve this issue.
This model code.
'use strict';
var bcrypt = require('bcrypt-nodejs');
module.exports = (sequelize, DataTypes) => {
var user = sequelize.define('user', {
email: DataTypes.STRING,
password: DataTypes.STRING,
status: DataTypes.INTEGER
}, {});
user.associate = function(models) {
// associations can be defined here
};
user.prototype.validPassword = function (password) {
return bcrypt.compareSync(password, this.password);
};
/* generateHash(password) {
return bcrypt.hash(password, bcrypt.genSaltSync(8));
},
validPassword(password) {
return bcrypt.compare(password, this.password);
}*/
return user;
};
Passport.js code.
var User = require('../../../models/').user;
const LocalStrategy = require('passport-local').Strategy;
var bcrypt = require('bcrypt-nodejs');
module.exports = function (passport) {
});
passport.use('local-login', new LocalStrategy({
// by default, local strategy uses username and password, we will override with email
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true // allows us to pass in the req from our route (lets us check if a user is logged in or not)
},
function (req, email, password, done) {
if (email)
// asynchronous
process.nextTick(function () {
User.findAll({where: {email: email,status: 1}}).then( function (user) {
if (!user && user.length==0){
return done(null, false , req.flash('message','All fields are required.'));
}else if (!User.validPassword(password)) {
return done(null, false, req.flash('message','Invalid username or password.'));
}
return done(null, user);
})
.catch(function (error) {
return done(error);
});
});
};