1
1
import { browser } from '$app/environment' ;
2
2
3
- import { initializeApp , getApps , FirebaseError } from 'firebase/app' ;
3
+ import { initializeApp , getApps } from 'firebase/app' ;
4
4
import {
5
5
getAuth ,
6
- setPersistence ,
7
- browserSessionPersistence ,
8
6
signInWithEmailAndPassword ,
9
7
signInWithPopup ,
10
8
type AuthProvider ,
11
9
type Auth ,
12
10
createUserWithEmailAndPassword
13
11
} 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' ;
15
23
import { httpsCallable , getFunctions , type Functions } from 'firebase/functions' ;
16
24
import {
17
25
getAnalytics ,
@@ -32,11 +40,11 @@ export const firebaseConfig = {
32
40
measurementId : env . PUBLIC_FB_MEASUREMENT_ID
33
41
} ;
34
42
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 ;
40
48
41
49
if (
42
50
! app &&
50
58
firebaseConfig . measurementId
51
59
) {
52
60
app = initializeApp ( firebaseConfig ) ;
53
-
54
61
auth = getAuth ( app ) ;
62
+
55
63
// 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 } ) ;
58
66
functions = getFunctions ( app ) ;
59
67
analytics = getAnalytics ( app ) ;
60
68
} 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.' ) ;
62
80
}
63
81
64
82
/* AUTH */
@@ -100,10 +118,13 @@ export const ccdSignInWithPopUp = async (provider: AuthProvider) => {
100
118
} ;
101
119
102
120
/* DB */
121
+ export const updateUser = async ( docRef : string , data : DocumentData ) => {
122
+ return setDoc ( doc ( firestore , docRef ) , data , { merge : true } ) ;
123
+ } ;
103
124
104
125
/* STRIPE */
105
126
export 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 ) ;
107
128
return await addDoc ( collection ( userDoc , 'checkout_sessions' ) , {
108
129
price,
109
130
success_url : window . location . href ,
0 commit comments