Skip to content

Commit ef5ed86

Browse files
committed
appex-279: delete all store users on uninstall
1 parent 2f951db commit ef5ed86

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

lib/auth.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ export function decodePayload(encodedContext: string) {
8484
return jwt.verify(encodedContext, JWT_KEY);
8585
}
8686

87-
// Removes store and storeUser on uninstall
87+
// Removes store and storeUsers on uninstall
8888
export async function removeDataStore(session: SessionProps) {
8989
await db.deleteStore(session);
90-
await db.deleteUser(session);
90+
await db.deleteStoreUsers(session);
9191
}
9292

9393
// Removes users from app - getSession() for user will fail after user is removed

lib/dbs/firebase.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
import { initializeApp } from 'firebase/app';
2-
import { deleteDoc, doc, getDoc, getFirestore, setDoc, updateDoc } from 'firebase/firestore';
2+
import {
3+
collection,
4+
deleteDoc,
5+
doc,
6+
getDoc,
7+
getDocs,
8+
getFirestore,
9+
query,
10+
setDoc,
11+
updateDoc,
12+
where,
13+
writeBatch,
14+
} from 'firebase/firestore';
315
import { SessionProps, UserData } from '../../types';
416

517
// Firebase config and initialization
@@ -91,6 +103,20 @@ export async function deleteUser({ context, user, sub }: SessionProps) {
91103
await deleteDoc(ref);
92104
}
93105

106+
export async function deleteStoreUsers({ context, sub }: SessionProps) {
107+
const contextString = context ?? sub;
108+
const storeHash = contextString?.split('/')[1] || '';
109+
const storeUsersRef = query(collection(db, 'storeUsers'), where('storeHash', '==', storeHash));
110+
const storeUsers = await getDocs(storeUsersRef);
111+
const batch = writeBatch(db);
112+
113+
storeUsers.docs.forEach(doc => {
114+
batch.delete(doc.ref);
115+
});
116+
117+
await batch.commit();
118+
}
119+
94120
export async function hasStoreUser(storeHash: string, userId: string) {
95121
if (!storeHash || !userId) return false;
96122

@@ -107,7 +133,9 @@ export async function getStoreToken(storeHash: string) {
107133
return storeDoc.data()?.accessToken ?? null;
108134
}
109135

110-
export async function deleteStore({ store_hash: storeHash }: SessionProps) {
136+
export async function deleteStore({ context, sub }: SessionProps) {
137+
const contextString = context ?? sub;
138+
const storeHash = contextString?.split('/')[1] || '';
111139
const ref = doc(db, 'store', storeHash);
112140

113141
await deleteDoc(ref);

lib/dbs/mysql.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ export async function deleteUser({ context, user, sub }: SessionProps) {
7171
await query('DELETE FROM storeUsers WHERE userId = ? AND storeHash = ?', values);
7272
}
7373

74+
export async function deleteStoreUsers({ context, sub }: SessionProps) {
75+
const contextString = context ?? sub;
76+
const storeHash = contextString?.split('/')[1] || '';
77+
await query('DELETE FROM storeUsers WHERE storeHash = ?', storeHash);
78+
}
79+
7480
export async function hasStoreUser(storeHash: string, userId: string) {
7581
if (!storeHash || !userId) return false;
7682

@@ -88,6 +94,8 @@ export async function getStoreToken(storeHash: string) {
8894
return results.length ? results[0].accessToken : null;
8995
}
9096

91-
export async function deleteStore({ store_hash: storeHash }: SessionProps) {
97+
export async function deleteStore({ context, sub }: SessionProps) {
98+
const contextString = context ?? sub;
99+
const storeHash = contextString?.split('/')[1] || '';
92100
await query('DELETE FROM stores WHERE storeHash = ?', storeHash);
93101
}

types/db.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ export interface Db {
2020
getStoreToken(storeId: string): string | null;
2121
deleteStore(session: SessionProps): Promise<void>;
2222
deleteUser(session: SessionProps): Promise<void>;
23+
deleteStoreUsers(session: SessionProps): Promise<void>;
2324
}

0 commit comments

Comments
 (0)