Skip to content

Commit 9d2c182

Browse files
authored
chore: make blabsy admin auth more robust (#421)
1 parent 3f05abe commit 9d2c182

File tree

2 files changed

+56
-7
lines changed
  • infrastructure/eid-wallet/src-tauri/gen/android/.idea
  • platforms/blabsy-w3ds-auth-api/src

2 files changed

+56
-7
lines changed

infrastructure/eid-wallet/src-tauri/gen/android/.idea/kotlinc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

platforms/blabsy-w3ds-auth-api/src/index.ts

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import cors from "cors";
44
import { config } from "dotenv";
55
import path from "path";
66
import { AuthController } from "./controllers/AuthController";
7-
import { initializeApp, cert, applicationDefault } from "firebase-admin/app";
7+
import { initializeApp, cert, applicationDefault, getApps } from "firebase-admin/app";
8+
import * as fs from "fs";
89
import { Web3Adapter } from "./web3adapter";
910
import { WebhookController, adapter } from "./controllers/WebhookController";
1011

@@ -27,19 +28,67 @@ app.use(express.urlencoded({ limit: "50mb", extended: true }));
2728
const authController = new AuthController();
2829

2930
// Initialize Firebase Admin SDK (only if credentials are available)
31+
let firebaseInitialized = false;
3032
try {
31-
if (process.env.GOOGLE_APPLICATION_CREDENTIALS || process.env.FIREBASE_CREDENTIALS_PATH) {
32-
initializeApp({
33-
credential: applicationDefault(),
34-
});
33+
// Check if already initialized
34+
if (getApps().length > 0) {
35+
firebaseInitialized = true;
36+
console.log("✅ Firebase Admin SDK already initialized");
37+
} else if (process.env.GOOGLE_APPLICATION_CREDENTIALS || process.env.FIREBASE_CREDENTIALS_PATH) {
38+
const credentialsPath = process.env.GOOGLE_APPLICATION_CREDENTIALS || process.env.FIREBASE_CREDENTIALS_PATH;
39+
40+
// Explicitly load credentials from file if path is provided
41+
if (credentialsPath && fs.existsSync(credentialsPath)) {
42+
try {
43+
const serviceAccount = JSON.parse(fs.readFileSync(credentialsPath, "utf8"));
44+
initializeApp({
45+
credential: cert(serviceAccount),
46+
});
47+
firebaseInitialized = true;
48+
console.log("✅ Firebase Admin SDK initialized with service account file");
49+
} catch (fileError: any) {
50+
console.error("❌ Failed to load service account file:", fileError.message);
51+
console.error(" File path:", credentialsPath);
52+
// Fall back to applicationDefault
53+
try {
54+
initializeApp({
55+
credential: applicationDefault(),
56+
});
57+
firebaseInitialized = true;
58+
console.log("✅ Firebase Admin SDK initialized with applicationDefault (fallback)");
59+
} catch (fallbackError: any) {
60+
console.error("❌ Failed to initialize with applicationDefault:", fallbackError.message);
61+
}
62+
}
63+
} else {
64+
// Try applicationDefault (for GCP metadata service or other default locations)
65+
try {
66+
initializeApp({
67+
credential: applicationDefault(),
68+
});
69+
firebaseInitialized = true;
70+
console.log("✅ Firebase Admin SDK initialized with applicationDefault");
71+
} catch (defaultError: any) {
72+
console.error("❌ Failed to initialize with applicationDefault:", defaultError.message);
73+
if (credentialsPath) {
74+
console.error(" Credentials path was set but file not found:", credentialsPath);
75+
}
76+
}
77+
}
3578
} else {
3679
console.warn("⚠️ Firebase credentials not configured. Firebase features will be disabled.");
80+
console.warn(" Set GOOGLE_APPLICATION_CREDENTIALS or FIREBASE_CREDENTIALS_PATH environment variable");
3781
}
3882
} catch (error: any) {
39-
console.warn("⚠️ Failed to initialize Firebase Admin SDK:", error.message);
83+
console.error("❌ Failed to initialize Firebase Admin SDK:", error.message);
84+
console.error(" Stack:", error.stack);
4085
console.warn("⚠️ Firebase features will be disabled.");
4186
}
4287

88+
if (!firebaseInitialized) {
89+
console.warn("⚠️ Firebase Admin SDK not initialized. Webhook and watcher features may fail.");
90+
}
91+
4392
// Initialize Web3Adapter
4493
const web3Adapter = new Web3Adapter();
4594

0 commit comments

Comments
 (0)