这是我的PassWare.js的MiddleWare我遇到的问题我无法登录获取没有错误,注册路由工作成功我正在进入正确的凭据,从我的观点来看一切都很棒,但我不是重定向到我试过的任何地方我得到http:500错误并且没有选择查询,其中username ='username'和密码='密码'
//Passport config
require('./config/passport')(passport)
//Passport MiddleWare
app.use(passport.initialize());
app.use(passport.session());
这是我的passport.js文件代码
var LocalStrategy = require('passport-local').Strategy;
var User = require('../models/user');
var bcrypt = require('bcryptjs');
module.exports = function (passport) {
passport.use(new LocalStrategy(function (username, password, done) {
models.User.findOne({
where:{username: username}
}.then(function (err,user) {
if(err) console.log(err);
if (!user) {
return done(null, false, {message: 'No user found!'});
}
bcrypt.compare(password, user.password, function (err, isMatch) {
if (err)
console.log(err);
if (isMatch) {
return done(null, user);
} else {
return done(null, false, {message: 'Wrong password.'});
}
});
})
)
}));
passport.serializeUser(function (user, done) {
done(null, user.id);
});
passport.deserializeUser(function (id, done) {
User.findById(id, function (err, user) {
done(err, user);
});
});
}
这是我在路线中的登录方式
/*
*Post Login
*/
router.post('/login',function(req,res,next){
passport.authenticate('local',{
successRedirect:'/',
failureRedirect:'/admin/pages',
failureFlash:true
})(req,res,next);
})
这是我的user.js
var express=require('express');
var router=express.Router();
var User = require('../models/user');
var models=require('../models');
var passport=require('passport');
var bcrypt=require('bcryptjs');
//Get Register
router.get('/register',function(req,res){
res.render('register',{
title:'Register'
})
});
/*
* Register Post
*/
router.post('/register',function(req,res){
var name=req.body.name;
var email=req.body.email;
var username=req.body.username;
var password=req.body.password;
var password2=req.body.password2;
models.User.findOne({
where:{username:username}
}).then(function(copyuser){
if(copyuser){
console.log('User already exist');
redirect('/users/login');
}
else{
var user={
name: name,
email: email,
username: username,
password: password,
admin: 0
};
// res.json(user);
bcrypt.genSalt(10,function(err,salt){
bcrypt.hash(user.password,salt,function(err,hash){
user.password=hash;
models.User.create(user).then(function(user){
res.json(user);
})
console.log('You are now registered');
res.redirect('/admin/pages')
})
})
}
})
})
/*
* Get Login
*/
router.get('/login',function(req,res){
if(res.locals.user) res.redirect('/');
res.render('login',{
title:'log in'
})
})
/*
*Post Login
*/
router.post('/login',function(req,res,next){
passport.authenticate('local',{
successRedirect:'/',
failureRedirect:'/admin/pages',
failureFlash:true
})(req,res,next);
})
//Exports
module.exports=router;