11import { browser } from '$app/environment' ;
22
3- import { initializeApp , getApps , FirebaseError } from 'firebase/app' ;
3+ import { initializeApp , getApps } from 'firebase/app' ;
44import {
55 getAuth ,
6- setPersistence ,
7- browserSessionPersistence ,
86 signInWithEmailAndPassword ,
97 signInWithPopup ,
108 type AuthProvider ,
119 type Auth ,
1210 createUserWithEmailAndPassword
1311} from 'firebase/auth' ;
14- import { getFirestore , collection , doc , addDoc , onSnapshot , Firestore } from 'firebase/firestore' ;
12+ import {
13+ getFirestore ,
14+ collection ,
15+ doc ,
16+ addDoc ,
17+ onSnapshot ,
18+ Firestore ,
19+ setDoc ,
20+ type DocumentData ,
21+ initializeFirestore
22+ } from 'firebase/firestore' ;
1523import { httpsCallable , getFunctions , type Functions } from 'firebase/functions' ;
1624import {
1725 getAnalytics ,
@@ -32,11 +40,11 @@ export const firebaseConfig = {
3240 measurementId : env . PUBLIC_FB_MEASUREMENT_ID
3341} ;
3442
35- let app = getApps ( ) . at ( 0 ) ;
36- let auth : Auth ;
37- let db : Firestore ;
38- let functions : Functions ;
39- let analytics : Analytics ;
43+ export let app = getApps ( ) . at ( 0 ) ;
44+ export let auth : Auth ;
45+ export let firestore : Firestore ;
46+ export let functions : Functions ;
47+ export let analytics : Analytics ;
4048
4149if (
4250 ! app &&
5058 firebaseConfig . measurementId
5159) {
5260 app = initializeApp ( firebaseConfig ) ;
53-
5461 auth = getAuth ( app ) ;
62+
5563 // As httpOnly cookies are to be used, do not persist any state client side.
56- setPersistence ( auth , browserSessionPersistence ) ;
57- db = getFirestore ( app ) ;
64+ // setPersistence(auth, browserSessionPersistence);
65+ firestore = initializeFirestore ( app , { ignoreUndefinedProperties : true } ) ;
5866 functions = getFunctions ( app ) ;
5967 analytics = getAnalytics ( app ) ;
6068} else {
61- console . debug ( 'Skipping Firebase Initialization, check firebaseconfig.' ) ;
69+ if (
70+ browser &&
71+ ( ! firebaseConfig . apiKey ||
72+ ! firebaseConfig . authDomain ||
73+ ! firebaseConfig . projectId ||
74+ ! firebaseConfig . storageBucket ||
75+ ! firebaseConfig . messagingSenderId ||
76+ ! firebaseConfig . appId ||
77+ ! firebaseConfig . measurementId )
78+ )
79+ console . debug ( 'Skipping Firebase Initialization, check firebaseconfig.' ) ;
6280}
6381
6482/* AUTH */
@@ -100,10 +118,13 @@ export const ccdSignInWithPopUp = async (provider: AuthProvider) => {
100118} ;
101119
102120/* DB */
121+ export const updateUser = async ( docRef : string , data : DocumentData ) => {
122+ return setDoc ( doc ( firestore , docRef ) , data , { merge : true } ) ;
123+ } ;
103124
104125/* STRIPE */
105126export const addSubscription = async ( price : string , uid : string ) => {
106- const userDoc = doc ( collection ( db , 'stripe-customers' ) , uid ) ;
127+ const userDoc = doc ( collection ( firestore , 'stripe-customers' ) , uid ) ;
107128 return await addDoc ( collection ( userDoc , 'checkout_sessions' ) , {
108129 price,
109130 success_url : window . location . href ,
0 commit comments