Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple passport-jwt strategy in the same app

How can I created two different passport-jwt using different passwords and use it to authenticate two different roles?

Example:

var passport_admin = require('../../node_modules/passport'); 
var passport_user = require('../../node_modules/passport'); 

require('../auth_layer/admin_jwt_auth')(passport_admin); 
require('../auth_layer/user_jwt_auth')(passport_user); 

app.post('/admin/profile',passport_admin.authenticate('jwt',{session:false}), business_admin.post_profile);

app.post('/user/profile',passport_user.authenticate('jwt',{session:false}), business_admin.post_profile);

When I do the above it does not work(401 when verifying token) because I require two different authentication midleware in my route.

How can I achieve that? or Does it make sense to do it?

Thanks for your help.

like image 605
TEN Avatar asked Sep 30 '16 16:09

TEN


People also ask

Which is better Passport or JWT?

JSON Web Token and Passport can be primarily classified as "User Management and Authentication" tools. JSON Web Token and Passport are both open source tools. It seems that Passport with 15.9K GitHub stars and 936 forks on GitHub has more adoption than JSON Web Token with 2.59K GitHub stars and 259 GitHub forks.

What is Passport JWT strategy?

A Passport strategy for authenticating with a JSON Web Token. This module lets you authenticate endpoints using a JSON web token. It is intended to be used to secure RESTful endpoints without sessions.

What is the difference between Passport local and Passport JWT?

passport-local is the strategy you would use if you are authenticating against a username and password stored 'locally' i.e. in the database of your app - 'local' means local to your application server, not local to the end user. passport-jwt is the strategy for using JSON Web Tokens.


1 Answers

I had exactly the same issue with you but after a number of research, trials and errors, I found my own method in solving this problem and wanted to share it with you. First on your syntax below, only one rule will be implemented:

    var passport_admin = require('../../node_modules/passport'); 
    var passport_user = require('../../node_modules/passport'); 

The rule which will be used is only the latest which is passport_user. To tackle this, you need to go to your passport.js api and create two passport rules with different names (in the same js file) like below

    passport.use('admin-rule',
    new JwtStrategy(opts, (...........) => {.........
    }));

    passport.use('user-rule',
    new JwtStrategy(opts, (...........) => {.........
    }));

Then you want to use the 'admin-rule' on your admin syntax, same idea with user syntax (use 'user-rule').

    app.post('/admin/profile',passport_admin.authenticate('admin-rule'
    {session:false}), business_admin.post_profile);

That way your admin & user will use the specified passport rule on it's router.

like image 119
Agus Marsono Avatar answered Oct 11 '22 14:10

Agus Marsono