Files
formbricks-formbricks/apps/web/app/page.tsx
Matti Nannt 9cc836a775 Clean up + Bugfixes (#443)
* format code

* fix building issue in database package

* update turbo config and package jsons

* update error package with more information and license

* fix typescript issues in ui package

* update package-lock

* update packages

* clean dependencies in web

* clean up dependencies in demo & formbricks-com

* remove unsused template file

* remove legacy capture endpoint

* remove unfinished client endpoints

* clean up unused functions

* fix formbricks not loading on invalid session

* update readme
2023-06-28 13:07:51 +02:00

51 lines
1.3 KiB
TypeScript

import ClientLogout from "@/app/ClientLogout";
import { authOptions } from "@/app/api/auth/[...nextauth]/authOptions";
import { WEBAPP_URL } from "@formbricks/lib/constants";
import type { Session } from "next-auth";
import { getServerSession } from "next-auth";
import { headers } from "next/headers";
import { redirect } from "next/navigation";
async function getEnvironment() {
const cookie = headers().get("cookie") || "";
const res = await fetch(`${WEBAPP_URL}/api/v1/environments/find-first`, {
headers: {
cookie,
},
});
if (!res.ok) {
const error = await res.json();
console.error(error);
throw new Error("Failed to fetch data");
}
return res.json();
}
export default async function Home() {
const session: Session | null = await getServerSession(authOptions);
if (!session) {
redirect("/auth/login");
}
if (session?.user && !session?.user?.onboardingCompleted) {
return redirect(`/onboarding`);
}
let environment;
try {
environment = await getEnvironment();
} catch (error) {
console.error("error getting environment", error);
}
if (!environment) {
console.error("Failed to get first environment of user; signing out");
return <ClientLogout />;
}
return redirect(`/environments/${environment.id}`);
}