Hola estoy intentando hacer que mi aplicación inicie sesión, pero no logro hacer debido a un error, no he podido resolverlo y necesito ayuda.
const { Schema, model } = require('mongoose');
const bcryptjs = require('bcryptjs');
const UserSchema = new Schema({
name : {
type: String,
require: true,
},
email : {
type: String,
require: true,
unique: true
},
password : {
type: String,
require: true
},
}, {
timestamps: true
});
UserSchema.methods.encryptPassword = async (password) =>{
const salt = await bcryptjs.genSalt(10);
return await bcryptjs.hash(password, salt);
};
UserSchema.methods.matchPassword = async function(password){
return await bcryptjs.compare(password, this.password);
};
module.exports = model('users', UserSchema);
este es el esquema que estoy utilizando y utilizo un modulo para encriptar las contraseñas y compararlas al iniciar sesion.
const passport = require('passport');
const localStrategy = require('passport-local').Strategy;
const User = require('../models/user');
passport.use(new localStrategy({
usernameField: 'email',
passwordField: 'password'
}, async (email, password, done) => {
// Match Email´s User
const user = await User.findOne({email: email});
if(!user) {
return done(null, false, {menssage: 'Not User Found'});
}else{
// Match Password's User
const match = await User.matchPassword(password);
if(match){
return done(null, user);
}else{
return done(null, false, {menssage: 'Incorrect Password'});
}
}
}));
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser((id, done) =>{
User.findById(id, (err, user) => {
done(err, user);
});
});
en esta parte estoy llamando a la función macthPassword
pero me manda el siguiente mensaje de error cuando intento iniciar sesión en la aplicación.
(node:8160) UnhandledPromiseRejectionWarning: TypeError: User.matchPassword is not a function
at Strategy._verify (C:\Users\Johan Semprum\Desktop\FAZT NODEJS\src\config\passport.js:16:34)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
(node:8160) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
Sinceramente no se que pueda ser pero necesito ayuda, si alguien me puede ayudar se lo agradecería demasiado, muchas gracias de antemano, quedo atento a cualquier pregunta.