GaborH GaborH - 1 year ago 108
Node.js Question

How to setup express-jwt with angular2 not blocking node_modules?

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?

Answer Source

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));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download