Files
prayer-app/src/lib/firebase.ts
T

87 lines
2.6 KiB
TypeScript

// Import the functions you need from the SDKs you need
import { initializeApp } from 'firebase/app';
import { getAuth } from 'firebase/auth';
import { getFirestore } from 'firebase/firestore';
import { getStorage } from 'firebase/storage';
import { getAnalytics } from 'firebase/analytics';
import { getMessaging, onMessage, getToken } from 'firebase/messaging';
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries
// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: "AIzaSyAHtvhdtBxNwf0UE3Fp5P0v1vITZKhLMJw",
authDomain: "prayer-app-6701f.firebaseapp.com",
projectId: "prayer-app-6701f",
storageBucket: "prayer-app-6701f.firebasestorage.app",
messagingSenderId: "555011794707",
appId: "1:555011794707:web:a3d60bdee742578cca1ce6",
measurementId: "G-0C3L52N228"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
// Initialize Firebase Authentication and get a reference to the service
export const auth = getAuth(app);
// Initialize Cloud Firestore and get a reference to the service
export const db = getFirestore(app);
// Initialize Firebase Storage and get a reference to the service
export const storage = getStorage(app);
// Initialize Firebase Cloud Messaging
export let messaging: any = null;
if (typeof window !== 'undefined') {
try {
messaging = getMessaging(app);
} catch (error) {
console.warn('FCM not supported in this environment:', error);
}
}
// Initialize Firebase Analytics and get a reference to the service
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let analytics: any = null;
if (typeof window !== 'undefined') {
import('firebase/analytics').then(({ getAnalytics, isSupported }) => {
isSupported().then((supported) => {
if (supported) {
analytics = getAnalytics(app);
}
});
});
}
export { analytics };
// FCM utility functions
export const requestNotificationPermission = async () => {
if (!messaging) return null;
try {
const permission = await Notification.requestPermission();
if (permission === 'granted') {
const token = await getToken(messaging, {
vapidKey: process.env.NEXT_PUBLIC_FCM_VAPID_KEY || 'your-vapid-key-here'
});
return token;
}
} catch (error) {
console.error('Error requesting notification permission:', error);
}
return null;
};
export const onMessageListener = () =>
new Promise((resolve) => {
if (messaging) {
onMessage(messaging, (payload) => {
resolve(payload);
});
}
});
export { app };