Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling parameterised routes in express-jwt using unless

Given the following route:

router.get('/api/members/confirm/:id, function (req, res, next)

how do I specify the route to be excluded? I have tried:

app.use('/api', expressJwt({ secret: config.secret}).unless({path: ['/api/members/confirm']}));

and

app.use('/api', expressJwt({ secret: config.secret}).unless({path: ['/api/members/confirm/:id']}));

but neither path in the unless array seem to work?

like image 548
Samuel Goldenbaum Avatar asked May 31 '15 15:05

Samuel Goldenbaum


2 Answers

The express-jwt module is using express-unless to give you this unless method, which doesn't accept express' :param path arguments syntax.

But it does accept a regex, so you could do this:

app.use('/api', expressJwt({ secret: config.secret}).unless({path: [/^\/api\/members\/confirm\/.*/]}));

If you don't like that, you can also give unless a function:

var myFilter = function(req) {return true;}
app.use('/api', expressJwt({ secret: config.secret}).unless(myFilter));
like image 93
Andrew Lavers Avatar answered Nov 20 '22 15:11

Andrew Lavers


You must be using regex to achieve what you are looking for, and you can also add in another field to specify what methods are allowed like so:

expressJwt({ secret, isRevoked }).unless({
    path: [
        { url: /^\/api\/members\/confirm\/.*/, methods: ['GET', 'PUT'] }
    ]
})
like image 32
Tudor B. Avatar answered Nov 20 '22 15:11

Tudor B.