简体   繁体   中英

Node.js how to share passportjs login sessions with a subdomain in the same app?

So I have one app, and I am using npm subdomain, so regular express routes are faked as subdomain routes.

// use fake subdomain routes
app.use(subdomain({ base : 'localhost', removeWWW : true }));

// this looks like app.localhost:9200/login
router.get('/subdomain/app/login', login.login);

// then this posts to app.localhost:9200/app_login
router.post('/subdomain/app/app_login', passport.authenticate('local-login', {
      successRedirect : '/app_login_success',
      failureRedirect : '/app_login_failure',
      failureFlash : true
}));

When I login via app.localhost:9200 I have to re login on localhost:9200

What are some options and solutions to logging in once and having the session active in both routes?

You can use a JSON Web Token (JWT), which is a fairly standard way of handling local auth. In essence, you will create an encoded token containing the user's credentials (eg ID), that will automatically expire after a certain time. Both of your servers would be able to decode this token. Then in your front end, you just need to set the headers to contain this token when sending to either of your domains. See the JWT NPM module and you can find a myriad of guides implementing it along side Passport.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM