Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
🐛 Fix checkout not redirecting guest to login (#1851)
Luke Vella41e4abe0bc29
diff --git a/apps/web/src/app/api/stripe/checkout/route.ts b/apps/web/src/app/api/stripe/checkout/route.tsindex 86908a5eb..fbae62ebb 100644--- a/apps/web/src/app/api/stripe/checkout/route.ts+++ b/apps/web/src/app/api/stripe/checkout/route.ts@@ -4,11 +4,12 @@ import { absoluteUrl } from "@rallly/utils/absolute-url";import type { NextRequest } from "next/server";import { NextResponse } from "next/server";import { z } from "zod";-import { getCurrentUserSpace } from "@/auth/data";+import { requireUserWithSpace } from "@/auth/data";import type {SubscriptionCheckoutMetadata,SubscriptionMetadata,} from "@/features/subscription/schema";+import { AppError } from "@/lib/errors";const inputSchema = z.object({period: z.enum(["monthly", "yearly"]).optional(),@@ -17,9 +18,16 @@ const inputSchema = z.object({});export async function POST(request: NextRequest) {- const { user, space } = await getCurrentUserSpace();- const spaceId = space.id;+ const { user, space } = await requireUserWithSpace();+ if (space.ownerId !== user.id) {+ throw new AppError({+ code: "FORBIDDEN",+ message: "You need to be the owner of this space to upgrade it",+ });+ }++ const spaceId = space.id;const formData = await request.formData();const data = inputSchema.safeParse(Object.fromEntries(formData.entries()));diff --git a/apps/web/src/auth/data.ts b/apps/web/src/auth/data.tsindex 4c92c8b51..7f8f5ca94 100644--- a/apps/web/src/auth/data.ts+++ b/apps/web/src/auth/data.ts@@ -1,6 +1,7 @@import "server-only";import { prisma } from "@rallly/database";+import { headers } from "next/headers";import { notFound, redirect } from "next/navigation";import { cache } from "react";import { createSpaceDTO } from "@/features/spaces/data";@@ -161,3 +162,70 @@ export const requireAdmin = cache(async () => {notFound();}});++export const requireUser = async () => {+ const session = await auth();+ const headersList = await headers();+ const pathname = headersList.get("x-pathname") ?? "/";++ if (!session?.user) {+ const searchParams = new URLSearchParams();+ searchParams.set("redirectTo", pathname);+ redirect(`/login?${searchParams.toString()}`);+ }++ const user = await prisma.user.findUnique({+ where: {+ id: session.user.id,+ },+ });++ if (!user) {+ redirect("/api/auth/invalid-session");+ }++ return createUserDTO(user);+};++export const requireUserWithSpace = async () => {+ const user = await requireUser();++ if (user.activeSpaceId) {+ const space = await prisma.space.findUnique({+ where: {+ id: user.activeSpaceId,+ },+ include: {+ subscription: true,+ members: true,+ },+ });++ if (space) {+ return {+ user,+ space: createSpaceDTO(user.id, space),+ };+ }+ console.error("Could not find user's active space");+ }++ const space = await prisma.space.findFirst({+ where: {+ ownerId: user.id,+ },+ include: {+ subscription: true,+ members: true,+ },+ });++ if (!space) {+ throw new AppError({+ code: "NOT_FOUND",+ message: "User does not own any spaces",+ });+ }++ return { user, space: createSpaceDTO(user.id, space) };+};