forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.ts
More file actions
50 lines (42 loc) · 1.28 KB
/
Copy pathadmin.ts
File metadata and controls
50 lines (42 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import * as admin from 'firebase-admin';
function ensureInitialized() {
if (admin.apps.length) return;
if (!process.env.FIREBASE_PROJECT_ID ||
!process.env.FIREBASE_CLIENT_EMAIL ||
!process.env.FIREBASE_PRIVATE_KEY) {
throw new Error('Missing Firebase Admin SDK configuration environment variables!');
}
const privateKey = process.env.FIREBASE_PRIVATE_KEY.replace(/\\n/g, '\n');
try {
admin.initializeApp({
credential: admin.credential.cert({
projectId: process.env.FIREBASE_PROJECT_ID,
clientEmail: process.env.FIREBASE_CLIENT_EMAIL,
privateKey: privateKey,
}),
});
console.log('Firebase Admin SDK initialized successfully.');
} catch (error: any) {
console.error('Firebase Admin SDK initialization error:', error.stack);
throw error;
}
}
export function getDb() {
ensureInitialized();
return admin.firestore();
}
export function getAdminAuth() {
ensureInitialized();
return admin.auth();
}
export const verifyFirebaseToken = async (token: string) => {
ensureInitialized();
try {
const decodedToken = await admin.auth().verifyIdToken(token);
return decodedToken;
} catch (error) {
console.error('Error verifying Firebase ID token:', error);
return null;
}
};
export default admin;