Skip to content

Commit

Permalink
Fix redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusab committed Feb 10, 2025
1 parent b713a99 commit c2e3b1f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { OnboardingSteps } from "@/components/onboarding-steps";
import { SearchInput } from "@/components/search-input";
import { HydrateClient, trpc } from "@/trpc/server";
import { getTranslations } from "next-intl/server";
import { notFound } from "next/navigation";
import { redirect } from "next/navigation";
import { Suspense } from "react";

export default async function Page({
Expand Down Expand Up @@ -56,8 +56,9 @@ export default async function Page({

return <OnboardingSteps projectId={data?.id} />;
}
} catch (error) {
return notFound();
} catch {
// If there is a permission error, redirect to the user's default organization
redirect("/api/default-org");
}

return (
Expand Down
25 changes: 25 additions & 0 deletions apps/web/src/app/api/default-org/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { getOrganizationByUserId } from "@/db/queries/organization";
import { getSession } from "@languine/supabase/session";
import { cookies } from "next/headers";
import { redirect } from "next/navigation";

export async function GET() {
const cookieStore = await cookies();
const {
data: { session },
} = await getSession();

// Delete user preferences cookie
cookieStore.delete("user-preferences");

if (session?.user.id) {
const organization = await getOrganizationByUserId(session.user.id);

// If user is part of an organization, redirect to the organization's default project
if (organization) {
redirect(`/${organization.organization.id}/default`);
}
}

redirect("/login");
}

0 comments on commit c2e3b1f

Please sign in to comment.