forked from CodingCatDev/codingcat.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirebase.ts
More file actions
104 lines (83 loc) · 2.7 KB
/
firebase.ts
File metadata and controls
104 lines (83 loc) · 2.7 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { initializeApp, getApps, cert } from 'firebase-admin/app';
import { getAuth } from 'firebase-admin/auth';
import { getFirestore } from 'firebase-admin/firestore';
import { env as publicEnv } from '$env/dynamic/public';
import { env as privateEnv } from '$env/dynamic/private';
import type { UserDoc } from '$lib/types';
export let app = getApps().at(0);
if (
!app &&
publicEnv.PUBLIC_FB_PROJECT_ID &&
privateEnv.PRIVATE_FB_CLIENT_EMAIL &&
privateEnv.PRIVATE_FB_PRIVATE_KEY
) {
app = initializeApp({
credential: cert({
projectId: publicEnv.PUBLIC_FB_PROJECT_ID,
clientEmail: privateEnv.PRIVATE_FB_CLIENT_EMAIL,
privateKey: privateEnv.PRIVATE_FB_PRIVATE_KEY
})
});
}
/* AUTH */
export const ccdCreateSessionCookie = async (idToken: string) => {
// Set session expiration to 5 days.
const expiresIn = 60 * 60 * 24 * 5 * 1000;
const auth = getAuth(app);
const sessionCookie = await auth.createSessionCookie(idToken, { expiresIn });
// Set cookie policy for session cookie.
const options = { maxAge: expiresIn, httpOnly: true, secure: true };
return {
name: 'session',
sessionCookie,
options
};
};
export const ccdValidateSessionCookie = async (session: string) => {
const auth = getAuth(app);
return await auth.verifySessionCookie(session, true);
};
export const validateStripeRole = async (uid: string) => {
const auth = getAuth(app);
const user = await auth.getUser(uid);
return user.customClaims?.['stripeRole'];
};
export const isAdmin = async (uid: string) => {
// Check if user is admin
const db = getFirestore();
const doc = await db.collection('admins').doc(uid).get();
return doc.exists;
};
export const setStripeRole = async (uid: string, remove = false) => {
const auth = getAuth(app);
auth.setCustomUserClaims(uid, { stripeRole: remove ? null : 'admin' });
};
export const getShowDrafts = async (uid?: string) => {
if (!uid) return false;
// Check if user is Pro and wants drafts
const auth = getAuth(app);
const user = await auth.getUser(uid);
if (!user?.customClaims?.['stripeRole']) return false;
const db = getFirestore();
const doc = await db.collection('users').doc(user.uid).get();
const userData = doc.data();
return userData?.pro?.settings?.showDrafts;
};
/* DB */
export const getStripeProducts = async () => {
const products: any = [];
const db = getFirestore();
if (!db) return products;
const snapshot = await db.collection('stripe-products').where('active', '==', true).get();
for (const doc of snapshot.docs) {
const priceSnapshot = await doc.ref.collection('prices').where('active', '==', true).get();
for (const price of priceSnapshot.docs) {
products.push({
id: doc.id,
...doc.data(),
price: price.id
});
}
}
return products;
};