0

I am making a nodejs, angular2 app. I would like to setup a JWT authentication, I am using express-jwt, and jsonwebtoken, all my routes, and application works as expected, as long as I haven't setup the express-jwt middleware like that.

app.use(expressJWT({secret: 'hey-hey'}).unless({path: ['/api/login', '/api/signup', '/home', '/signup', '/login', '/']}));

I get the following error:

GET http://localhost:3001/node_modules/core-js/client/shim.min.js 
login:13 GET http://localhost:3001/node_modules/zone.js/dist/zone.js 
login:14 GET http://localhost:3001/node_modules/reflect-metadata/Reflect.js 
login:15 GET http://localhost:3001/node_modules/systemjs/dist/system.src.js 
login:17 GET http://localhost:3001/systemjs.config.js 

Basically, my app has no acces to these routes only with authentication. I also tried to include these routes in the unless:path, which didn't help.

How could I make this routes accessible, using the jwt?

GaborH
  • 689
  • 2
  • 11
  • 24

2 Answers2

0

I added the following to the unless path: /^\/node_modules\/.*/ , /^\/app\/.*/

It is a regular expression, which takes all files from node_modules, and the app directory.

GaborH
  • 689
  • 2
  • 11
  • 24
0

use one of the following

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

or you can also give unless a function:

var myFilter = function(req) {return true;}
app.use('/api', expressJwt({ secret: config.secret}).unless(myFilter));
Vinay Pandya
  • 3,020
  • 2
  • 26
  • 42
  • Which comes from here: http://stackoverflow.com/questions/30559158/handling-parameterised-routes-in-express-jwt-using-unless – GaborH Nov 07 '16 at 11:38