1
- import { initializeApp } from 'firebase/app' ;
1
+ import { FirebaseApp , initializeApp } from 'firebase/app' ;
2
2
import {
3
3
deleteDoc ,
4
4
doc ,
5
5
getDoc ,
6
6
getFirestore ,
7
7
setDoc ,
8
8
Timestamp ,
9
+ Firestore
9
10
} from 'firebase/firestore' ;
10
11
import { env } from '~/env.mjs' ;
11
12
@@ -26,21 +27,29 @@ export interface AuthProps {
26
27
user : User ;
27
28
}
28
29
29
- const { FIRE_API_KEY , FIRE_DOMAIN , FIRE_PROJECT_ID } = env ;
30
-
31
- const firebaseConfig = {
32
- apiKey : FIRE_API_KEY ,
33
- authDomain : FIRE_DOMAIN ,
34
- projectId : FIRE_PROJECT_ID ,
35
- } ;
36
-
37
30
export interface ClientTokenData {
38
31
token : string ;
39
32
expiresAt : Timestamp ;
40
33
}
41
34
42
- const app = initializeApp ( firebaseConfig ) ;
43
- const db = getFirestore ( app ) ;
35
+ let app : FirebaseApp ;
36
+ let db : Firestore ;
37
+
38
+ function getDb ( ) {
39
+ if ( ! db ) {
40
+ const { FIRE_API_KEY , FIRE_DOMAIN , FIRE_PROJECT_ID } = env ;
41
+
42
+ const firebaseConfig = {
43
+ apiKey : FIRE_API_KEY ,
44
+ authDomain : FIRE_DOMAIN ,
45
+ projectId : FIRE_PROJECT_ID ,
46
+ } ;
47
+
48
+ app = initializeApp ( firebaseConfig ) ;
49
+ db = getFirestore ( app ) ;
50
+ }
51
+ return db ;
52
+ }
44
53
45
54
// Firestore data management functions
46
55
@@ -49,7 +58,7 @@ export async function setUser(user: User) {
49
58
if ( ! user ) return Promise . resolve ( ) ;
50
59
51
60
const { email, id, username } = user ;
52
- const ref = doc ( db , 'users' , String ( id ) ) ;
61
+ const ref = doc ( getDb ( ) , 'users' , String ( id ) ) ;
53
62
const data : UserData = { email } ;
54
63
55
64
if ( username ) {
@@ -70,7 +79,7 @@ export async function setStore(props: AuthProps) {
70
79
if ( ! accessToken || ! scope ) return null ;
71
80
72
81
const storeHash = context ?. split ( '/' ) [ 1 ] || '' ;
73
- const ref = doc ( db , 'store' , storeHash ) ;
82
+ const ref = doc ( getDb ( ) , 'store' , storeHash ) ;
74
83
const data = { accessToken, adminId : id , scope } ;
75
84
76
85
await setDoc ( ref , data ) ;
@@ -88,27 +97,27 @@ export async function setStoreUser(session: AuthProps) {
88
97
89
98
const storeHash = context . split ( '/' ) [ 1 ] ;
90
99
const documentId = `${ userId } _${ storeHash } ` ;
91
- const ref = doc ( db , 'storeUsers' , documentId ) ;
100
+ const ref = doc ( getDb ( ) , 'storeUsers' , documentId ) ;
92
101
93
102
await setDoc ( ref , { storeHash } ) ;
94
103
}
95
104
96
105
export async function deleteUser ( storeHash : string , user : User ) {
97
106
const docId = `${ user . id } _${ storeHash } ` ;
98
- const ref = doc ( db , 'storeUsers' , docId ) ;
107
+ const ref = doc ( getDb ( ) , 'storeUsers' , docId ) ;
99
108
100
109
await deleteDoc ( ref ) ;
101
110
}
102
111
103
112
export async function getStoreToken ( storeHash : string ) : Promise < string | null > {
104
113
if ( ! storeHash ) return null ;
105
- const storeDoc = await getDoc ( doc ( db , 'store' , storeHash ) ) ;
114
+ const storeDoc = await getDoc ( doc ( getDb ( ) , 'store' , storeHash ) ) ;
106
115
107
116
return storeDoc . data ( ) ?. accessToken ;
108
117
}
109
118
110
119
export async function deleteStore ( storeHash : string ) {
111
- const ref = doc ( db , 'store' , storeHash ) ;
120
+ const ref = doc ( getDb ( ) , 'store' , storeHash ) ;
112
121
await deleteDoc ( ref ) ;
113
122
}
114
123
@@ -120,7 +129,7 @@ export async function saveClientToken(clientToken: string): Promise<string> {
120
129
const exchangeToken = crypto . randomUUID ( ) ;
121
130
const expiresAt = Timestamp . fromMillis ( Date . now ( ) + 120 * 1000 ) ; // 2 minutes from now
122
131
123
- const ref = doc ( db , 'exchangeTokens' , exchangeToken ) ;
132
+ const ref = doc ( getDb ( ) , 'exchangeTokens' , exchangeToken ) ;
124
133
const data : Omit < ClientTokenData , 'expiresAt' > & { expiresAt : Timestamp } = {
125
134
token : clientToken ,
126
135
expiresAt
@@ -132,7 +141,7 @@ export async function saveClientToken(clientToken: string): Promise<string> {
132
141
}
133
142
134
143
export async function getClientTokenMaybeAndDelete ( exchangeToken : string ) : Promise < string | false > {
135
- const ref = doc ( db , 'exchangeTokens' , exchangeToken ) ;
144
+ const ref = doc ( getDb ( ) , 'exchangeTokens' , exchangeToken ) ;
136
145
const docSnap = await getDoc ( ref ) ;
137
146
138
147
await deleteDoc ( ref ) ;
0 commit comments