1
- import firebase from 'firebase/app' ;
2
- import 'firebase/firestore' ;
1
+ import { initializeApp } from 'firebase/app' ;
2
+ import { deleteDoc , doc , getDoc , getFirestore , setDoc , updateDoc } from 'firebase/firestore' ;
3
3
import { SessionProps , UserData } from '../../types' ;
4
4
5
5
// Firebase config and initialization
6
6
// Prod applications might use config file
7
7
const { FIRE_API_KEY , FIRE_DOMAIN , FIRE_PROJECT_ID } = process . env ;
8
-
9
8
const firebaseConfig = {
10
9
apiKey : FIRE_API_KEY ,
11
10
authDomain : FIRE_DOMAIN ,
12
11
projectId : FIRE_PROJECT_ID ,
13
12
} ;
14
-
15
- if ( ! firebase . apps . length ) {
16
- // Initialize Firebase
17
- firebase . initializeApp ( firebaseConfig ) ;
18
- } else {
19
- firebase . app ( ) ;
20
- }
21
-
22
- const db = firebase . firestore ( ) ;
13
+ const app = initializeApp ( firebaseConfig ) ;
14
+ const db = getFirestore ( app ) ;
23
15
24
16
// Firestore data management functions
25
17
@@ -28,14 +20,14 @@ export async function setUser({ user }: SessionProps) {
28
20
if ( ! user ) return null ;
29
21
30
22
const { email, id, username } = user ;
31
- const ref = db . collection ( 'users' ) . doc ( String ( id ) ) ;
23
+ const ref = doc ( db , 'users' , String ( id ) ) ;
32
24
const data : UserData = { email } ;
33
25
34
26
if ( username ) {
35
27
data . username = username ;
36
28
}
37
29
38
- await ref . set ( data , { merge : true } ) ;
30
+ await setDoc ( ref , data , { merge : true } ) ;
39
31
}
40
32
41
33
export async function setStore ( session : SessionProps ) {
@@ -49,10 +41,10 @@ export async function setStore(session: SessionProps) {
49
41
if ( ! accessToken || ! scope ) return null ;
50
42
51
43
const storeHash = context ?. split ( '/' ) [ 1 ] || '' ;
52
- const ref = db . collection ( 'store' ) . doc ( storeHash ) ;
44
+ const ref = doc ( db , 'store' , storeHash ) ;
53
45
const data = { accessToken, adminId : id , scope } ;
54
46
55
- await ref . set ( data ) ;
47
+ await setDoc ( ref , data ) ;
56
48
}
57
49
58
50
// User management for multi-user apps
@@ -69,24 +61,23 @@ export async function setStoreUser(session: SessionProps) {
69
61
70
62
const contextString = context ?? sub ;
71
63
const storeHash = contextString ?. split ( '/' ) [ 1 ] || '' ;
72
- const collection = db . collection ( 'storeUsers' ) ;
73
64
const documentId = `${ userId } _${ storeHash } ` ; // users can belong to multiple stores
74
- const ref = collection . doc ( documentId ) ;
75
- const storeUser = await ref . get ( ) ;
65
+ const ref = doc ( db , 'storeUsers' , documentId ) ;
66
+ const storeUser = await getDoc ( ref ) ;
76
67
77
68
// Set admin (store owner) if installing/ updating the app
78
69
// https://developer.bigcommerce.com/api-docs/apps/guide/users
79
70
if ( accessToken ) {
80
71
// Create a new admin user if none exists
81
- if ( ! storeUser ? .exists ) {
82
- await ref . set ( { storeHash, isAdmin : true } ) ;
72
+ if ( ! storeUser . exists ( ) ) {
73
+ await setDoc ( ref , { storeHash, isAdmin : true } ) ;
83
74
} else if ( ! storeUser . data ( ) ?. isAdmin ) {
84
- await ref . update ( { isAdmin : true } ) ;
75
+ await updateDoc ( ref , { isAdmin : true } ) ;
85
76
}
86
77
} else {
87
78
// Create a new user if it doesn't exist
88
- if ( ! storeUser ? .exists ) {
89
- await ref . set ( { storeHash, isAdmin : owner . id === userId } ) ; // isAdmin true if owner == user
79
+ if ( ! storeUser . exists ( ) ) {
80
+ await setDoc ( ref , { storeHash, isAdmin : owner . id === userId } ) ; // isAdmin true if owner == user
90
81
}
91
82
}
92
83
}
@@ -95,29 +86,29 @@ export async function deleteUser({ context, user, sub }: SessionProps) {
95
86
const contextString = context ?? sub ;
96
87
const storeHash = contextString ?. split ( '/' ) [ 1 ] || '' ;
97
88
const docId = `${ user ?. id } _${ storeHash } ` ;
98
- const storeUsersRef = db . collection ( 'storeUsers' ) . doc ( docId ) ;
89
+ const ref = doc ( db , 'storeUsers' , docId ) ;
99
90
100
- await storeUsersRef . delete ( ) ;
91
+ await deleteDoc ( ref ) ;
101
92
}
102
93
103
94
export async function hasStoreUser ( storeHash : string , userId : string ) {
104
95
if ( ! storeHash || ! userId ) return false ;
105
96
106
97
const docId = `${ userId } _${ storeHash } ` ;
107
- const userDoc = await db . collection ( 'storeUsers' ) . doc ( docId ) . get ( ) ;
98
+ const userDoc = await getDoc ( doc ( db , 'storeUsers' , docId ) ) ;
108
99
109
- return userDoc ? .exists ;
100
+ return userDoc . exists ( ) ;
110
101
}
111
102
112
103
export async function getStoreToken ( storeHash : string ) {
113
104
if ( ! storeHash ) return null ;
114
- const storeDoc = await db . collection ( 'store' ) . doc ( storeHash ) . get ( ) ;
105
+ const storeDoc = await getDoc ( doc ( db , 'store' , storeHash ) ) ;
115
106
116
- return storeDoc ?. exists ? storeDoc . data ( ) ?. accessToken : null ;
107
+ return storeDoc . data ( ) ?. accessToken ?? null ;
117
108
}
118
109
119
110
export async function deleteStore ( { store_hash : storeHash } : SessionProps ) {
120
- const ref = db . collection ( 'store' ) . doc ( storeHash ) ;
111
+ const ref = doc ( db , 'store' , storeHash ) ;
121
112
122
- await ref . delete ( ) ;
113
+ await deleteDoc ( ref ) ;
123
114
}
0 commit comments