How do you setup emulators for apps initialized with initializeServerApp (NextJS 14)?
#2647
Replies: 3 comments
-
|
I have a tendency of spending days looking for a solution by myself, then finding one not long after I ask publicly for help 🙃 The reason this wasn't working was becore The way I solved this was by checking if the emulator is enabled. Each component has their own way of doing so, but here's how I'm doing it for Auth, Firestore and Database: export async function getAuthenticatedAppForUser() {
const idToken = headers().get("Authorization")?.split("Bearer ")[1];
const firebaseServerApp = initializeServerApp(
firebaseConfig,
idToken
? {
authIdToken: idToken,
}
: {}
);
const auth = getAuth(firebaseServerApp);
const firestore = getFirestore(firebaseServerApp);
const db = getDatabase(firebaseServerApp);
if (
process.env.NEXT_PUBLIC_VERCEL_ENV === "development" &&
process.env.NEXT_PUBLIC_USE_EMULATORS === "true"
) {
const emulatorHost = process.env.NEXT_PUBLIC_EMULATOR_HOST || "127.0.0.1";
if (!auth.emulatorConfig) {
connectAuthEmulator(auth, `http://${emulatorHost}:9099`, {
disableWarnings: true,
});
}
if (!(firestore.toJSON() as any).settings.host.includes(emulatorHost)) {
connectFirestoreEmulator(firestore, emulatorHost, 8080);
}
if (!(db as any)._repoInternal.repoInfo_.isUsingEmulator) {
connectDatabaseEmulator(db, emulatorHost, 9000);
}
}
await auth.authStateReady();
return { auth, firestore, db };
}I haven't used this thoroughly yet though, but I wouldn't be surprised if this has some drawbacks... Leaving this post open in case anyone has a better suggestion |
Beta Was this translation helpful? Give feedback.
-
|
I tried to do the same as you but I can't make the emulator to work after user login even tho I double checked all environment variables and emulators ports.. Also, do you know how to check if storage is connected to the emulator? Your examples only have auth, firestore and storage haha
|
Beta Was this translation helpful? Give feedback.
-
|
The root cause is that Fix: guard the import { initializeServerApp } from 'firebase/app';
import { getAuth, connectAuthEmulator } from 'firebase/auth';
import { getFirestore, connectFirestoreEmulator } from 'firebase/firestore';
import { getDatabase, connectDatabaseEmulator } from 'firebase/database';
export async function getAuthenticatedAppForUser(authIdToken?: string) {
const firebaseServerApp = initializeServerApp(
firebaseConfig,
authIdToken ? { authIdToken } : {}
);
const auth = getAuth(firebaseServerApp);
if (process.env.NODE_ENV === 'development') {
// Guard: only connect if not already connected
if (!auth.emulatorConfig) {
connectAuthEmulator(auth, 'http://127.0.0.1:9099', { disableWarnings: true });
}
const db = getFirestore(firebaseServerApp);
// Firestore does not expose emulatorConfig, check the internal host instead
if (!(db as any)._settings?.host?.includes('localhost')) {
connectFirestoreEmulator(db, '127.0.0.1', 8080);
}
const rtdb = getDatabase(firebaseServerApp);
// RTDB: check if emulator host is already set
if (!(rtdb as any).repo_.repoInfo_.isCustomHost) {
connectDatabaseEmulator(rtdb, '127.0.0.1', 9000);
}
}
await auth.authStateReady();
return { firebaseServerApp, currentUser: auth.currentUser };
}Why the guards look different per service:
Tested with Next.js 14 App Router + Firebase 10.x emulator suite. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello, I recently followed the codelab for integrating Firebase with NextJS. After that worked, I started working on setting up emulators. For the client-side part, everything worked well as expected. However, on the server, I can't get emulators to work on an app created with
initializeServerApp. This might be due to how the guide usesgetServerApp, which initializes a new app on each call in order to pass anauthIdTokento it.I was able to make emulators work on the server by moving the initialization outside of the
getServerAppfunction. However, this prevents me from passing theauthIdTokenHas anyone been able to get emulators to work with
initializeServerApp?Beta Was this translation helpful? Give feedback.
All reactions