我正在尝试使用我在http://code.tutsplus.com/tutorials/authenticating-nodejs-applications-with-passport--cms-21619中找到的教程进行登录工作。我修改了代码以使用Sequelize。我能够验证用户名和密码,但在序列化用户后,它重定向到的页面不会加载。我想知道是否有我遗漏的东西。
我并没有真正收到错误,但是重定向到/ test / home页面仍然处于挂起状态,并且它继续执行发布请求。它似乎没有运行req.isAuthenticated()
Executing (default): SELECT "id", "email", "username", "password" FROM "users" AS "user" WHERE "user"."username" = 'user14' LIMIT 1;
username and password matched
serializing user:
POST /test/login 302 377.738 ms - 64
Executing (default): SELECT "id", "email", "username", "password" FROM "users" AS "user" WHERE "user"."id" = 16;
GET /test/home - - ms - -
Executing (default): SELECT "id", "email", "username", "password" FROM "users" AS "user" WHERE "user"."id" = 16;
POST /test/home - - ms - -
Executing (default): SELECT "id", "email", "username", "password" FROM "users" AS "user" WHERE "user"."id" = 16;
POST /test/home - - ms - -
这是我app.js的片段:
// configuring passport
var passport = require('passport');
var expressSession = require('express-session');
app.use(expressSession({ secret: 'keyboard cat', resave: false, saveUninitialized: false }));
app.use(passport.initialize());
app.use(passport.session());
// initialize passport
var initPassport = require('./passport/init');
initPassport(passport);
// routes
var routes = require('./routes/index');
var loginTest = require('./routes/test')(passport);
var gameRoute = require('./routes/game');
app.use('/', routes);
app.use('/test', loginTest);
app.use('/game', gameRoute);
init.js:
var login = require('./login');
var User = require('../server/models/index').user;
module.exports = function(passport){
passport.serializeUser(function(user, done) {
console.log('serializing user: ');
done(null, user.get('id'));
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
console.log('deserializing user:',user);
done(err, user);
});
});
// setting up Passport Strategies for Login
login(passport);
}
test.js(路由器)
var express = require('express');
var router = express.Router();
var isAuthenticated = function (req, res, next) {
if (req.isAuthenticated())
return next();
res.redirect('/test');
}
module.exports = function(passport){
router.post('/login', passport.authenticate('login', {
successRedirect: '/test/home',
failureRedirect: '/test',
failureFlash : true
}));
/* GET Home Page */
router.get('/home', isAuthenticated, function(req, res){
res.render('home', { user: req.user });
});
return router;
}
login.js
var LocalStrategy = require('passport-local').Strategy;
var User = require('../server/models/index').user;
var bCrypt = require('bcrypt-nodejs');
module.exports = function(passport){
passport.use('login', new LocalStrategy({
passReqToCallback : true
},
function(req, username, password, done) {
// check if user with username exists or not
User.findOne({
where: {
username: username
}
}).then(function(user) {
if (!user){
console.log('User Not Found with username '+username);
return done(null, false, { message: 'Incorrect username.' });
}
if (!isValidPassword(user, password)){
console.log('Invalid Password');
return done(null, false, { message: 'Incorrect password.' });
}
console.log('username and password matched');
return done(null, user);
}
);
})
);
var isValidPassword = function(user, password){
return bCrypt.compareSync(password, user.password);
}
}
答案 0 :(得分:3)
经过一番研究,我意识到造成这个问题的原因是deserializing
。我的console.log没有给我一个错误,因为它停留在User.findById(...)。 Sequelize不会返回回调,而是回复承诺。
而不是将其用于init.js,
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
console.log('deserializing user:',user);
done(err, user);
});
});
请改用此语法
passport.deserializeUser(function(id, done) {
User.findById(id).then(function(user) {
console.log('deserializing user:',user);
done(null, user);
}).catch(function(err) {
if (err) {
throw err;
}
});
});