11import { toastStore } from '@codingcatdev/blackcatui' ;
22import { initializeApp , getApps , FirebaseError } from 'firebase/app' ;
3- import { getAuth , setPersistence , browserSessionPersistence , signInWithEmailAndPassword , signInWithPopup , type AuthProvider , GoogleAuthProvider } from 'firebase/auth' ;
4- import { getFirestore , collection , doc , addDoc , onSnapshot } from 'firebase/firestore' ;
5- import { httpsCallable , getFunctions } from 'firebase/functions' ;
3+ import { getAuth , setPersistence , browserSessionPersistence , signInWithEmailAndPassword , signInWithPopup , type AuthProvider , type Auth } from 'firebase/auth' ;
4+ import { getFirestore , collection , doc , addDoc , onSnapshot , Firestore } from 'firebase/firestore' ;
5+ import { httpsCallable , getFunctions , type Functions } from 'firebase/functions' ;
66
7- import {
8- PUBLIC_FB_API_KEY ,
9- PUBLIC_FB_AUTH_DOMAIN ,
10- PUBLIC_FB_PROJECT_ID ,
11- PUBLIC_FB_STORAGE_BUCKET ,
12- PUBLIC_FB_MESSAGE_SENDER_ID ,
13- PUBLIC_FB_APP_ID ,
14- PUBLIC_FB_MEASUREMENT_ID
15- } from '$env/static/public' ;
7+ import { env } from '$env/dynamic/public' ;
168
179const firebaseConfig = {
18- apiKey : PUBLIC_FB_API_KEY ,
19- authDomain : PUBLIC_FB_AUTH_DOMAIN ,
20- projectId : PUBLIC_FB_PROJECT_ID ,
21- storageBucket : PUBLIC_FB_STORAGE_BUCKET ,
22- messagingSenderId : PUBLIC_FB_MESSAGE_SENDER_ID ,
23- appId : PUBLIC_FB_APP_ID ,
24- measurementId : PUBLIC_FB_MEASUREMENT_ID
10+ apiKey : env . PUBLIC_FB_API_KEY ,
11+ authDomain : env . PUBLIC_FB_AUTH_DOMAIN ,
12+ projectId : env . PUBLIC_FB_PROJECT_ID ,
13+ storageBucket : env . PUBLIC_FB_STORAGE_BUCKET ,
14+ messagingSenderId : env . PUBLIC_FB_MESSAGE_SENDER_ID ,
15+ appId : env . PUBLIC_FB_APP_ID ,
16+ measurementId : env . PUBLIC_FB_MEASUREMENT_ID
2517} ;
2618
27- export let app = getApps ( ) . at ( 0 ) ;
28- if ( ! app ) {
19+ let app = getApps ( ) . at ( 0 ) ;
20+ let auth : Auth ;
21+ let db : Firestore ;
22+ let functions : Functions ;
23+
24+ if ( ! app &&
25+ firebaseConfig . apiKey &&
26+ firebaseConfig . authDomain &&
27+ firebaseConfig . projectId &&
28+ firebaseConfig . storageBucket &&
29+ firebaseConfig . messagingSenderId &&
30+ firebaseConfig . appId &&
31+ firebaseConfig . measurementId ) {
32+
2933 app = initializeApp ( firebaseConfig ) ;
34+
35+ auth = getAuth ( app ) ;
36+ // As httpOnly cookies are to be used, do not persist any state client side.
37+ setPersistence ( auth , browserSessionPersistence ) ;
38+ db = getFirestore ( app ) ;
39+ functions = getFunctions ( app ) ;
3040}
3141
3242/* AUTH */
33- export const auth = getAuth ( app ) ;
34- // As httpOnly cookies are to be used, do not persist any state client side.
35- setPersistence ( auth , browserSessionPersistence ) ;
3643
3744const setCookie = ( idToken : string ) => {
3845 document . cookie = '__ccdlogin=' + idToken + ';max-age=3600' ;
@@ -70,7 +77,6 @@ export const ccdSignInWithPopUp = async (provider: AuthProvider) => {
7077}
7178
7279/* DB */
73- export const db = getFirestore ( app ) ;
7480
7581/* STRIPE */
7682export const addSubscription = async ( price : string , uid : string ) => {
@@ -96,8 +102,6 @@ export const addSubscription = async (price: string, uid: string) => {
96102} ;
97103
98104/* FUNCTIONS */
99- export const functions = getFunctions ( app ) ;
100-
101105export const openStripePortal = async ( ) => {
102106 const functionRef = httpsCallable ( functions , 'ext-firestore-stripe-payments-createPortalLink' ) ;
103107 const { data } = await functionRef ( {
0 commit comments