firebase / firebase/firebase-tools

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

Open
#5,528 7 comments 7 reactions 1 assignee Claimed by @jamesdaniels View on GitHub
integration: web frameworks type: bug
Dominant language
TypeScript
Stars
4.5k
Forks
1.3k
Avg merge
1d 12h
Merged PRs (30d)
84

Description

### [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:
```ts
interface Props {
serverTime: number;
locals: string;
}
export const getServerSideProps: GetServerSideProps = 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 (

Firebase Next SSR Auth


Server time: { serverTime }


locals: {locals}


{ auth === undefined/*loading*/ ?

Loading...

:
auth.user === null/*not logged in*/ ? Login :

Auth state: {JSON.stringify(auth, null, 2)}

Logout

}

)
}
```

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`
5. Create a page that uses getServerSideProps
6. Implement Firebase Auth
7. Access the Auth state from `getServerSideProps`
8. 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`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.