0
\$\begingroup\$

I have several routes, and I need Facebook authentication only in the routes that start with /admin:

app.get('/admin/dashboard', passport.ensureAuthenticated, .....);
app.get('/admin/dashboard/new/email', passport.ensureAuthenticated, ....);
app.get('/admin/dashboard/new/question', passport.ensureAuthenticated, ....);
app.get('/admin/dashboard/new/answer', passport.ensureAuthenticated, ....);
app.get('/admin/dashboard/new/etc', passport.ensureAuthenticated, ....);

If I can't use the app.use, is there anyone to call passport.ensureAuthenticated repetition in routes that start with /admin?

\$\endgroup\$
0
\$\begingroup\$

From Express docs:

Path

// will match paths starting with /abcd
app.use('/abcd', function (req, res, next) {
  next();
})

Path Pattern

// will match paths starting with /abcd and /abd
app.use('/abc?d', function (req, res, next) {
  next();
})

// will match paths starting with /abcd, /abbcd, /abbbbbcd and so on
app.use('/ab+cd', function (req, res, next) {
  next();
})

// will match paths starting with /abcd, /abxcd, /abFOOcd, /abbArcd and so on
app.use('/ab*cd', function (req, res, next) {
  next();
})

// will match paths starting with /ad and /abcd
app.use('/a(bc)?d', function (req, res, next) {
  next();
})

Regular Expression

// will match paths starting with /abc and /xyz
app.use(/\/abc|\/xyz/, function (req, res, next) {
  next();
})

Array

// will match paths starting with /abcd, /xyza, /lmn, and /pqr
app.use(['/abcd', '/xyza', /\/lmn|\/pqr/], function (req, res, next) {
  next();
})
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy