I use firebase for authentication on my website and I want to keep the users auth session active across subdomains.
Unfortunately, firebase uses Local Storage to store the user's session. Which unfortunately is independent to each subdomain.
I already know that you can generate a JWT token using firebase from the server side, but then it doesn't allow the user to log out of the site because the user would still end up logged in other subdomains.
It appears Firebase now has support for cookies built in so you should be able to follow this new guide to use it across subdomains:
https://firebase.google.com/docs/auth/admin/manage-cookies
After having spent much longer then I intended to getting single-sign-in working across subdomains, I wrote up a blog post detailing how to accomplish this.
We have three applications at different domains.
accounts.domain.com
app1.domain.com
app2.domain.com
We have three Firebase Functions
...cloudfunctions.net/users-signin
...cloudfunctions.net/users-checkAuthStatus
...cloudfunctions.net/users-signout
In order to sign in:
accounts.domain.com
app/users-signin
cloud function which verifies the information and, if valid, sets a signed __session
cookie which contains the user's UID and returns a success indication to the client./users-checkAuthStatus
cloud function which looks for the signed __session
cookie, extracts the user UID, and uses the UID and the firebase-admin SDK to mint a custom auth token which it returns to the client.app1.domain.com
, the app first checks to see if the person is already signed in using the firebase javascript SDK.
/users-checkAuthStatus
cloud function which looks for the signed __session
cookie and returns a custom auth token to the client if a valid __session
cookie is found.
Again, this is a high level overview which ignores issues like cross-site-scripting attacks, actually signing out, etc. For more information, check out the blog post.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With