Sam*_*Sam 4 node.js passport-local passport.js
我按照passport.js的文档使用passport-local:http : //www.passportjs.org/docs/authorize/
当我将用户发送给/login他们时,他们已通过身份验证,但在该文档中没有任何地方可以找到如何授权我的用户。
我试过这个,但这给了我一个bad request:
router.get('/somepage', passport.authenticate('local'), function(req, res, next) {
});
Run Code Online (Sandbox Code Playgroud)
我正在寻找一次保护我所有页面的方法。我正在使用 Express 4.16 并使用不同的路由文件来拆分我的路由。
山姆
您可以使用middleware一个小技巧在策略之间进行切换
例子:
const allowUrl = ['public', 'nonprivate','home'];
const authenticationMiddleware = (whiteList =[]) => (req, res, next) => {
if(whiteList.find(req.baseUrl)) {
next();
}
if (req.isAuthenticated()) {
return next()
}
res.redirect('/');
}
app = express();
app.use(passort.initialize());
app.use(authenticationMiddleware(allowUrl));
app.use(apiRouter);
app.listen(3000, ()=> console.log('hello internet');
Run Code Online (Sandbox Code Playgroud)