简体   繁体   中英

passportJS authentication for all routes

I'm using passportJS for protecting API Endpoints in an Express APP.

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.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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