3

I have a node application that's using passport-local-mongoose for authentication. I have a second node app that's served in an iframe of the first app. I'd like to protect the express routes in the second app with the same session of the first app so that the user only logs in once, but the express routes in the second app are still protected. Is there a way for the two node apps to share a session? Or perhaps there is another approach?

Thank you.

Jas Ahluwalia
  • 183
  • 3
  • 15

1 Answers1

1

The easiest way I can think of would be to simply store sessions in MongoDB. Then your second app's middleware could check the session passed in the request against the same database. The downside of this approach is that you have to implement some of the same authentication logic in 2 places.

Perhaps even better, you could build a 3rd microservice that handles authentication specifically. That would basically wrap your session store and centralize the authentication logic in one place. Both of the other 2 apps would then consume this service. This is an area I'm still learning about personally, and this site has been helpful: https://dejanglozic.com/2014/10/07/sharing-micro-service-authentication-using-nginx-passport-and-redis/.

ccnokes
  • 6,885
  • 5
  • 47
  • 54
  • I'm using express as my middleware, and at the moment i'm ok with having authentication logic the same in both places. Could you perhaps post some sample code? i'm trying right now with connect-mongo – Jas Ahluwalia Jun 13 '16 at 21:15