Spaces:
Sleeping
Sleeping
| import 'server-only'; | |
| import admin from 'firebase-admin'; | |
| interface FirebaseAdminConfig { | |
| projectId: string; | |
| clientEmail: string; | |
| privateKey: string; | |
| } | |
| function formatPrivateKey(key: string) { | |
| return key.replace(/\\n/g, '\n'); | |
| } | |
| export function createFirebaseAdminApp(config: FirebaseAdminConfig) { | |
| if (admin.apps.length > 0) { | |
| return admin.app(); | |
| } | |
| return admin.initializeApp({ | |
| credential: admin.credential.cert({ | |
| projectId: config.projectId, | |
| clientEmail: config.clientEmail, | |
| privateKey: formatPrivateKey(config.privateKey), | |
| }), | |
| }); | |
| } | |
| export function getAdminAuth() { | |
| // Only initialize if we have credentials, otherwise return null (or throw) | |
| // This prevents build crashes if env vars are missing | |
| if (!process.env.FIREBASE_PROJECT_ID || !process.env.FIREBASE_CLIENT_EMAIL || !process.env.FIREBASE_PRIVATE_KEY) { | |
| console.warn("Firebase Admin credentials missing. Server-side auth will fail."); | |
| return null; | |
| } | |
| const app = createFirebaseAdminApp({ | |
| projectId: process.env.FIREBASE_PROJECT_ID, | |
| clientEmail: process.env.FIREBASE_CLIENT_EMAIL, | |
| privateKey: process.env.FIREBASE_PRIVATE_KEY, | |
| }); | |
| return app.auth(); | |
| } |