Files
calendar/apps/web/lib/signup/getServerSideProps.tsx
T

39 lines
1.1 KiB
TypeScript

import { getServerSession } from "@calcom/features/auth/lib/getServerSession";
import { WEBAPP_URL } from "@calcom/lib/constants";
import { getSafeRedirectUrl } from "@calcom/lib/getSafeRedirectUrl";
import type { GetServerSidePropsContext } from "next";
import { z } from "zod";
export const getServerSideProps = async (ctx: GetServerSidePropsContext) => {
const redirectUrlData = z
.string()
.refine((value) => value.startsWith(WEBAPP_URL), {
params: (value: string) => ({ value }),
message: "Redirect URL must start with 'cal.com'",
})
.optional()
.safeParse(ctx.query.redirect);
const redirectUrl = redirectUrlData.success && redirectUrlData.data ? redirectUrlData.data : null;
const session = await getServerSession({
req: ctx.req,
});
if (session?.user?.id) {
return {
redirect: {
permanent: false,
destination: getSafeRedirectUrl(redirectUrl || "") || "/",
},
} as const;
}
return {
redirect: {
permanent: false,
destination: redirectUrl ? `/auth/login?callbackUrl=${encodeURIComponent(redirectUrl)}` : "/auth/login",
},
} as const;
};