-
Notifications
You must be signed in to change notification settings - Fork 51
Daily branch 2025 08 27 #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7e0632a
feat: add pagination for chats & messages
rossmanko 5b89b00
feat: add chat deletion functionality
rossmanko ed283ad
refactor: small improvements
rossmanko 9669112
feat: add pro plan using stripe entitlements with workos
rossmanko 298d6ed
feat: improve stripe subscription purchase process
rossmanko b9bf9f3
refactor: implement coderabbit suggestions
rossmanko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import { NextRequest, NextResponse } from "next/server"; | ||
| import { WorkOS } from "@workos-inc/node"; | ||
|
|
||
| const workos = new WorkOS(process.env.WORKOS_API_KEY!, { | ||
| clientId: process.env.WORKOS_CLIENT_ID!, | ||
| }); | ||
rossmanko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| export async function GET(req: NextRequest) { | ||
| try { | ||
| // Get the session cookie | ||
| const sessionCookie = req.cookies.get("wos-session")?.value; | ||
|
|
||
| if (!sessionCookie) { | ||
| return NextResponse.json( | ||
| { error: "No session cookie found" }, | ||
| { status: 401 }, | ||
| ); | ||
| } | ||
|
|
||
| // Load the original session | ||
| const session = workos.userManagement.loadSealedSession({ | ||
| cookiePassword: process.env.WORKOS_COOKIE_PASSWORD!, | ||
| sessionData: sessionCookie, | ||
| }); | ||
|
|
||
| const refreshResult = await session.refresh(); | ||
| const { sealedSession, entitlements } = refreshResult as any; | ||
|
|
||
| const hasProPlan = (entitlements || []).includes("pro-monthly-plan"); | ||
|
|
||
rossmanko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Create response with entitlements | ||
| const response = NextResponse.json({ | ||
| entitlements: entitlements || [], | ||
| hasProPlan, | ||
| }); | ||
|
|
||
| // Set the updated refresh session data in a cookie | ||
| if (sealedSession) { | ||
| response.cookies.set("wos-session", sealedSession, { | ||
| httpOnly: true, | ||
| sameSite: "lax", | ||
| secure: true, | ||
| }); | ||
| } | ||
|
|
||
| return response; | ||
| } catch (error) { | ||
| console.error("💥 [Entitlements API] Error refreshing session:", error); | ||
| return NextResponse.json( | ||
| { error: "Failed to refresh session" }, | ||
| { status: 500 }, | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import Stripe from "stripe"; | ||
|
|
||
| const stripe = new Stripe(process.env.STRIPE_SECRET_KEY as string, { | ||
| apiVersion: "2025-08-27.basil", | ||
| }); | ||
|
|
||
| export { stripe }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| import { stripe } from "../stripe"; | ||
| import { workos } from "../workos"; | ||
| import { getUserID } from "@/lib/auth/get-user-id"; | ||
| import { NextRequest, NextResponse } from "next/server"; | ||
|
|
||
| export const POST = async (req: NextRequest) => { | ||
| try { | ||
| // Get user ID from authenticated session | ||
| const userId = await getUserID(req); | ||
|
|
||
| // Get user details from WorkOS to use email as organization name | ||
| const user = await workos.userManagement.getUser(userId); | ||
| const orgName = user.email; | ||
| const subscriptionLevel = "pro-monthly-plan"; | ||
|
|
||
rossmanko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Check if user already has an organization | ||
| const existingMemberships = | ||
| await workos.userManagement.listOrganizationMemberships({ | ||
| userId, | ||
| }); | ||
|
|
||
| let organization; | ||
|
|
||
| if (existingMemberships.data && existingMemberships.data.length > 0) { | ||
| // User already has an organization, use the first one | ||
| const membership = existingMemberships.data[0]; | ||
| organization = await workos.organizations.getOrganization( | ||
| membership.organizationId, | ||
| ); | ||
| } else { | ||
| // Create new organization for the user | ||
| organization = await workos.organizations.createOrganization({ | ||
| name: orgName, | ||
| }); | ||
|
|
||
| await workos.userManagement.createOrganizationMembership({ | ||
| organizationId: organization.id, | ||
| userId, | ||
| roleSlug: "admin", | ||
| }); | ||
| } | ||
|
|
||
| // Retrieve price ID from Stripe | ||
| // The Stripe look up key for the price *must* be the same as the subscription level string | ||
| let price; | ||
|
|
||
| try { | ||
| price = await stripe.prices.list({ | ||
| lookup_keys: [subscriptionLevel], | ||
| }); | ||
|
|
||
| // Check if price data exists and has at least one item | ||
| if (!price.data || price.data.length === 0) { | ||
| console.error( | ||
| `No price found for lookup key: ${subscriptionLevel}. This is likely because the products and prices have not been created yet. Run the setup script \`pnpm run setup\` to automatically create them.`, | ||
| ); | ||
| return NextResponse.json( | ||
| { | ||
| error: "Subscription plan not found", | ||
| details: `No price found for plan: ${subscriptionLevel}`, | ||
| }, | ||
| { status: 404 }, | ||
| ); | ||
| } | ||
| } catch (error) { | ||
| console.error( | ||
| `Error retrieving price from Stripe for lookup key: ${subscriptionLevel}. This is likely because the products and prices have not been created yet. Run the setup script \`pnpm run setup\` to automatically create them.`, | ||
| error, | ||
| ); | ||
| return NextResponse.json( | ||
| { error: "Error retrieving price from Stripe" }, | ||
| { status: 500 }, | ||
| ); | ||
| } | ||
|
|
||
| // Check if organization already has a Stripe customer | ||
| let customer; | ||
|
|
||
| // Try to find existing customer by email and organization metadata | ||
| const existingCustomers = await stripe.customers.list({ | ||
| email: user.email, | ||
| limit: 10, // Get more to check metadata | ||
| }); | ||
|
|
||
| // Look for a customer with matching organization ID in metadata | ||
| const matchingCustomer = existingCustomers.data.find( | ||
| (c) => c.metadata.workOSOrganizationId === organization.id, | ||
| ); | ||
|
|
||
| if (matchingCustomer) { | ||
| customer = matchingCustomer; | ||
| } | ||
|
|
||
| if (!customer) { | ||
| // Create new Stripe customer | ||
| customer = await stripe.customers.create({ | ||
| email: user.email, | ||
| metadata: { | ||
| workOSOrganizationId: organization.id, | ||
| }, | ||
| }); | ||
|
|
||
| // Update WorkOS organization with Stripe customer ID | ||
| // This will allow WorkOS to automatically add entitlements to the access token | ||
| await workos.organizations.updateOrganization({ | ||
| organization: organization.id, | ||
| stripeCustomerId: customer.id, | ||
| }); | ||
| } | ||
|
|
||
| const baseUrl = process.env.NEXT_PUBLIC_BASE_URL; | ||
| if (!baseUrl) { | ||
| return NextResponse.json( | ||
| { error: "NEXT_PUBLIC_BASE_URL is not configured" }, | ||
| { status: 500 }, | ||
| ); | ||
| } | ||
|
|
||
| const session = await stripe.checkout.sessions.create({ | ||
| customer: customer.id, | ||
| billing_address_collection: "auto", | ||
| line_items: [ | ||
| { | ||
| price: price.data[0].id, | ||
| quantity: 1, | ||
| }, | ||
| ], | ||
| mode: "subscription", | ||
| success_url: `${process.env.NEXT_PUBLIC_BASE_URL}`, | ||
| cancel_url: `${process.env.NEXT_PUBLIC_BASE_URL}`, | ||
| }); | ||
rossmanko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return NextResponse.json({ url: session.url }); | ||
| } catch (error: unknown) { | ||
| const errorMessage = | ||
| error instanceof Error ? error.message : "An error occurred"; | ||
| console.error(errorMessage, error); | ||
| return NextResponse.json({ error: errorMessage }, { status: 500 }); | ||
| } | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import { WorkOS } from "@workos-inc/node"; | ||
|
|
||
| const workos = new WorkOS(process.env.WORKOS_API_KEY, { | ||
| clientId: process.env.WORKOS_CLIENT_ID, | ||
| }); | ||
|
|
||
| export { workos }; | ||
rossmanko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.