2022-08-09 09:21:15 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
|
|
|
|
import { ensureSession } from "@calcom/lib/auth";
|
|
|
|
import { defaultHandler, defaultResponder } from "@calcom/lib/server";
|
|
|
|
|
|
|
|
async function handler(req: NextApiRequest, res: NextApiResponse<Response>): Promise<void> {
|
2022-09-05 20:25:21 +00:00
|
|
|
// Only logged in users can opt-in/out
|
|
|
|
await ensureSession({ req });
|
|
|
|
|
|
|
|
// If has the cookie, Opt-out of V2
|
|
|
|
if ("calcom-v2-early-access" in req.cookies && req.cookies["calcom-v2-early-access"] === "1") {
|
|
|
|
res.setHeader("Set-Cookie", `calcom-v2-early-access=0; Max-Age=0; Path=/`);
|
|
|
|
} else {
|
|
|
|
/* Opt-int to V2 */
|
|
|
|
res.setHeader("Set-Cookie", "calcom-v2-early-access=1; Path=/");
|
|
|
|
}
|
|
|
|
|
2022-09-07 01:18:47 +00:00
|
|
|
let redirectUrl = "/";
|
|
|
|
|
|
|
|
// We take you back where you came from if possible
|
|
|
|
if (typeof req.headers["referer"] === "string") redirectUrl = req.headers["referer"];
|
|
|
|
|
|
|
|
res.redirect(redirectUrl);
|
2022-08-09 09:21:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default defaultHandler({
|
|
|
|
GET: Promise.resolve({ default: defaultResponder(handler) }),
|
|
|
|
});
|