ken*_*dds 2 authentication routes node.js express passport.js
我正在使用NodeJS,Express和PassportJS来构建Web应用程序.我遇到了一条我无法理解的路线问题.当我有:
...
app.get('/auth/facebook', passport.authenticate('facebook'));
...
Run Code Online (Sandbox Code Playgroud)
一切似乎都很好.但当我改变为:
...
app.get('/auth/facebook',
function(req, res) {
passport.authenticate('facebook');
});
...
Run Code Online (Sandbox Code Playgroud)
它挂了吗?我在app.get函数上遗漏了什么吗?我希望能够这样做,因为我想让路径更加动态,我确定了护照的身份验证.例如:
...
app.get('/auth/:provider',
function(req, res) {
passport.authenticate(req.params.provider);
});
...
Run Code Online (Sandbox Code Playgroud)
凡提供者可能是facebook
,twitter
或google
...
passport.authenticate是中间件,请从源头上看看:https://github.com/jaredhanson/passport/blob/master/lib/passport/middleware/authenticate.js
passport.authenticate('facebook')返回一个函数,该函数接收来自express的req,res,next参数并处理它们.
所以:
app.get('/auth/:provider',
function(req, res, next) {
passport.authenticate(req.params.provider)(req, res, next);
});
Run Code Online (Sandbox Code Playgroud)
是你需要的.
归档时间: |
|
查看次数: |
1086 次 |
最近记录: |