ERROR TypeError: user.validatePassword is not a fun... | Fórum | School of Net

Deseja poder participar de nosso fórum e tirar todas as suas dúvidas?
Clique aqui e assine nosso plano de acesso ilimitado. Saiba mais.

por Alan

2 meses, 1 semana atrás Alan

ERROR TypeError: user.validatePassword is not a function

Não consegui validar o usuário, no console apresenta o seguinte erro: ERROR TypeError: user.validatePassword is not a function at Strategy._verify (C:\Users\ALGORITMEW\Desktop\nodejs-qa\src\passport.js:11:16) at Strategy.authenticate (C:\Users\ALGORITMEW\Desktop\nodejs-qa\node_modules\passport-local\lib\strategy.js:90:12) at attempt (C:\Users\ALGORITMEW\Desktop\nodejs-qa\node_modules\passport\lib\middleware\authenticate.js:361:16) at authenticate (C:\Users\ALGORITMEW\Desktop\nodejs-qa\node_modules\passport\lib\middleware\authenticate.js:362:7) at Layer.handle [as handle_request] (C:\Users\ALGORITMEW\Desktop\nodejs-qa\node_modules\express\lib\router\layer.js:95:5) at next (C:\Users\ALGORITMEW\Desktop\nodejs-qa\node_modules\express\lib\router\route.js:137:13) at Route.dispatch (C:\Users\ALGORITMEW\Desktop\nodejs-qa\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (C:\Users\ALGORITMEW\Desktop\nodejs-qa\node_modules\express\lib\router\layer.js:95:5) at C:\Users\ALGORITMEW\Desktop\nodejs-qa\node_modules\express\lib\router\index.js:281:22 at Function.process_params (C:\Users\ALGORITMEW\Desktop\nodejs-qa\node_modules\express\lib\router\index.js:335:12) at next (C:\Users\ALGORITMEW\Desktop\nodejs-qa\node_modules\express\lib\router\index.js:275:10) at Function.handle (C:\Users\ALGORITMEW\Desktop\nodejs-qa\node_modules\express\lib\router\index.js:174:3) at router (C:\Users\ALGORITMEW\Desktop\nodejs-qa\node_modules\express\lib\router\index.js:47:12) at Layer.handle [as handle_request] (C:\Users\ALGORITMEW\Desktop\nodejs-qa\node_modules\express\lib\router\layer.js:95:5) at trim_prefix (C:\Users\ALGORITMEW\Desktop\nodejs-qa\node_modules\express\lib\router\index.js:317:13) at C:\Users\ALGORITMEW\Desktop\nodejs-qa\node_modules\express\lib\router\index.js:284:7 at Function.process_params (C:\Users\ALGORITMEW\Desktop\nodejs-qa\node_modules\express\lib\router\index.js:335:12) at next (C:\Users\ALGORITMEW\Desktop\nodejs-qa\node_modules\express\lib\router\index.js:275:10) at C:\Users\ALGORITMEW\Desktop\nodejs-qa\node_modules\express\lib\router\index.js:635:15 at next (C:\Users\ALGORITMEW\Desktop\nodejs-qa\node_modules\express\lib\router\index.js:260:14) at Function.handle (C:\Users\ALGORITMEW\Desktop\nodejs-qa\node_modules\express\lib\router\index.js:174:3) at router (C:\Users\ALGORITMEW\Desktop\nodejs-qa\node_modules\express\lib\router\index.js:47:12) Executing (default): SELECT `id`, `username`, `email`, `password`, `createdAt`, `updatedAt` FROM `Users` AS `User` WHERE `User`.`username` = 'teste'; Meu user model ``` const bcrypt = require('bcrypt-nodejs'); module.exports = (sequelize, DataTypes) => { const User = sequelize.define('User', { username: { type: DataTypes.STRING, allowNull: false, unique: true }, email: { type: DataTypes.STRING, allowNull: false, unique: true, validade: { isEmail: true } }, password: { type: DataTypes.STRING, allowNull: false } }); User.prototype.validatePassword = function(password){ return bcrypt.compareSync(password, this.password); } User.addHook('beforeCreate', function(user){ user.password = bcrypt.hashSync(user.password, bcrypt.genSaltSync(10), null); }); return User; } ``` Meu passport ``` const passport = require('passport'); const LocalStrategy = require('passport-local').Strategy; const User = require('./models/index').User; passport.use(new LocalStrategy({}, async function(username, password, done){ try { const user = User.findOne({ where: {username} }); if(!user){ return done(null, false, {}); } if(!user.validatePassword(password)){ return done(null, false, {}); } return done(null, user); } catch (err) { console.log('ERROR', err); } } )); passport.serializeUser(function(user, done){ done(null, user); }); passport.deserializeUser(function(user, done){ done(null, user); }); module.exports = passport; ```

2 Respostas