Skip to content

Commit 9781e26

Browse files
committed
block locked lessons
1 parent 311c520 commit 9781e26

1 file changed

Lines changed: 32 additions & 25 deletions

File tree

  • apps/codingcatdev/src/routes/(content-single)/course/[slug]/lesson/[lessonSlug]

‎apps/codingcatdev/src/routes/(content-single)/course/[slug]/lesson/[lessonSlug]/+page.server.ts‎

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,47 @@ export const load = (async ({ params, parent, url }) => {
1717
});
1818
}
1919

20-
if (content.locked && !user?.uid) {
21-
throw redirect(307, `/login?redirectTo=${url.pathname}`);
22-
}
23-
24-
let allowedPast = true;
20+
if (content.locked) {
21+
// Locked but needs to be logged in
22+
if (!user?.uid) {
23+
throw redirect(307, `/login?redirectTo=${url.pathname}`);
24+
}
2525

26-
if (env.VERCEL_ENV !== "production") {
26+
// Check if user is admin
2727
const doc = await db.collection('admins').doc(user.uid).get();
28-
allowedPast = doc.exists;
29-
}
28+
const isAdmin = doc.exists;
3029

31-
if (!user?.stripeRole && allowedPast) {
32-
const snapshot = await db.collection('stripe-products').where('active', '==', true).get();
33-
const products = [];
30+
// User should have stripeRole or be admin
31+
if (user?.stripeRole || isAdmin) {
32+
return {
33+
contentType,
34+
content
35+
};
36+
} else {
37+
const snapshot = await db.collection('stripe-products').where('active', '==', true).get();
38+
const products = [];
3439

35-
for (const doc of snapshot.docs) {
36-
const priceSnapshot = await doc.ref.collection('prices').where('active', '==', true).get();
40+
for (const doc of snapshot.docs) {
41+
const priceSnapshot = await doc.ref.collection('prices').where('active', '==', true).get();
3742

38-
for (const price of priceSnapshot.docs) {
39-
products.push({
40-
id: doc.id,
41-
...doc.data(),
42-
price: price.id
43-
})
43+
for (const price of priceSnapshot.docs) {
44+
products.push({
45+
id: doc.id,
46+
...doc.data(),
47+
price: price.id
48+
})
49+
}
4450
}
45-
}
4651

47-
return {
48-
products,
49-
locked: content?.locked,
50-
title: content?.title
51-
};
52+
return {
53+
products,
54+
locked: content?.locked,
55+
title: content?.title
56+
};
57+
}
5258
}
5359

60+
//Not Locked we just send it back
5461
return {
5562
contentType,
5663
content

0 commit comments

Comments
 (0)