-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfirebaseConfig.js
More file actions
66 lines (58 loc) · 2.55 KB
/
firebaseConfig.js
File metadata and controls
66 lines (58 loc) · 2.55 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// firebaseConfig.js — Single Firebase initialization from env vars
import { initializeApp, getApps } from 'firebase/app';
import { getFirestore } from 'firebase/firestore';
import { Platform } from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
// ---------------------------------------------------------------------------
// Runtime guard: ensure all required EXPO_PUBLIC_FIREBASE_* env vars are set.
// ---------------------------------------------------------------------------
const requiredEnvVars = [
'EXPO_PUBLIC_FIREBASE_API_KEY',
'EXPO_PUBLIC_FIREBASE_AUTH_DOMAIN',
'EXPO_PUBLIC_FIREBASE_PROJECT_ID',
'EXPO_PUBLIC_FIREBASE_STORAGE_BUCKET',
'EXPO_PUBLIC_FIREBASE_MESSAGING_SENDER_ID',
'EXPO_PUBLIC_FIREBASE_APP_ID',
];
const missing = requiredEnvVars.filter((key) => !process.env[key]);
if (missing.length > 0) {
const msg =
`[UBConnect] Missing required environment variables:\n` +
missing.map((k) => ` • ${k}`).join('\n') +
`\n\nCopy .env.example to .env and fill in your Firebase project values.`;
console.error(msg);
throw new Error(msg);
}
// ---------------------------------------------------------------------------
// Firebase config — all values sourced from Expo public env vars
// ---------------------------------------------------------------------------
const firebaseConfig = {
apiKey: process.env.EXPO_PUBLIC_FIREBASE_API_KEY,
authDomain: process.env.EXPO_PUBLIC_FIREBASE_AUTH_DOMAIN,
projectId: process.env.EXPO_PUBLIC_FIREBASE_PROJECT_ID,
storageBucket: process.env.EXPO_PUBLIC_FIREBASE_STORAGE_BUCKET,
messagingSenderId: process.env.EXPO_PUBLIC_FIREBASE_MESSAGING_SENDER_ID,
appId: process.env.EXPO_PUBLIC_FIREBASE_APP_ID,
measurementId: process.env.EXPO_PUBLIC_FIREBASE_MEASUREMENT_ID,
};
import { getAuth, initializeAuth, getReactNativePersistence } from 'firebase/auth';
// ---------------------------------------------------------------------------
// Single initialization — one app, one auth, one db
// ---------------------------------------------------------------------------
const app = getApps().length === 0 ? initializeApp(firebaseConfig) : getApps()[0];
let authInstance;
if (Platform.OS === 'web') {
authInstance = getAuth(app);
} else {
try {
authInstance = initializeAuth(app, {
persistence: getReactNativePersistence(AsyncStorage),
});
} catch (e) {
// If auth is already initialized due to Fast Refresh, fallback
authInstance = getAuth(app);
}
}
export const auth = authInstance;
export const db = getFirestore(app);
export default app;