I am having an issue trying to log using mongoose and bcrypt. I get the error:
TypeError: cb is not a function
My model:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const bcrypt = require('bcrypt');
SALT_WORK_FACTOR = 10;
const userSchema = Schema({
email:{type: String, required: true},
encrypted_password:{type: String},
active:{type: Boolean},
role_id:{ type: Schema.Types.ObjectId, ref: 'roles' ,required: true},
create_date:{type: Date, default: Date.now},
});
userSchema.methods.comparePassword = function comparePassword(candidatePassword, cb) {
bcrypt.compare(candidatePassword, this.password, function (err, isMatch) {
if (err) {
return cb(err);
}
cb(null, isMatch);
});
};
const User = module.exports = mongoose.model('users',userSchema);
My authentication method:
const express = require('express')
, router = express.Router()
const app = express();
const jwt = require('jsonwebtoken');
app.set('superSecret', 'XXXXXXX');
Users = require('./../database/models/people_culture/user');
module.exports = function(app){
app.post('/api/authenticate', (req, res) => {
Users.findOne({email: req.body.email}, function(err, user) {
console.log(user)
if (err) throw err;
if (!user) {
res.json({ success: false, message: 'Authentication failed. User not found.' });
} else if (user) {
password.' });
var ismatch = user.comparePassword(req.body.password);
if(!ismatch){
res.json({ success: false, message: 'Authentication failed. Wrong password.' });
} else {
var token = jwt.sign(user, app.get('superSecret'), {
expiresIn: 1440 // expires in 24 hours
});
res.json({
success: true,
message: 'This is the key',
token: token
});
}
}
});
});
app.use('/api', router);
}
I follow several tutorials and always get the same error. The function to hash the password works fine, but when I try to compare the password can't log in because of this error.
I am using Express, Mongoose, JsonWebToken and bcrypt to authenticate the user.
Thanks in advance
via joselegit
No comments:
Post a Comment