mirror of
https://github.com/formbricks/formbricks.git
synced 2026-04-30 10:40:13 -05:00
35b2d12e18
Co-authored-by: Dhruwang <dhruwangjariwala18@gmail.com> Co-authored-by: Matthias Nannt <mail@matthiasnannt.com> Co-authored-by: pandeymangg <anshuman.pandey9999@gmail.com>
63 lines
2.1 KiB
TypeScript
63 lines
2.1 KiB
TypeScript
import ClientEnvironmentRedirect from "@/app/ClientEnvironmentRedirect";
|
|
import { authOptions } from "@/modules/auth/lib/authOptions";
|
|
import { ClientLogout } from "@/modules/ui/components/client-logout";
|
|
import type { Session } from "next-auth";
|
|
import { getServerSession } from "next-auth";
|
|
import { getTranslations } from "next-intl/server";
|
|
import { redirect } from "next/navigation";
|
|
import { getFirstEnvironmentIdByUserId } from "@formbricks/lib/environment/service";
|
|
import { getIsFreshInstance } from "@formbricks/lib/instance/service";
|
|
import { getMembershipByUserIdOrganizationId } from "@formbricks/lib/membership/service";
|
|
import { getAccessFlags } from "@formbricks/lib/membership/utils";
|
|
import { getOrganizationsByUserId } from "@formbricks/lib/organization/service";
|
|
|
|
const Page = async () => {
|
|
const t = await getTranslations();
|
|
const session: Session | null = await getServerSession(authOptions);
|
|
const isFreshInstance = await getIsFreshInstance();
|
|
|
|
if (!session) {
|
|
if (isFreshInstance) {
|
|
redirect("/setup/intro");
|
|
} else {
|
|
redirect("/auth/login");
|
|
}
|
|
}
|
|
|
|
if (!session?.user) {
|
|
return <ClientLogout />;
|
|
}
|
|
|
|
const userOrganizations = await getOrganizationsByUserId(session.user.id);
|
|
|
|
if (userOrganizations.length === 0) {
|
|
return redirect("/setup/organization/create");
|
|
}
|
|
|
|
let environmentId: string | null = null;
|
|
try {
|
|
environmentId = await getFirstEnvironmentIdByUserId(session?.user.id);
|
|
} catch (error) {
|
|
console.error(`error getting environment: ${error}`);
|
|
}
|
|
|
|
const currentUserMembership = await getMembershipByUserIdOrganizationId(
|
|
session?.user.id,
|
|
userOrganizations[0].id
|
|
);
|
|
const { isManager, isOwner } = getAccessFlags(currentUserMembership?.role);
|
|
|
|
if (!environmentId) {
|
|
console.error(t("common.failed_to_get_first_environment_of_user"));
|
|
if (isOwner || isManager) {
|
|
return redirect(`/organizations/${userOrganizations[0].id}/projects/new/mode`);
|
|
} else {
|
|
return redirect(`/organizations/${userOrganizations[0].id}/landing`);
|
|
}
|
|
}
|
|
|
|
return <ClientEnvironmentRedirect environmentId={environmentId} />;
|
|
};
|
|
|
|
export default Page;
|