Skip to content

Commit b8488db

Browse files
committed
add pro membership
1 parent 1779a6f commit b8488db

12 files changed

Lines changed: 163 additions & 108 deletions

File tree

‎apps/codingcatdev/src/lib/client/firebase.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { toastStore } from '@codingcatdev/blackcatui';
22
import { initializeApp, getApps, FirebaseError } from 'firebase/app';
33
import { getAuth, setPersistence, browserSessionPersistence, signInWithEmailAndPassword, signInWithPopup, type AuthProvider, GoogleAuthProvider } from 'firebase/auth';
44
import { getFirestore, collection, doc, addDoc, onSnapshot } from 'firebase/firestore';
5+
import { httpsCallable, getFunctions } from 'firebase/functions';
56

67
import {
78
PUBLIC_FB_API_KEY,
@@ -71,6 +72,7 @@ export const ccdSignInWithPopUp = async (provider: AuthProvider) => {
7172
/* DB */
7273
export const db = getFirestore(app);
7374

75+
/* STRIPE */
7476
export const addSubscription = async (price: string) => {
7577
const userDoc = doc(collection(db, 'stripe-customers'), auth.currentUser?.uid)
7678
const docRef = await addDoc(collection(userDoc, 'checkout_sessions'), {
@@ -91,4 +93,15 @@ export const addSubscription = async (price: string) => {
9193
window.location.assign(url);
9294
}
9395
});
94-
};
96+
};
97+
98+
/* FUNCTIONS */
99+
export const functions = getFunctions(app);
100+
101+
export const openStripePortal = async () => {
102+
const functionRef = httpsCallable(functions, 'ext-firestore-stripe-payments-createPortalLink');
103+
const { data } = await functionRef({
104+
returnUrl: window.location.href,
105+
}) as { data: { url: string } };
106+
window.location.assign(data.url);
107+
}

‎apps/codingcatdev/src/lib/server/firebase.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,9 @@ export const ccdCreateSessionCookie = async (idToken: string) => {
4242

4343
export const ccdValidateSessionCookie = async (session: string) => {
4444
return await auth.verifySessionCookie(session, true);
45+
}
46+
47+
export const validateStripeRole = async (uid: string) => {
48+
const user = await auth.getUser(uid);
49+
return user.customClaims?.['stripeRole']
4550
}

‎apps/codingcatdev/src/routes/(content-single)/course/[slug]/lesson/[lessonSlug]/+layout.svelte‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
55
import type { LayoutData } from './$types';
66
export let data: LayoutData;
7-
console.log(data);
87
</script>
98

109
<!-- App Shell -->

‎apps/codingcatdev/src/routes/(content-single)/course/[slug]/lesson/[lessonSlug]/Lesson.svelte‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
course: Course;
77
content: Lesson;
88
};
9-
console.log(data?.content);
109
</script>
1110

1211
{#if data?.content}
Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
1-
import { ccdValidateSessionCookie } from '$lib/server/firebase';
2-
import { redirect, error } from '@sveltejs/kit';
1+
import { db } from '$lib/server/firebase';
2+
import { redirect } from '@sveltejs/kit';
33
import type { LayoutServerLoad } from './$types';
44

5-
export const load = (async ({ cookies, url }) => {
6-
try {
7-
const ccdsession = cookies.get('session');
5+
export const load = (async ({ url, parent }) => {
6+
const data = await parent();
7+
if (!data?.user?.uid) {
8+
throw redirect(307, `/login?redirectTo=${url.pathname}`);
9+
}
810

9-
if (!ccdsession) {
10-
throw redirect(307, `/login?redirectTo=${url.pathname}`);
11-
}
11+
const snapshot = await db.collection('stripe-products').where('active', '==', true).get();
12+
const products = [];
1213

13-
const decodedClaims = await ccdValidateSessionCookie(ccdsession);
14+
for (const doc of snapshot.docs) {
15+
const priceSnapshot = await doc.ref.collection('prices').where('active', '==', true).get();
1416

15-
if (!decodedClaims) {
16-
throw redirect(307, `/login?redirectTo=${url.pathname}`);
17+
for (const price of priceSnapshot.docs) {
18+
products.push({
19+
id: doc.id,
20+
...doc.data(),
21+
price: price.id
22+
})
1723
}
18-
19-
return {
20-
user: decodedClaims
21-
};
22-
} catch (error) {
23-
cookies.set('session', "", { expires: new Date(0) });
24-
console.error(error);
25-
throw redirect(307, `/login?redirectTo=${url.pathname}`);
2624
}
25+
return {
26+
products
27+
};
28+
2729
}) satisfies LayoutServerLoad;
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<script lang="ts">
2+
export let products: {
3+
price: string;
4+
id: string;
5+
}[];
6+
7+
import { Icon } from '@steeze-ui/svelte-icon';
8+
import { RocketLaunch } from '@steeze-ui/heroicons';
9+
10+
import { modalStore, type ModalSettings } from '@codingcatdev/blackcatui';
11+
12+
import ProModal from './ProModal.svelte';
13+
14+
const showPro = () => {
15+
const d: ModalSettings = {
16+
type: 'component',
17+
component: {
18+
ref: ProModal
19+
},
20+
meta: {
21+
products: products
22+
}
23+
};
24+
modalStore.trigger(d);
25+
};
26+
</script>
27+
28+
<button
29+
class="bcu-card variant-filled-primary w-full max-w-sm p-8 relative hover:shadow-2xl hover:scale-105 hover:cursor-pointer ease-in-out duration-300"
30+
aria-label="open-pro-modal"
31+
on:click={showPro}
32+
>
33+
<div class="absolute top-0 right-0 mt-2 mr-2 md:mt-8 md:mr-8">
34+
<div class="bcu-button variant-filled-surface capitalize">Go Pro</div>
35+
</div>
36+
<h2>Pro</h2>
37+
<ul class="text-left text-md md:text-lg">
38+
<li class="my-2 flex flex-row gap-1 items-center">
39+
<Icon src={RocketLaunch} theme="solid" class="w-8" />
40+
Watch all PRO courses
41+
</li>
42+
<li class="my-2 flex flex-row gap-1 items-center">
43+
<Icon src={RocketLaunch} theme="solid" class="w-8" />
44+
Join PRO office hours
45+
</li>
46+
<li class="my-2 flex flex-row gap-1 items-center">
47+
<Icon src={RocketLaunch} theme="solid" class="w-8" />
48+
Read all PRO tutorials
49+
</li>
50+
<li class="my-2 flex flex-row gap-1 items-center">
51+
<Icon src={RocketLaunch} theme="solid" class="w-8" />
52+
Get PRO source code from GitHub
53+
</li>
54+
</ul>
55+
</button>

apps/codingcatdev/src/routes/(protected)/dashboard/ProModal.svelte renamed to apps/codingcatdev/src/routes/(protected)/ProModal.svelte

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
<script lang="ts">
22
import { Icon } from '@steeze-ui/svelte-icon';
3-
import { ArrowRightCircle, CheckCircle, RocketLaunch } from '@steeze-ui/heroicons';
3+
import {
4+
ArrowRightCircle,
5+
ArrowTopRightOnSquare,
6+
CheckCircle,
7+
RocketLaunch
8+
} from '@steeze-ui/heroicons';
49
510
import KcPrimary from '$lib/components/global/icons/KCPrimary.svelte';
611
import AjPrimary from '$lib/components/global/icons/AJPrimary.svelte';
@@ -102,10 +107,7 @@
102107
</div>
103108

104109
{#if redirecting}
105-
<button
106-
class="bcu-button variant-filled-surface flex self-end gap-2"
107-
on:click|once={() => onSubscribe($modalStore[0].meta?.products)}
108-
>
110+
<button class="bcu-button variant-filled-surface flex self-end gap-2">
109111
Redirecting
110112
<ProgressCircle
111113
stroke={100}
@@ -120,7 +122,7 @@
120122
on:click|once={() => onSubscribe($modalStore[0].meta?.products)}
121123
>
122124
Continue
123-
<Icon src={ArrowRightCircle} theme="solid" class="w-8" />
125+
<Icon src={ArrowTopRightOnSquare} theme="mini" class="w-8" />
124126
</button>
125127
{/if}
126128
</div>
Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,55 @@
11
<script lang="ts">
2+
import type { PageData } from './$types';
3+
export let data: PageData;
4+
5+
import { openStripePortal } from '$lib/client/firebase';
6+
import { ProgressCircle } from '@codingcatdev/blackcatui';
27
import LayoutWrapper from '../../(layout-partials)/LayoutWrapper.svelte';
38
import LogoutButton from '../../login/LogoutButton.svelte';
4-
import type { PageData } from './$types';
9+
import ProButton from '../ProButton.svelte';
510
6-
export let data: PageData;
11+
import { Icon } from '@steeze-ui/svelte-icon';
12+
import { ArrowTopRightOnSquare } from '@steeze-ui/heroicons';
13+
14+
let redirecting = false;
15+
16+
const onOpenMembership = async () => {
17+
redirecting = true;
18+
await openStripePortal();
19+
};
720
</script>
821

922
<LayoutWrapper>
1023
<h1>{data.user?.email}</h1>
1124
<LogoutButton />
25+
26+
{#if data?.user?.stripeRole}
27+
<div class="bcu-card p-8 flex flex-col gap-2 md:gap-8">
28+
<h2>Membership</h2>
29+
30+
Current Membership: {data?.user?.stripeRole}
31+
32+
{#if redirecting}
33+
<button class="bcu-button variant-filled-surface flex gap-2">
34+
Redirecting
35+
<ProgressCircle
36+
stroke={100}
37+
meter="stroke-primary-50"
38+
track="stroke-primary-500/30"
39+
class="w-8"
40+
/>
41+
</button>
42+
{:else}
43+
<button
44+
class="bcu-button variant-filled-surface flex gap-2"
45+
on:click|once={onOpenMembership}
46+
>
47+
Open Membership
48+
<Icon src={ArrowTopRightOnSquare} theme="mini" class="w-8" />
49+
</button>
50+
{/if}
51+
</div>
52+
{:else}
53+
<ProButton products={data.products} />
54+
{/if}
1255
</LayoutWrapper>

‎apps/codingcatdev/src/routes/(protected)/dashboard/+page.server.ts‎

Lines changed: 0 additions & 26 deletions
This file was deleted.

‎apps/codingcatdev/src/routes/(protected)/dashboard/+page.svelte‎

Lines changed: 7 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,22 @@
11
<script lang="ts">
2+
import ProButton from '../ProButton.svelte';
23
import type { PageData } from './$types';
34
export let data: PageData;
4-
import { Icon } from '@steeze-ui/svelte-icon';
5-
import { RocketLaunch } from '@steeze-ui/heroicons';
6-
7-
import { modalStore, type ModalSettings } from '@codingcatdev/blackcatui';
8-
9-
import ProModal from './ProModal.svelte';
10-
11-
const showPro = () => {
12-
const d: ModalSettings = {
13-
type: 'component',
14-
component: {
15-
ref: ProModal
16-
},
17-
meta: {
18-
products: data.products
19-
}
20-
};
21-
modalStore.trigger(d);
22-
};
235
</script>
246

257
<div class="flex flex-col gap-4 p-2">
268
<div class="p-4 sm:p-10">
27-
<section class="">
9+
<section class="flex flex-col gap-2 md:gap-8">
10+
<div class="flex justify-between">
11+
<h1>Dashboard</h1>
12+
<a class="bcu-button variant-filled" href="/account">Account & Settings</a>
13+
</div>
2814
<div class="flex flex-col md:flex-row gap-2">
2915
<div class="flex flex-col w-full gap-2">
3016
<div class="bcu-card variant-filled">Welcome Message</div>
3117
<div class="bcu-card variant-filled">Join Discord for LBT</div>
3218
</div>
33-
<button
34-
class="bcu-card variant-filled-primary w-full max-w-sm p-8 relative hover:shadow-2xl hover:scale-105 hover:cursor-pointer ease-in-out duration-300"
35-
aria-label="open-pro-modal"
36-
on:click={showPro}
37-
>
38-
<div class="absolute top-0 right-0 mt-2 mr-2 md:mt-8 md:mr-8">
39-
<div class="bcu-button variant-filled-surface capitalize">Go Pro</div>
40-
</div>
41-
<h2>Pro</h2>
42-
<ul class="text-left text-md md:text-lg">
43-
<li class="my-2 flex flex-row gap-1 items-center">
44-
<Icon src={RocketLaunch} theme="solid" class="w-8" />
45-
Watch all PRO courses
46-
</li>
47-
<li class="my-2 flex flex-row gap-1 items-center">
48-
<Icon src={RocketLaunch} theme="solid" class="w-8" />
49-
Join PRO office hours
50-
</li>
51-
<li class="my-2 flex flex-row gap-1 items-center">
52-
<Icon src={RocketLaunch} theme="solid" class="w-8" />
53-
Read all PRO tutorials
54-
</li>
55-
<li class="my-2 flex flex-row gap-1 items-center">
56-
<Icon src={RocketLaunch} theme="solid" class="w-8" />
57-
Get PRO source code from GitHub
58-
</li>
59-
</ul>
60-
</button>
19+
<ProButton products={data.products} />
6120
</div>
6221
</section>
6322
</div>

0 commit comments

Comments
 (0)