-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckStatus.ts
41 lines (38 loc) · 1.3 KB
/
checkStatus.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { FirebaseApp } from "firebase/app";
import { getAuth } from "firebase/auth";
import {
collection,
getFirestore,
onSnapshot,
query,
where,
} from "firebase/firestore";
export const getPremiumStatus = async (app: FirebaseApp) => {
const auth = getAuth(app);
const userId = auth.currentUser?.uid;
if (!userId) throw new Error("User not logged in");
const db = getFirestore(app);
const subscriptionsRef = collection(db, "customers", userId, "subscriptions");
const q = query(
subscriptionsRef,
where("status", "in", ["trialing", "active"])
);
return new Promise<boolean>((resolve, reject) => {
const unsubscribe = onSnapshot(
q,
(snapshot) => {
// In this implementation we only expect one active or trialing subscription to exist.
console.log("Subscription snapshot", snapshot.docs.length);
if (snapshot.docs.length === 0) {
console.log("No active or trialing subscriptions found");
resolve(false);
} else {
console.log("Active or trialing subscription found");
resolve(true);
}
unsubscribe();
},
reject
);
});
};