Files
formbricks/apps/web/app/api/google-sheet/callback/route.ts
T
Shubham Palriwala 13afba7615 feat: move env vars from build-time to run-time to better support self-hosting environments (#789)
* feat: privacy, imprint, and terms URL env vars now do not need rebuilding

* feat: disable_singup env var now do not need rebuilding

* feat: password_reset_disabled env var now do not need rebuilding

* feat: email_verification_disabled env var now do not need rebuilding

* feat: github_oauth & google_oauth env var now do not need rebuilding

* feat: move logic of env vars to serverside and send boolean client-side

* feat: invite_disabled env var now do not need rebuilding

* feat: rename vars logically

* feat: migration guide

* feat: update docker-compose as per v1.1

* deprecate: unused NEXT_PUBLIC_VERCEL_URL & VERCEL_URL

* deprecate: unused RAILWAY_STATIC_URL

* deprecate: unused RENDER_EXTERNAL_URL

* deprecate: unused HEROKU_APP_NAME

* fix: define WEBAPP_URL & replace NEXT_WEBAPP_URL with it

* migrate: NEXT_PUBLIC_IS_FORMBRICKS_CLOUD to IS_FORMBRICKS_CLOUD

* chore: move all env parsing to a constants.ts from page files

* feat: migrate client side envs to server side

* redo: isFormbricksCloud to navbar serverside page

* fix: constants is now a server only file

* fix: removal of use swr underway

* fix: move 1 tag away from swr to service

* feat: move away from tags swr

* feat: move away from surveys  swr

* feat: move away from eventClass swr

* feat: move away from event swr

* fix: make constants server-only

* remove comments from .env.example, use constants in MetaInformation

* clean up services

* rename tag function

* fix build error

* fix smaller bugs, fix Response % not working in summary

---------

Co-authored-by: Matthias Nannt <mail@matthiasnannt.com>
2023-09-30 21:40:59 +02:00

85 lines
2.5 KiB
TypeScript

import { prisma } from "@formbricks/database";
import {
GOOGLE_SHEETS_CLIENT_ID,
WEBAPP_URL,
GOOGLE_SHEETS_CLIENT_SECRET,
GOOGLE_SHEETS_REDIRECT_URL,
} from "@formbricks/lib/constants";
import { google } from "googleapis";
import { NextRequest, NextResponse } from "next/server";
export async function GET(req: NextRequest) {
const url = req.url;
const queryParams = new URLSearchParams(url.split("?")[1]); // Split the URL and get the query parameters
const environmentId = queryParams.get("state"); // Get the value of the 'state' parameter
const code = queryParams.get("code");
if (!environmentId) {
return NextResponse.json({ error: "Invalid environmentId" });
}
if (code && typeof code !== "string") {
return NextResponse.json({ message: "`code` must be a string" }, { status: 400 });
}
const client_id = GOOGLE_SHEETS_CLIENT_ID;
const client_secret = GOOGLE_SHEETS_CLIENT_SECRET;
const redirect_uri = GOOGLE_SHEETS_REDIRECT_URL;
if (!client_id) return NextResponse.json({ Error: "Google client id is missing" }, { status: 400 });
if (!client_secret) return NextResponse.json({ Error: "Google client secret is missing" }, { status: 400 });
if (!redirect_uri) return NextResponse.json({ Error: "Google redirect url is missing" }, { status: 400 });
const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uri);
let key;
let userEmail;
if (code) {
const token = await oAuth2Client.getToken(code);
key = token.res?.data;
// Set credentials using the provided token
oAuth2Client.setCredentials({
access_token: key.access_token,
});
// Fetch user's email
const oauth2 = google.oauth2({
auth: oAuth2Client,
version: "v2",
});
const userInfo = await oauth2.userinfo.get();
userEmail = userInfo.data.email;
}
const googleSheetIntegration = {
type: "googleSheets" as "googleSheets",
environment: environmentId,
config: {
key,
data: [],
email: userEmail,
},
};
const result = await prisma.integration.upsert({
where: {
type_environmentId: {
environmentId,
type: "googleSheets",
},
},
update: {
...googleSheetIntegration,
environment: { connect: { id: environmentId } },
},
create: {
...googleSheetIntegration,
environment: { connect: { id: environmentId } },
},
});
if (result) {
return NextResponse.redirect(`${WEBAPP_URL}/environments/${environmentId}/integrations/google-sheets`);
}
}