sveltekit-pocketbase-starter
Advanced tools
Comparing version 0.1.4 to 0.1.5
{ | ||
"name": "sveltekit-pocketbase-starter", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"author": "Dominick Caponi <d.caponi1@gmail.com>", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
import { redirect } from '@sveltejs/kit'; | ||
import { decodeJwt } from '$lib/jwt'; | ||
import Stripe from 'stripe'; | ||
import type { LayoutServerLoad } from './$types'; | ||
const stripe = new Stripe(import.meta.env['VITE_STRIPE_SECRET_KEY'], { | ||
apiVersion: '2023-08-16', | ||
}); | ||
export const load: LayoutServerLoad = async ({ locals, url }) => { | ||
@@ -16,2 +21,28 @@ const protectedRoutes = ['protected'] | ||
if (currentUser){ | ||
let subscriptionStatus = null; | ||
let subscriptionID = null; | ||
const stripeCustomerResult = await stripe.customers.list({ | ||
limit: 1, | ||
email: currentUser.email | ||
}); | ||
if (stripeCustomerResult.data.length > 0){ | ||
const stripeCustomerID = stripeCustomerResult.data[0].id; | ||
const subscriptionResult = await stripe.subscriptions.list({ | ||
customer: stripeCustomerID | ||
}); | ||
if (subscriptionResult.data.length > 0){ | ||
subscriptionStatus = subscriptionResult.data[0].status; | ||
subscriptionID = subscriptionResult.data[0].id; | ||
await locals.pb?.collection('users').update(currentUserToken.id, { | ||
subscriptionID: subscriptionStatus === "active" ? subscriptionID : null | ||
}) | ||
} else { | ||
await locals.pb?.collection('users').update(currentUserToken.id, { | ||
subscriptionID: null | ||
}) | ||
} | ||
} | ||
if (currentUser.nonce) { | ||
@@ -27,2 +58,5 @@ const nonce = url.searchParams.get('nonce') | ||
loggedIn: locals.pb?.authStore.isValid, | ||
username: currentUser?.name || "Current User", | ||
subscriptionID: currentUser?.subscriptionID, | ||
credits: currentUser?.credits | ||
} | ||
@@ -29,0 +63,0 @@ } |
@@ -16,3 +16,5 @@ import jwt from 'jsonwebtoken'; | ||
export type Choice = { | ||
type: "payment" | "subscription"; | ||
sku: string; | ||
description: string; | ||
price: number; | ||
@@ -28,5 +30,7 @@ label: string; | ||
const offerings: Array<Choice> = [ | ||
{sku: "good", price: 5, label: "good", stripeID: VITE_STRIPE_ID_GOOD_PRODUCT}, | ||
{sku: "better", price: 20, label: "better", stripeID: VITE_STRIPE_ID_BETTER_PRODUCT}, | ||
{sku: "best", price: 30, label: "best", stripeID: VITE_STRIPE_ID_BEST_PRODUCT} | ||
{type: "payment", sku: "good", price: 5, description: "a good product", label: "good", stripeID: VITE_STRIPE_ID_GOOD_PRODUCT}, | ||
{type: "payment", sku: "better", price: 20, description: "a better product", label: "better", stripeID: VITE_STRIPE_ID_BETTER_PRODUCT}, | ||
{type: "payment", sku: "best", price: 30, description: "the best product", label: "best", stripeID: VITE_STRIPE_ID_BEST_PRODUCT}, | ||
{type: "subscription", sku: "subscription", description: "a subscription", price: 20, label: "Monthly Subscription", description: "As many interviews as you like. Billed monthly. Cancel anytime.", credits: 0, stripeID: VITE_STRIPE_ID_SUBSCRIPTION}, | ||
] | ||
@@ -33,0 +37,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
367751
30
367