Passport 自定义回调如何工作?
How does the Passport custom callback work?
我不明白这个例子
app.get('/login', function(req, res, next) {
passport.authenticate('local', function(err, user, info) {
if (err) { return next(err); }
if (!user) { return res.redirect('/login'); }
req.logIn(user, function(err) {
if (err) { return next(err); }
return res.redirect('/users/' + user.username);
});
})(req, res, next);
});
我在这里看到的是
app.get('path', function(req, res, next) {/*bunch of code*/})(req, res, next)
由于它不是对放置在 (req, res, next) 后面的函数的引用,所以它如何工作?
您的简化示例有点偏差,可能是由于括号不匹配等原因...
如果我减少自定义回调的 "official" 通行证示例,我得到:
app.get('/login', function(req, res, next) {
passport.authenticate('local', function(err, user, info) {
})(req, res, next);
});
所以我的第一个假设是 (req, res, next)
被传递给从 passport.authenticate
返回的符合 express-middleware 的函数。
如果我四处寻找 the authenticate code on GitHub,大约在第 81 行左右(截至撰写本文时),看起来这就是从以下内容开始发生的事情:
return function authenticate(req, res, next) {
/* lots and lots of lines follow */
}
我不明白这个例子
app.get('/login', function(req, res, next) {
passport.authenticate('local', function(err, user, info) {
if (err) { return next(err); }
if (!user) { return res.redirect('/login'); }
req.logIn(user, function(err) {
if (err) { return next(err); }
return res.redirect('/users/' + user.username);
});
})(req, res, next);
});
我在这里看到的是
app.get('path', function(req, res, next) {/*bunch of code*/})(req, res, next)
由于它不是对放置在 (req, res, next) 后面的函数的引用,所以它如何工作?
您的简化示例有点偏差,可能是由于括号不匹配等原因...
如果我减少自定义回调的 "official" 通行证示例,我得到:
app.get('/login', function(req, res, next) {
passport.authenticate('local', function(err, user, info) {
})(req, res, next);
});
所以我的第一个假设是 (req, res, next)
被传递给从 passport.authenticate
返回的符合 express-middleware 的函数。
如果我四处寻找 the authenticate code on GitHub,大约在第 81 行左右(截至撰写本文时),看起来这就是从以下内容开始发生的事情:
return function authenticate(req, res, next) {
/* lots and lots of lines follow */
}