Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use two separate passport instances in a single express server file

I have an express app with a user and admin section. Both will run on different ports and need different authentication. I created an app.js where I include passport and create an app as well as adminApp. Then I initialize passport for both as follows:

var passport = require('passport');
var app = express();
var adminapp = express();

adminapp.use(passport.initialize());
adminapp.use(passport.session());

app.use(passport.initialize());
app.use(passport.session());

Now I have routes defined to listen to /auth and handle authentication. Since I am running these on different ports, I have same patterns for routes, and both are listening correctly on their ports. So lets say I have a userSessions.js and adminSessions.js files to handle the authentication routes. These are included as follows:

var userSessions = require('./routes/userSessions');
var adminSessions = require('./routes/adminSessions');

app.use('/auth', userSessions);
adminapp.use('/auth', adminSessions); 

Now if I try to authenticate on the admin app, it still goes to the passport.serializeUser and passport.deSerializeUser methods of userSessions.js.

How can I handle this situation so that admin and user authentication are handled separately? Or am I doing this the wrong way and admin section should be handled in completely different app?

like image 422
Vivek V Dwivedi Avatar asked Oct 12 '25 19:10

Vivek V Dwivedi


1 Answers

Have not tried but you can look for passports

var Passport = require('passport').Passport,
    appPass = new Passport(),
    adminappPass = new Passport();
like image 76
xdeepakv Avatar answered Oct 14 '25 09:10

xdeepakv



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!