Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(examples/firebase-auth-firestore): Fix imports and throw meaningful errors when missing .env #3352

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@
- pacexy
- pcattori
- penspinner
- penx
- phishy
- plastic041
- princerajroy
Expand Down
39 changes: 32 additions & 7 deletions examples/firebase-auth-firestore/app/server/firebase.server.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,40 @@
import admin from "firebase-admin/app";
import client from "firebase/app";
import {
getApps as getServerApps,
initializeApp as initializeServerApp,
cert as serverCert,
} from "firebase-admin/app";
import {
getApps as getClientApps,
initializeApp as initializeClientApp,
} from "firebase/app";
import { getAuth as getServerAuth } from "firebase-admin/auth";
import { getAuth as getClientAuth } from "firebase/auth";

if (client.getApps().length === 0) {
client.initializeApp(JSON.parse(process.env.CLIENT_CONFIG as string));
if (getClientApps().length === 0) {
if (!process.env.CLIENT_CONFIG) {
throw new Error("Missing CLIENT_CONFIG environment variable");
}
let config;
try {
config = JSON.parse(process.env.CLIENT_CONFIG);
} catch {
throw Error("Invalid CLIENT_CONFIG environment variable");
}
initializeClientApp(config);
}

if (admin.getApps().length === 0) {
admin.initializeApp({
credential: admin.cert(JSON.parse(process.env.SERVICE_ACCOUNT as string)),
if (getServerApps().length === 0) {
if (!process.env.SERVICE_ACCOUNT) {
throw new Error("Missing SERVICE_ACCOUNT environment variable");
}
let config;
try {
config = JSON.parse(process.env.SERVICE_ACCOUNT);
} catch {
throw Error("Invalid SERVICE_ACCOUNT environment variable");
}
initializeServerApp({
credential: serverCert(config),
});
}

Expand Down