无论登录详细信息如何,我总是被重定向到失败的URL,控制台上没有记录任何内容,并且没有显示任何错误。关于为什么会发生这种情况的任何想法?
登录Jade
extends layout
block content
h1 Login
p Please Login below
form(method='post', action='/users/login',enctype='multipart/form-data')
.form-group
label Username
input.form-control(name='username', type='text', placeholder='Enter username')
.form-group
label Password
input.form-control(name='password', type='password', placeholder='Enter Password')
input.btn.btn-default(name='submit', type='submit', value='Login')
user.js的
var express = require('express');
var router = express.Router();
var multer = require('multer');
var uploads = multer({ dest: './uploads' });
var passport = require('passport');
var localStrategy = require('passport-local').Strategy;
var User = require('../models/userModel');
/* GET users listing. */
router.get('/', function(req, res, next) {
res.send('respond with a resource');
});
router.get('/register', function(req, res, next) {
res.render('register',{ title: 'Register'})
});
router.get('/login', function(req, res, next) {
res.render('login',{ title: 'Login'})
});
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.getUserById(id, function(err, user) {
done(err, user);
});
});
passport.use(new localStrategy(
function(username, password, done){
User.getUserByUsername(username, function(err, user){
if(err) throw err;
if(!user){
console.log('Unknown user');
return done(null, false,{message:'Unkown User'});
}
User.comparePassword(password, user.password, function(err, isMatch){
if(err) throw err;
if(isMatch){ return done(null, user)}
else{
console.log('Invalid password');
return done(null, false, {message:'Invalid Password'});
}
});
});
}
));
router.post('/login', passport.authenticate('local',{failureRedirect:'/users/login',failureFlash: true }), function(req, res){
console.log('Auth Sucessful');
req.flash('sucess','Logged in');
res.redirect('/');
});
module.exports = router;
我的UserModel.js它有comparePassword和getUserByUsername。
var mongoose = require('mongoose');
var bcrypt = require('bcrypt');
mongoose.connect('mongodb://localhost/nodeauth');
var db = mongoose.connection;
//Schema
var UserSchema = mongoose.Schema({
username:{
type:String,
index:true
},
password:{
type:String,
bcrypt :true,
required: true
},
email:String,
name:String,
profileImage:{
type:String
}
});
var User = module.exports = mongoose.model('User',UserSchema);
module.exports.comparePassword = function(candidatePassword, hash , callback){
bcrypt.compare(candidatePassword, hash, function(err, ismatch){
if(err) return callback(err);
callback(null, ismatch);
});
}
module.exports.getUserByUsername = function(username, callback){
var query = {username : username};
User.findOne(query, function(err, user) {
callback(err, user);
});
}
module.exports.getUserById = function(id, callback){
User.findById(id,callback);
}
module.exports.createUser = function(newUser,callback){
bcrypt.hash(newUser.password, 10, function(err, hash){
if(err) throw err;
//Set hash password
newUser.password = hash;
newUser.save(callback);
} );
}
答案 0 :(得分:0)
function(username, password, done){
User.getUserByUsername(username, function(err, user){
if(err) throw err;
if(!user){
console.log('Unknown user');
return done(null, false,{message:'Unkown User'});
}
User.comparePassword(password, user.password, function(err, isMatch){
if(err) throw err;
if(isMatch){ return done(null, user)}
else{
console.log('Invalid password');
return done(null, false, {message:'Invalid Password'});
}
});
});
}
这是你的问题所在。自if(err) throw err;
以来
存在并且你没有得到错误,很可能无论发生什么都不被认为是错误,所以它必须是你的逻辑在某一点上失败。
您应该测试,您的密码是否正确返回,或者UserModel中的bcrypt比较是否正常工作。这些是我现在看来至关重要的要点。