简体   繁体   English

所有路线的passportJS身份验证

[英]passportJS authentication for all routes

I'm using passportJS for protecting API Endpoints in an Express APP.我正在使用 passportJS 来保护 Express APP 中的 API 端点。

The following is working fine.以下工作正常。

app.get("/route1",
passport.authenticate('basic', { session: false }),
    (req, res) => { //something });   

However, I'm adding more routes and don't want to repeat that passport.authenticate for every new route I create.但是,我正在添加更多路线,并且不想为我创建的每条新路线重复该passport.authenticate

Ie IE

 app.get("/route2..N",
    passport.authenticate('basic', { session: false }),
        (req, res) => { //something });

I understand that this is a middleware and that I should be able to do this, but I haven't found any examples.我知道这是一个中间件,我应该能够做到这一点,但我没有找到任何示例。

passport.authenticate just returns a middleware function so: passport.authenticate只返回一个中间件 function 所以:

app.use(passport.authenticate('basic', { session: false });
app.get("/route1", (req, res) => { /* something */ } )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM