How do you setup emulators for apps initialized with initializeServerApp
(NextJS 14)?
#2647
Replies: 1 comment
-
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.
-
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 anauthIdToken
to it.I was able to make emulators work on the server by moving the initialization outside of the
getServerApp
function. However, this prevents me from passing theauthIdToken
Has anyone been able to get emulators to work with
initializeServerApp
?Beta Was this translation helpful? Give feedback.
All reactions