为什么是护照?对每个请求执行serializeUser
Why is passport.serializeUser executed on each request?
我正在使用passport.js + passport-facebook-token来保护我的API与Strongloop的环回框架构建。
为什么passport在反序列化成功后要再次序列化反序列化的用户?还有护照。每次请求都调用身份验证方法!我做错了什么?
这是node的日志:
deserializeUser, id: XXXXXXXXXXXXXXXX
User found.
serializeUser, id: XXXXXXXXXXXXXXXX
GET /api/events?access_token=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 304 182ms
下面是js代码:
passport.use(new FacebookTokenStrategy({
clientID: XXXXXXXXXXXXXXXX,
clientSecret: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX'
},
function(accessToken, refreshToken, profile, done) {
//check user table for anyone with a facebook ID of profile.id
User.findOne({
'facebookId': profile.id
}, function(err, user) {
if (err) {
return done(err);
}
if (user) {
console.log("User found.");
return done(err, user);
} else {
console.log("User not found.");
User.create({
email: profile.emails[0].value,
facebookId: profile.id,
password: 'secret'
}, function(err, user) {
console.log(user.id);
console.log(user.email);
console.log(user.facebookId);
console.log("User created");
return done(err, user);
});
}
});
}));
passport.serializeUser(function(user, done) {
console.log('serializeUser, id: ' + user.facebookId);
done(null, user.facebookId);
});
passport.deserializeUser(function(id, done) {
console.log('deserializeUser, id: ' + id);
User.findOne({
'facebookId': id
}, function(err, user) {
if (!err) {
done(null, user);
} else {
done(err, user);
}
});
});
关于为什么passport.authenticate
在每个请求上被调用的问题,这是因为您将其定义为中间件,可能在任何路由逻辑发生之前。
如果你的应用程序中有私有和公共部分,你可以这样做:
// Define a specific that will handle authentication logic
app.get("/auth", passport.authenticate('facebook-token',function(){...});
// Public sections which do not require authentication
app.get("/public1",...);
app.post("/public2",...);
// Private sections which do require authentication
app.get("/private1", function(req,res,next){
if (req.isAuthenticated()){ // Check if user is authenticated
// do things...
}else{ // Wow, this guy is not authenticated. Kick him out of here !
res.redirect("/auth");
}
});
现在,如果您有多个私有节,您可能会发现为每个私有节做同样的事情有点麻烦。您可以定义一个自定义函数来检查用户是否经过身份验证,如果是,则允许请求继续进行。就像
function isThisGuyAuthenticated(req,res,next){
if (req.isAuthenticated()){
return next(); // Ok this guy is clean, please go on !
}else{
res.redirect("/auth"); // This guy is shady, please authenticate !
}
}
并像这样使用:
app.get("/private1",isThisGuyAuthenticated, doCrazySecretStuff); // doCrazySecretStuff will not be called if the user is not authenticated
app.get("/private2", isThisGuyAuthenticated, getCocaColaRecipe);
app.get("/private3", isThisGuyAuthenticated, flyToMars);
app.get("/public", showInfo); // showInfo will be called whether the user is authenticated or not
现在,如果你的应用程序只有私有部分,你可以避免重复调用isThisGuyAuthenticated
定义为中间件(但不是通过定义passport.authenticate
本身作为中间件!);
// Endpoint that will be hit is the user is redirected to /auth
// BEWARE it needs to be above the middleware, otherwise you'll end up with an infinite redirection loop
app.get("/auth", passport.authenticate('facebook-token',function(){...});
// Middleware that will be called on every request
app.use(isThisGuyAuthenticated);
// You app's endpoints
app.get("/private1", doCrazySecretStuff); // doCrazySecretStuff will not be called if the user is not authenticated
app.get("/private2", getCocaColaRecipe);
app.get("/private3", flyToMars);
明白了吗?
EDIT:我错误地将中间件放在"/auth"端点之前。确保它位于
相关文章:
- 防止双击执行两次jQuery post请求
- 暂停函数执行流程,直到ajax请求完成
- Chrome浏览器“;挂起”;同时在循环中执行AJAX请求
- 如何仅在完成对gap.client.youtube的请求.execute后执行函数
- AJAX 请求不会在 400 个错误查询上执行失败
- 嵌套在 .get 请求中的 jQuery 不会执行
- 通过 ajax 请求执行 ajax 请求
- 如何在执行 Ajax 请求大量数据库更新时实现进度条
- 从 Web Worker 执行 AJAX 请求是否可行
- 在不执行ajax请求的情况下,使用jQuery读取HTTP标头以检查404
- Angular在不触发摘要的情况下执行XHR请求
- Django Ajax请求执行不正确
- 在 php 中为特定的 get 请求执行 javascript
- 跨域请求-执行所有脚本的页面内容
- Javascript/Jquery对象文字版本,对变量请求执行函数
- Ajax请求执行两次
- 为什么是护照?对每个请求执行serializeUser
- 从Ajax请求执行内联脚本
- 对不成功的jquery ajax请求执行一些操作
- Ajax请求执行失败,显示错误消息