firebase / firebase/firebase-tools

`__FIREBASE_DEFAULTS__` cookie not set in NextJS App Router app

Open
#6,247 4 comments 1 reaction 0 assignees 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:** 12.4.7

**Platform:** macOS

### [REQUIRED] Test case

When using Firebase Auth with a Next.js App Router (now the recommended router over Page Router), can't access the name of the initialized Firebase app as recommended in [the docs](https://firebase.google.com/docs/hosting/frameworks/nextjs#optional_integrate_with_firebase_authentication).

### [REQUIRED] Steps to reproduce

1. `npx create-next-app@latest`
1. when asked _Would you like to use App Router? (recommended)_, answer "Yes"
1. `firebase init hosting`
2. Access `cookies` in a server component:

server component:

```tsx
import { cookies } from "next/headers";

export default async function Home() {
const allCookies = cookies().getAll();

return (


Server component (app/page.tsx)


All cookies on the server


{JSON.stringify(allCookies, null, 2)}


);
}
```
1. Try to auto-init FirebaseApp in a client component:

```tsx
"use client";

import { initializeApp } from "firebase/app";

// Calling with no arguments triggers auto-init
const app = initializeApp();

export default function SignInButton() {

return (
<>

Client component


Cookies available to the client:


{JSON.stringify(document.cookie, null, 2)}


Firebase App name from initializeApp().name


{app.name}


);
}
```

### [REQUIRED] Expected behavior

auto-init works on a deployed app

### [REQUIRED] Actual behavior

auto-init works with `firebase emulators:init`, but doesn't work on a deployed app. Instead, the `__FIREBASE_DEFAULTS__` cookie is not available, and the JS SDK errors with the following error:

> FirebaseError: Firebase: Need to provide options, when not being deployed to hosting via source. (app/no-options).

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.