似乎每当我调用 Passport 的ensureAuthenticated
中间件时,它都会导致该passport.deserializeUser
函数被向上调用 6-7 次。我不确定这是我的应用程序通过 Express、Sequelize 的结构还是 Passport 的导入方式。出于这个原因,我将列出一些文件,希望能找到它误入歧途的地方。
这是我如何构建所有内容的方式
application/
auth/
models/
node-modules/
public/
routes/
views/
app.js
我的假设是因为中间件不是单例,和/或因为我的路由设置很奇怪。注意:我按照本指南设置了单例续集方法。
./app.js
// .. imports
app.set('models', require('./models')); // singleton ORM (my assumption)
// .. session stuff
app.use(passport.initialize());
app.use(passport.session());
app.use(require('./auth'));
// .. etc
app.use('/', require('./routes')); // routing style possible issue?
// .. create server
./auth/index.js
module.exports = function () {
var express = require('express')
, passport = require('passport')
, Local = require('passport-local').Strategy
, app = express();
passport.use(new Local(
function(username, password, done) {
app.get('models').User.find({
where: {
username: username,
password: password
}
}).done(function (err, user) {
if (err) {
return done(err);
}
if (!user) {
return done(null, false, { message: 'Invalid login' });
}
return done(null, user);
});
}
));
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
app.get('models').User.find(id).done(function(err, user) {
done(err, user);
});
});
return app;
}();
./auth/middleware.js
exports.check = function(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
res.redirect('/login')
};
exports.is = function(role) {
return function (req, res, next) {
if (req.usertypes[req.user.type] === role) next();
else res.redirect('back');
}
};
./routes/index.js
module.exports = function () {
var express = require('express')
, app = express();
app.get('/', function (req, res) {
if (!req.user) res.redirect('/login');
else res.redirect('/' + req.usertypes[req.user.type]);
});
app.use('/admin', require('./admin'));
app.use('/another1', require('./another1')); // yadda
app.use('/another2', require('./another2')); // yadda
app.use('/login', require('./login'));
app.get('/logout', function(req, res){
req.logout();
res.redirect('/');
});
return app;
}();
最后,. /routes/ admin.js
module.exports = function () {
var express = require('express')
, auth = require('../auth/middleware')
, admin = express();
// auth.check seems to be what's firing the multiple queries:
// Executing: SELECT * FROM `users` WHERE `users`.`id`=1 LIMIT 1;
// 6 times from the looks of it.
admin.get('/', auth.check, auth.is('admin'), function (req, res) {
res.render('admin', {
username: 'req.user.username'
});
});
admin.get('/users.json', auth.check, auth.is('admin'), function (req, res) {
res.contentType('application/json');
admin.get('models').User.findAll().done(function (err, users) {
if (users.length === 0) {
// handle
} else {
res.send(JSON.stringify(users));
}
});
});
admin.post('/adduser', auth.check, auth.is('admin'), function (req, res) {
var post = req.body;
admin.get('models').User.create(post).done(function (err, user) {
if (!err) {
res.send(JSON.stringify({success: true, users: user}));
} else {
res.send(JSON.stringify({success: false, message: err}));
}
});
});
return admin;
}();
我知道这是一些代码,但我觉得它非常简单。任何指导将不胜感激。