Skip to content

Commit

Permalink
Remove Clerk
Browse files Browse the repository at this point in the history
  • Loading branch information
IhsenBouallegue committed Nov 14, 2023
1 parent a69a251 commit 5f007c3
Show file tree
Hide file tree
Showing 11 changed files with 18,684 additions and 9,317 deletions.
10,288 changes: 10,288 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"dependencies": {
"@auth/core": "^0.18.0",
"@auth/drizzle-adapter": "^0.3.3",
"@clerk/nextjs": "4.26.1",
"@hookform/resolvers": "^3.3.1",
"@paralleldrive/cuid2": "2.2.2",
"@planetscale/database": "^1.11.0",
Expand Down Expand Up @@ -69,7 +68,6 @@
"devDependencies": {
"@babel/core": "^7.19.6",
"@biomejs/biome": "1.3.1",
"@clerk/types": "3.57.0",
"@evilmartians/lefthook": "^1.5.0",
"@next/eslint-plugin-next": "^14.0.0",
"@types/node": "20.8.9",
Expand Down
3 changes: 1 addition & 2 deletions src/app/(home)/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import Faq from "@/sections/home/faq";
import { Features } from "@/sections/home/features/features";
import Hero from "@/sections/home/hero";
import Mission from "@/sections/home/mission";
import Pricing from "@/sections/home/pricing";

export default function page() {
return (
Expand All @@ -15,7 +14,7 @@ export default function page() {
<Hero />
<Features />
<Mission />
<Pricing />
{/* <Pricing /> */}
<Faq />
{/* <Features /> */}
{/* <Contact /> */}
Expand Down
54 changes: 23 additions & 31 deletions src/app/api/billing-portal/route.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
import { findOrCreateCustomerId } from "@/lib/findOrCreateCustomerId";
import { stripe } from "@/lib/stripe";
import { auth } from "@clerk/nextjs";
import { NextRequest, NextResponse } from "next/server";

export async function POST(req: NextRequest) {
const origin = req.headers.get("origin") || "http://localhost:3000";
try {
const { userId, orgId } = auth();
if (!userId)
return NextResponse.json(
{ message: "You must be logged in to manage billing" },
{ status: 500 }
);
const customerId = await findOrCreateCustomerId({
clerkUserId: userId,
clerkOrgId: orgId,
});

const { url } = await stripe.billingPortal.sessions.create({
customer: customerId,
return_url: `${origin}/dashboard`,
});

return NextResponse.json({ url }, { status: 200 });
} catch (e) {
console.error(e, "Stripe Billing Portal redirect error");

// Here, consider redirecting the user to an error page
return NextResponse.error();
}
export async function POST() {
1; // const origin = req.headers.get("origin") || "http://localhost:3000";
// try {
// const { userId, orgId } = auth();
// if (!userId)
// return NextResponse.json(
// { message: "You must be logged in to manage billing" },
// { status: 500 }
// );
// const customerId = await findOrCreateCustomerId({
// clerkUserId: userId,
// clerkOrgId: orgId,
// });
// const { url } = await stripe.billingPortal.sessions.create({
// customer: customerId,
// return_url: `${origin}/dashboard`,
// });
// return NextResponse.json({ url }, { status: 200 });
// } catch (e) {
// console.error(e, "Stripe Billing Portal redirect error");
// // Here, consider redirecting the user to an error page
// return NextResponse.error();
// }
}
94 changes: 45 additions & 49 deletions src/app/api/create-checkout-session/route.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,48 @@
import { findOrCreateCustomerId } from "@/lib/findOrCreateCustomerId";
import { stripe } from "@/lib/stripe";
import { auth } from "@clerk/nextjs";
import { NextResponse } from "next/server";
import Stripe from "stripe";
// import { findOrCreateCustomerId } from "@/lib/findOrCreateCustomerId";
// import { stripe } from "@/lib/stripe";
// import { NextResponse } from "next/server";
// import Stripe from "stripe";

interface CheckoutSubscriptionBody {
priceId: string;
}

export async function POST(req: Request) {
const body = (await req.json()) as CheckoutSubscriptionBody;
const origin = req.headers.get("origin") || "http://localhost:3000";

try {
const { userId, orgId, user } = auth();
if (!userId)
return NextResponse.json(
{ message: "You must be logged in to subscribe" },
{ status: 500 }
);
const customerId = await findOrCreateCustomerId({
clerkUserId: userId,
clerkOrgId: orgId,
});

const session = await stripe.checkout.sessions.create({
customer_email: user?.emailAddresses[0].emailAddress,
billing_address_collection: "auto",
customer: customerId,
line_items: [
{
price: body.priceId,
quantity: 1,
},
],
mode: "subscription",
success_url: `${origin}/dashboard`,
cancel_url: `${origin}`,
});
if (session.url === null)
return NextResponse.json({ message: "No session url" }, { status: 500 });
// interface CheckoutSubscriptionBody {
// priceId: string;
// }

return NextResponse.json({ url: session.url }, { status: 200 });
} catch (error) {
if (error instanceof Stripe.errors.StripeError) {
const { message } = error;
return NextResponse.json({ message }, { status: error.statusCode });
}
return NextResponse.json({ error }, { status: 500 });
}
export async function POST() {
// const body = (await req.json()) as CheckoutSubscriptionBody;
// const origin = req.headers.get("origin") || "http://localhost:3000";
// try {
// const { userId, orgId, user } = auth();
// if (!userId)
// return NextResponse.json(
// { message: "You must be logged in to subscribe" },
// { status: 500 }
// );
// const customerId = await findOrCreateCustomerId({
// clerkUserId: userId,
// clerkOrgId: orgId,
// });
// const session = await stripe.checkout.sessions.create({
// customer_email: user?.emailAddresses[0].emailAddress,
// billing_address_collection: "auto",
// customer: customerId,
// line_items: [
// {
// price: body.priceId,
// quantity: 1,
// },
// ],
// mode: "subscription",
// success_url: `${origin}/dashboard`,
// cancel_url: `${origin}`,
// });
// if (session.url === null)
// return NextResponse.json({ message: "No session url" }, { status: 500 });
// return NextResponse.json({ url: session.url }, { status: 200 });
// } catch (error) {
// if (error instanceof Stripe.errors.StripeError) {
// const { message } = error;
// return NextResponse.json({ message }, { status: error.statusCode });
// }
// return NextResponse.json({ error }, { status: 500 });
// }
}
13 changes: 13 additions & 0 deletions src/app/api/products/route.old.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export async function GET() {
// const { sessionClaims } = auth();
// const stripeCustomerId = getStripeCustomerId(sessionClaims);
// const subscription = await stripe.subscriptions.list({
// customer: stripeCustomerId,
// status: "active",
// });
// const subscribedProductId = subscription.data[0]?.items?.data[0].plan.product;
// if (!subscribedProductId || subscribedProductId === null)
// return NextResponse.json({ message: "No product found" }, { status: 500 });
// const product = await stripe.products.retrieve(subscribedProductId as string);
// return NextResponse.json(product);
}
19 changes: 0 additions & 19 deletions src/app/api/products/route.ts

This file was deleted.

125 changes: 62 additions & 63 deletions src/lib/findOrCreateCustomerId.ts
Original file line number Diff line number Diff line change
@@ -1,67 +1,66 @@
import { clerkClient } from "@clerk/nextjs";
import { stripe } from "./stripe";
// import { stripe } from "./stripe";

export const findOrCreateCustomerId = async ({
clerkUserId,
clerkOrgId,
}: {
clerkUserId: string;
clerkOrgId?: string;
}) => {
let stripeCustomerId;
// export const findOrCreateCustomerId = async ({
// clerkUserId,
// clerkOrgId,
// }: {
// clerkUserId: string;
// clerkOrgId?: string;
// }) => {
// let stripeCustomerId;

if (clerkOrgId && clerkUserId) {
const user = await clerkClient.users.getUser(clerkUserId);
let organization = await clerkClient.organizations.getOrganization({
organizationId: clerkOrgId,
});
// if user already has a stripeCustomerId, return it
if (organization.publicMetadata?.stripeCustomerId) {
return organization.publicMetadata.stripeCustomerId as string;
}
// otherwise, create a new customer and save the stripeCustomerId
const createdCustomer = await stripe.customers.create({
name: `${organization.name}`,
email: user.emailAddresses.find(
(x) => x.id === user.primaryEmailAddressId
)!.emailAddress,
metadata: {
orgId: user.id,
},
});
// update the organization's publicMetadata with the stripeCustomerId
organization = await clerkClient.organizations.updateOrganization(
organization.id,
{
publicMetadata: {
stripeCustomerId: createdCustomer.id,
},
}
);
stripeCustomerId = createdCustomer.id;
}
// if (clerkOrgId && clerkUserId) {
// const user = await clerkClient.users.getUser(clerkUserId);
// let organization = await clerkClient.organizations.getOrganization({
// organizationId: clerkOrgId,
// });
// // if user already has a stripeCustomerId, return it
// if (organization.publicMetadata?.stripeCustomerId) {
// return organization.publicMetadata.stripeCustomerId as string;
// }
// // otherwise, create a new customer and save the stripeCustomerId
// const createdCustomer = await stripe.customers.create({
// name: `${organization.name}`,
// email: user.emailAddresses.find(
// (x) => x.id === user.primaryEmailAddressId
// )!.emailAddress,
// metadata: {
// orgId: user.id,
// },
// });
// // update the organization's publicMetadata with the stripeCustomerId
// organization = await clerkClient.organizations.updateOrganization(
// organization.id,
// {
// publicMetadata: {
// stripeCustomerId: createdCustomer.id,
// },
// }
// );
// stripeCustomerId = createdCustomer.id;
// }

let user = await clerkClient.users.getUser(clerkUserId);
// if user already has a stripeCustomerId, return it
if (user.publicMetadata.stripeCustomerId) {
return user.publicMetadata.stripeCustomerId as string;
}
// otherwise, create a new customer and save the stripeCustomerId
const createdCustomer = await stripe.customers.create({
name: `${user.firstName} ${user.lastName}`,
email: user.emailAddresses.find((x) => x.id === user.primaryEmailAddressId)!
.emailAddress,
metadata: {
userId: user.id,
},
});
// update the user's publicMetadata with the stripeCustomerId
user = await clerkClient.users.updateUser(user.id, {
publicMetadata: {
stripeCustomerId: createdCustomer.id,
},
});
stripeCustomerId = createdCustomer.id;
// let user = await clerkClient.users.getUser(clerkUserId);
// // if user already has a stripeCustomerId, return it
// if (user.publicMetadata.stripeCustomerId) {
// return user.publicMetadata.stripeCustomerId as string;
// }
// // otherwise, create a new customer and save the stripeCustomerId
// const createdCustomer = await stripe.customers.create({
// name: `${user.firstName} ${user.lastName}`,
// email: user.emailAddresses.find((x) => x.id === user.primaryEmailAddressId)!
// .emailAddress,
// metadata: {
// userId: user.id,
// },
// });
// // update the user's publicMetadata with the stripeCustomerId
// user = await clerkClient.users.updateUser(user.id, {
// publicMetadata: {
// stripeCustomerId: createdCustomer.id,
// },
// });
// stripeCustomerId = createdCustomer.id;

return stripeCustomerId;
};
// return stripeCustomerId;
// };
Loading

1 comment on commit 5f007c3

@vercel
Copy link

@vercel vercel bot commented on 5f007c3 Nov 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.