firebase / firebase-tools

The Firebase Command Line Tools
MIT License
4.01k stars 925 forks source link

Can't access `currentUser` and `firebaseApp` from `res.locals` on `getServerSideProps` #5528

Open erickborquez opened 1 year ago

erickborquez commented 1 year ago

[REQUIRED] Environment info

firebase-tools: 11.23.1

Platform: macOS

[REQUIRED] Test case

Create a project using NextJs and Firebase Auth. Create a page that accesses the auth state from a getServerSideProps doing res.locals.currentUser.

NextJs Code:

interface Props {
  serverTime: number;
  locals: string;
}
export const getServerSideProps: GetServerSideProps<Props> = async ({ req, res }) => {
  const locals = JSON.stringify((res as any).locals);
  return { props: { serverTime: Date.now(), locals } };
};

export default function Home({ serverTime, locals }:Props) {
  const auth = useAuth();

  return (
    <main>
      <h1>Firebase Next SSR Auth</h1>
      <p>Server time: { serverTime }</p>
      <p>locals: {locals}</p>
      { auth === undefined/*loading*/ ? <p>Loading...</p> : 
        auth.user === null/*not logged in*/ ? <button onClick={signInWithGoogleRedirect}>Login</button> :
        <div>
          <pre>Auth state: {JSON.stringify(auth, null, 2)}</pre>
          <button onClick={logout}>Logout</button>
        </div>
      }
    </main>
  )
}

This is what the pages return. Notice that locals don't have the currentUser value.

Screenshot 2023-02-17 at 0 35 16

Example repo: https://github.com/ErickJoestar/Firebase-Next-SSR-Auth/tree/auth-ssr

[REQUIRED] Steps to reproduce

  1. Initialize a project with NextJs
  2. Enable web frameworks firebase experiments:enable webframeworks
  3. Init hosting firebase init hosting
  4. Create a page that uses getServerSideProps
  5. Implement Firebase Auth
  6. Access the Auth state from getServerSideProps
  7. Deploy the app

[REQUIRED] Expected behavior

The current user should be accessible from res.locals.currentUser on getServerSideProps

[REQUIRED] Actual behavior

There is no currentUser

TheIronDev commented 1 year ago

Hi @ErickJoestar , several QQs:

Thank you! Tyler

erickborquez commented 1 year ago

I don't see any error in my logs

image

The example was a deployed environment, but the same thing happens in an emulated environment. Here I have a live example: https://testing-ca405.web.app/ This is the source code: https://github.com/ErickJoestar/Firebase-Next-SSR-Auth/tree/auth-ssr

LMK if you need more info, thanks!

nsaicharan commented 1 year ago

I too have the same issue.

iamalvisng commented 1 year ago

@ErickJoestar I have the same issue too, did you manage to resolve it?

erickborquez commented 1 year ago

Unfortunately I haven't found a solution

jugglerjer commented 1 year ago

Hey folks, I'm having what I think is a similar issue.

I cloned @erickborquez's auth-ssr branch and ran firebase experiments:enable webframeworks and firebase emulators:start.

Not only is currrentUser undefined, the entire locals object is undefined. I appear to be signed-in on the client side, but that information is not available server-side. The screenshot below was taken after a successful sign in with Google.

Screenshot 2023-07-31 at 1 06 04 PM

The same thing is happening on an actual project of mine, which is how I found myself on this issue.

Node version: 18.17.0 Firebase tools version: 12.4.6

urielEntityMed commented 10 months ago

I'm experiencing the same problem. I attempted to work around it by using cookies and found that Firebase functions incoming requests usually remove all cookies except for the "__session" cookie. Could this be a relevant factor?