当前,我为更多的宣誓提供者提供了以下代码:
// github
router.get("/:provider", passport.authenticate(:provider));
router.get("/:provider/callback", passport.authenticate(:provider), (req, res) => {
console.log(chalk.blue("went into {:provider} callback"));
res.redirect("http://localhost:3000/profile");
});
是否有一种方法可以将其统一为抽象路由?即像
function callbackDistributer(req, res, next) {
console.log(req.params);
global.provider = req.params.provider;
next();
}
router.get(
"/:provider/callback",
callbackDistributer,
(req, res, next) => {
passport.authenticate(global.provider)(req, res, next);
},
(req, res) => {
console.log(chalk.red("went into: " + global.provider));
res.redirect("http://localhost:3000/profile");
}
);
更新: 以下代码可以满足我的要求。感谢@Usman Abdur Rehman。
Reading package lists... Done
Building dependency tree
Reading state information... Done
Package mana-toolkit is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
E: Package 'mana-toolkit' has no installation candidate
答案 0 :(得分:1)
在通行证之前具有中间件功能。对中间件进行身份验证
function ownMiddleware(req,res,next){
global.provider = req.params.provider
next()
}
,然后在路由处理程序中将其用作
router.get("/:provider/callback", ownMiddleware ,passport.authenticate(global.provider), (req, res) => {
console.log(chalk.blue("went into {:provider} callback"));
res.redirect("http://localhost:3000/profile");
});
我认为它应该工作