Firebase Authentication Logout Issue on Subdomain Redirection (Main Domain to Subdomain) in Next.js Project

77 views
Skip to first unread message

Thivagar Radhakrishnan

unread,
Sep 30, 2024, 10:28:16 AMSep 30
to Firebase Google Group
The Issue

-I'm working on a website with Firebase Authentication, and I'm encountering an issue with session management between my main domain and subdomain.

Here's the setup:

My Setup:
Main Domain: abcd.com
Subdomain: blog.abcd.com
Authentication: Firebase Authentication
Frontend: Next.js
Backend: Express with Firebase Admin SDK

The problem occurs when a user logs in on the main domain (abcd.com). After the login process, if the user clicks the "Resources" button (which redirects them to the subdomain blog.abc.com), they are automatically logged out on the subdomain. I need to maintain the session across both the main domain and subdomain.

Questions:

Why is the session cookie not being recognized on the subdomain?
How can I ensure that Firebase authentication persists across both the main domain and subdomain?

Any help would be greatly appreciated. Thank you!

jamesd...@google.com

unread,
Oct 2, 2024, 12:25:42 AMOct 2
to Firebase Google Group
Out of box, Firebase Authentication is only going to work on a single domain. A solution for multi-domain authentication could be made from the following components:

1) Forgo client-side authentication (or at least persistence) in favor of server-side https://firebase.google.com/docs/auth/admin/manage-cookies
When you create the cookie you'll be able to set your own policies. Note the client side SDK does not respect these cookies.
2) So from there if you need client-side authenticated context, spin up an API endpoint / Server Action that will use the Firebase Admin SDK to mint a custom token provided the request has a valid session cookie created in step-1 https://firebase.google.com/docs/auth/admin/create-custom-tokens and sign in with that token https://firebase.google.com/docs/auth/web/custom-auth
Reply all
Reply to author
Forward
0 new messages