* version and verify-booking-token * username route * api/me * logo route * render email in dev * link route * future * nope and geo location * referal link and daily * intercom route and dynamic variables * intercom route and dynamic variables * scim 2.0 and helpscout route * app credentials * api/book events * fix daily import path in tests * fix buildLegacyRequest generation * fix type errors * migrate the /teams/ routes * move cron jobs * fix daily import path in tests * fix buildLegacyRequest generation * fix type errors * migrate the /teams/ routes * move cron jobs * Revert "api/book events" This reverts commit 607a32fb5b754cad090c2d0cbf64a68f990e220e. * mock next server NextResponse to fix daily video * add default responder to teams api * fix search params * uses nextUrl.searchParams instead of new url * uses nextUrl.searchParams instead of new url * remove outdated api config * remove app dir version of inbound dynamic variables * restore pages version of inbound variables * fix missing code from stupid cursor --------- Co-authored-by: Benny Joo <sldisek783@gmail.com>
87 lines
3.0 KiB
TypeScript
87 lines
3.0 KiB
TypeScript
import { OAuth2Client } from "googleapis-common";
|
|
import { cookies, headers } from "next/headers";
|
|
import type { NextRequest } from "next/server";
|
|
import { NextResponse } from "next/server";
|
|
import { z } from "zod";
|
|
|
|
import getAppKeysFromSlug from "@calcom/app-store/_utils/getAppKeysFromSlug";
|
|
import { throwIfNotHaveAdminAccessToTeam } from "@calcom/app-store/_utils/throwIfNotHaveAdminAccessToTeam";
|
|
import { getServerSession } from "@calcom/features/auth/lib/getServerSession";
|
|
import { WEBAPP_URL } from "@calcom/lib/constants";
|
|
import { getSafeRedirectUrl } from "@calcom/lib/getSafeRedirectUrl";
|
|
import prisma from "@calcom/prisma";
|
|
|
|
import { buildLegacyRequest } from "@lib/buildLegacyCtx";
|
|
|
|
const stateSchema = z.object({
|
|
teamId: z.string(),
|
|
});
|
|
|
|
export async function GET(request: NextRequest) {
|
|
try {
|
|
const headersList = headers();
|
|
const cookiesList = cookies();
|
|
const legacyReq = buildLegacyRequest(headersList, cookiesList);
|
|
|
|
const session = await getServerSession({ req: legacyReq });
|
|
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json({ message: "You must be logged in to do this" }, { status: 401 });
|
|
}
|
|
|
|
const code = request.nextUrl.searchParams.get("code");
|
|
const state = request.nextUrl.searchParams.get("state");
|
|
|
|
if (!state) {
|
|
return NextResponse.json({ message: "No state provided" }, { status: 400 });
|
|
}
|
|
|
|
const parsedState = stateSchema.parse(JSON.parse(state));
|
|
const { teamId } = parsedState;
|
|
|
|
await throwIfNotHaveAdminAccessToTeam({
|
|
teamId: Number(teamId) ?? null,
|
|
userId: session.user.id,
|
|
});
|
|
|
|
if (!code || typeof code !== "string") {
|
|
return NextResponse.json({ message: "`code` must be a string" }, { status: 400 });
|
|
}
|
|
|
|
const { client_id, client_secret } = await getAppKeysFromSlug("google-calendar");
|
|
|
|
if (!client_id || typeof client_id !== "string")
|
|
return NextResponse.json({ message: "Google client_id missing." }, { status: 400 });
|
|
|
|
if (!client_secret || typeof client_secret !== "string")
|
|
return NextResponse.json({ message: "Google client_secret missing." }, { status: 400 });
|
|
|
|
const redirect_uri = `${WEBAPP_URL}/api/teams/googleworkspace/callback`;
|
|
const oAuth2Client = new OAuth2Client(client_id, client_secret, redirect_uri);
|
|
|
|
const credentials = await oAuth2Client.getToken(code);
|
|
|
|
await prisma.credential.create({
|
|
data: {
|
|
type: "google_workspace_directory",
|
|
key: credentials.res?.data,
|
|
userId: session.user.id,
|
|
},
|
|
});
|
|
|
|
let redirectUrl = `${WEBAPP_URL}/settings`;
|
|
|
|
if (teamId) {
|
|
redirectUrl = `${WEBAPP_URL}/settings/teams/${teamId}/members?inviteModal=true&bulk=true`;
|
|
}
|
|
|
|
const safeRedirectUrl = getSafeRedirectUrl(redirectUrl) ?? `${WEBAPP_URL}/teams`;
|
|
return NextResponse.redirect(safeRedirectUrl);
|
|
} catch (error) {
|
|
console.error("Error in Google Workspace callback:", error);
|
|
|
|
// Redirect to teams page on error
|
|
return NextResponse.redirect(`${WEBAPP_URL}/teams`);
|
|
}
|
|
}
|