store last visited org in cookie

This commit is contained in:
miloschwartz
2025-06-24 14:54:07 -04:00
parent 34180ca454
commit 9bb4d8b2a3
6 changed files with 14229 additions and 14065 deletions

View File

@@ -8,6 +8,7 @@ import { GetOrgUserResponse } from "@server/routers/user";
import { AxiosResponse } from "axios";
import { redirect } from "next/navigation";
import { cache } from "react";
import SetLastOrgCookie from "@app/components/SetLastOrgCookie";
export default async function OrgLayout(props: {
children: React.ReactNode;
@@ -52,6 +53,7 @@ export default async function OrgLayout(props: {
return (
<>
{props.children}
<SetLastOrgCookie orgId={orgId} />
</>
);
}

View File

@@ -12,6 +12,7 @@ import { cleanRedirect } from "@app/lib/cleanRedirect";
import { Layout } from "@app/components/Layout";
import { rootNavItems } from "./navigation";
import { InitialSetupCompleteResponse } from "@server/routers/auth";
import { cookies } from "next/headers";
export const dynamic = "force-dynamic";
@@ -72,6 +73,25 @@ export default async function Page(props: {
}
}
// Check for pangolin-last-org cookie and redirect if valid
const allCookies = await cookies();
const lastOrgCookie = allCookies.get("pangolin-last-org")?.value;
if (lastOrgCookie && orgs.length > 0) {
// Check if the last org from cookie exists in user's organizations
const lastOrgExists = orgs.some(org => org.orgId === lastOrgCookie);
if (lastOrgExists) {
redirect(`/${lastOrgCookie}`);
} else {
const ownedOrg = orgs.find(org => org.isOwner);
if (ownedOrg) {
redirect(`/${ownedOrg.orgId}`);
} else {
redirect("/setup");
}
}
}
return (
<UserProvider user={user}>
<Layout orgs={orgs} navItems={rootNavItems} showBreadcrumbs={false}>

View File

@@ -0,0 +1,19 @@
"use client";
import { useEffect } from "react";
interface SetLastOrgCookieProps {
orgId: string;
}
export default function SetLastOrgCookie({ orgId }: SetLastOrgCookieProps) {
useEffect(() => {
const isSecure =
typeof window !== "undefined" &&
window.location.protocol === "https:";
document.cookie = `pangolin-last-org=${orgId}; path=/; max-age=${60 * 60 * 24 * 30}; samesite=lax${isSecure ? "; secure" : ""}`;
}, [orgId]);
return null;
}