mirror of
https://github.com/formbricks/formbricks.git
synced 2026-02-21 10:08:34 -06:00
51 lines
1.4 KiB
TypeScript
51 lines
1.4 KiB
TypeScript
import type { Session } from "next-auth";
|
|
import { getServerSession } from "next-auth";
|
|
import { redirect } from "next/navigation";
|
|
|
|
import { authOptions } from "@formbricks/lib/authOptions";
|
|
import { ONBOARDING_DISABLED } from "@formbricks/lib/constants";
|
|
import { getFirstEnvironmentByUserId } from "@formbricks/lib/environment/service";
|
|
import { getTeamsByUserId } from "@formbricks/lib/team/service";
|
|
import { ClientLogout } from "@formbricks/ui/ClientLogout";
|
|
|
|
const Page = async () => {
|
|
const session: Session | null = await getServerSession(authOptions);
|
|
|
|
if (!session) {
|
|
redirect("/auth/login");
|
|
}
|
|
|
|
if (!session?.user) {
|
|
return <ClientLogout />;
|
|
}
|
|
|
|
const teams = await getTeamsByUserId(session.user.id);
|
|
if (!teams || teams.length === 0) {
|
|
console.error("Failed to get teams, redirecting to create-first-team");
|
|
return redirect("/create-first-team");
|
|
}
|
|
|
|
if (!ONBOARDING_DISABLED && !session.user.onboardingCompleted) {
|
|
return redirect(`/onboarding`);
|
|
}
|
|
|
|
let environment;
|
|
try {
|
|
environment = await getFirstEnvironmentByUserId(session?.user.id);
|
|
if (!environment) {
|
|
throw new Error("No environment found");
|
|
}
|
|
} 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}`);
|
|
};
|
|
|
|
export default Page;
|