naim-firebase-auth-wrapper
Version:
React components and hooks for Firebase Authentication and Firestore with Mantine UI
100 lines • 4.15 kB
JavaScript
/**
* Environment variable utility to handle Firebase configuration
* Framework-agnostic approach to configuration
*/
/**
* Get a configuration value from various sources
* Priority:
* 1. Window.__FIREBASE_CONFIG__
* 2. Window.__ENV__
* 3. process.env
*/
export function getConfigValue(key) {
console.log(`[Firebase Env] Getting config value for: ${key}`);
// Check if we're in a browser environment
const isBrowser = typeof window !== 'undefined';
console.log(`[Firebase Env] Environment: ${isBrowser ? 'Browser' : 'Server'}`);
if (isBrowser) {
// Try to get from window.__FIREBASE_CONFIG__
if (window.__FIREBASE_CONFIG__ && window.__FIREBASE_CONFIG__[key]) {
console.log(`[Firebase Env] Found ${key} in window.__FIREBASE_CONFIG__`);
return window.__FIREBASE_CONFIG__[key];
}
// Try to get from window.__ENV__
if (window.__ENV__ && window.__ENV__[key]) {
console.log(`[Firebase Env] Found ${key} in window.__ENV__`);
return window.__ENV__[key];
}
// Try to get from process.env (for Next.js)
const envKey = `NEXT_PUBLIC_FIREBASE_${key.toUpperCase()}`;
if (process.env && process.env[envKey]) {
console.log(`[Firebase Env] Found ${key} in process.env.${envKey}`);
return process.env[envKey];
}
}
else {
// Server-side: only use process.env
const envKey = `FIREBASE_${key.toUpperCase()}`;
if (process.env && process.env[envKey]) {
console.log(`[Firebase Env] Found ${key} in process.env.${envKey}`);
return process.env[envKey];
}
}
console.log(`[Firebase Env] Config value for ${key} not found`);
return undefined;
}
/**
* Get Firebase configuration from various sources
*/
export function getFirebaseConfig(providedConfig) {
console.log('[Firebase Env] Getting Firebase configuration');
// If running tests, return a test config
if (process.env.NODE_ENV === 'test') {
console.log('[Firebase Env] Using test configuration');
return {
apiKey: 'test-api-key',
authDomain: 'test-project.firebaseapp.com',
projectId: 'test-project',
storageBucket: 'test-project.appspot.com',
messagingSenderId: '123456789',
appId: '1:123456789:web:abcdef123456789'
};
}
// If config is provided directly, use it
if (providedConfig) {
console.log('[Firebase Env] Using provided configuration');
return providedConfig;
}
// Try to build config from environment variables
console.log('[Firebase Env] Building configuration from environment variables');
const config = {
apiKey: getConfigValue('api_key') || '',
authDomain: getConfigValue('auth_domain') || '',
projectId: getConfigValue('project_id') || '',
storageBucket: getConfigValue('storage_bucket') || '',
messagingSenderId: getConfigValue('messaging_sender_id') || '',
appId: getConfigValue('app_id') || '',
};
// Optional measurementId
const measurementId = getConfigValue('measurement_id');
if (measurementId) {
config.measurementId = measurementId;
}
// Check if we have the minimum required fields
if (!config.apiKey || !config.projectId) {
console.warn('[Firebase Env] Firebase configuration missing required fields (apiKey, projectId)');
console.log('[Firebase Env] Config values:', {
apiKey: config.apiKey ? 'Set' : 'Not set',
authDomain: config.authDomain ? 'Set' : 'Not set',
projectId: config.projectId ? 'Set' : 'Not set',
storageBucket: config.storageBucket ? 'Set' : 'Not set',
messagingSenderId: config.messagingSenderId ? 'Set' : 'Not set',
appId: config.appId ? 'Set' : 'Not set',
measurementId: config.measurementId ? 'Set' : 'Not set'
});
return null;
}
console.log('[Firebase Env] Firebase configuration successfully built');
return config;
}
//# sourceMappingURL=env.js.map