Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

feat: add a public cookie that stores the company id

Brad Barbinb8c7c3cfd9c2
diff --git a/apps/carbon/app/routes/_public+/callback.tsx b/apps/carbon/app/routes/_public+/callback.tsx
index caafb6b85d..7c1a5c6af0 100644
--- a/apps/carbon/app/routes/_public+/callback.tsx
+++ b/apps/carbon/app/routes/_public+/callback.tsx
@@ -5,11 +5,13 @@ import {
error,
getCarbonServiceRole,
} from "@carbon/auth";
+import { refreshAccessToken } from "@carbon/auth/auth.server";
+import { setCompanyId } from "@carbon/auth/company.server";
import {
- commitAuthSession,
destroyAuthSession,
flash,
getAuthSession,
+ setAuthSession,
} from "@carbon/auth/session.server";
import { getUserByEmail } from "@carbon/auth/users.server";
import { validator } from "@carbon/form";
@@ -18,7 +20,6 @@ import type { ActionFunctionArgs, LoaderFunctionArgs } from "@vercel/remix";
import { json, redirect } from "@vercel/remix";
import { useEffect, useRef, useState } from "react";
-import { refreshAccessToken } from "@carbon/auth/auth.server";
import {
Alert,
AlertDescription,
@@ -75,12 +76,15 @@ export async function action({ request }: ActionFunctionArgs): FormActionData {
const user = await getUserByEmail(authSession.email);
if (user?.data) {
+ const sessionCookie = await setAuthSession(request, {
+ authSession,
+ });
+ const companyIdCookie = setCompanyId(authSession.companyId);
return redirect(path.to.resetPassword, {
- headers: {
- "Set-Cookie": await commitAuthSession(request, {
- authSession,
- }),
- },
+ headers: [
+ ["Set-Cookie", sessionCookie],
+ ["Set-Cookie", companyIdCookie],
+ ],
});
} else {
return redirect(
diff --git a/apps/carbon/app/routes/_public+/login.tsx b/apps/carbon/app/routes/_public+/login.tsx
index 4bd4119318..a46d06d2ab 100644
--- a/apps/carbon/app/routes/_public+/login.tsx
+++ b/apps/carbon/app/routes/_public+/login.tsx
@@ -5,7 +5,8 @@ import {
safeRedirect,
} from "@carbon/auth";
import { signInWithEmail, verifyAuthSession } from "@carbon/auth/auth.server";
-import { commitAuthSession, getAuthSession } from "@carbon/auth/session.server";
+import { setCompanyId } from "@carbon/auth/company.server";
+import { getAuthSession, setAuthSession } from "@carbon/auth/session.server";
import { ValidatedForm, validationError, validator } from "@carbon/form";
import {
Alert,
@@ -25,6 +26,7 @@ import posthog from "posthog-js";
import { LuAlertCircle } from "react-icons/lu";
import { Hidden, Input, Password, Submit } from "~/components/Form";
+
import type { FormActionData, Result } from "~/types";
import { path } from "~/utils/path";
@@ -63,12 +65,16 @@ export async function action({ request }: ActionFunctionArgs): FormActionData {
});
}
+ const sessionCookie = await setAuthSession(request, {
+ authSession,
+ });
+ const companyIdCookie = setCompanyId(authSession.companyId);
+
throw redirect(safeRedirect(redirectTo), {
- headers: {
- "Set-Cookie": await commitAuthSession(request, {
- authSession,
- }),
- },
+ headers: [
+ ["Set-Cookie", sessionCookie],
+ ["Set-Cookie", companyIdCookie],
+ ],
});
}
diff --git a/apps/carbon/app/routes/_public+/refresh-session.tsx b/apps/carbon/app/routes/_public+/refresh-session.tsx
index 9b512a3289..d3eed92acd 100644
--- a/apps/carbon/app/routes/_public+/refresh-session.tsx
+++ b/apps/carbon/app/routes/_public+/refresh-session.tsx
@@ -1,7 +1,8 @@
import { assertIsPost } from "@carbon/auth";
+import { setCompanyId } from "@carbon/auth/company.server";
import {
- commitAuthSession,
refreshAuthSession,
+ setAuthSession,
} from "@carbon/auth/session.server";
import { useNavigate } from "@remix-run/react";
import type { ActionFunctionArgs } from "@vercel/remix";
@@ -18,14 +19,18 @@ export async function action({ request }: ActionFunctionArgs) {
const authSession = await refreshAuthSession(request);
+ const sessionCookie = await setAuthSession(request, {
+ authSession,
+ });
+ const companyIdCookie = setCompanyId(authSession.companyId);
+
return json(
{ success: true },
{
- headers: {
- "Set-Cookie": await commitAuthSession(request, {
- authSession,
- }),
- },
+ headers: [
+ ["Set-Cookie", sessionCookie],
+ ["Set-Cookie", companyIdCookie],
+ ],
}
);
}
diff --git a/apps/carbon/app/routes/api+/countries.ts b/apps/carbon/app/routes/api+/countries.ts
index a3efb5dfa1..9a6d3e0c5a 100644
--- a/apps/carbon/app/routes/api+/countries.ts
+++ b/apps/carbon/app/routes/api+/countries.ts
@@ -1,15 +1,9 @@
import { requirePermissions } from "@carbon/auth/auth.server";
import type { ClientLoaderFunctionArgs } from "@remix-run/react";
-import { queryOptions } from "@tanstack/react-query";
import type { LoaderFunctionArgs, SerializeFrom } from "@vercel/remix";
import { json } from "@vercel/remix";
import { getCountries } from "~/modules/shared";
-import { queryClient } from "~/utils/react-query";
-
-const countriesQuery = () =>
- queryOptions({
- queryKey: ["countries"],
- });
+import { countriesQuery, queryClient } from "~/utils/react-query";
export async function loader({ request }: LoaderFunctionArgs) {
const { client } = await requirePermissions(request, {});
diff --git a/apps/carbon/app/routes/api+/items.uoms.ts b/apps/carbon/app/routes/api+/items.uoms.ts
index 4d1c718e69..de69da0359 100644
--- a/apps/carbon/app/routes/api+/items.uoms.ts
+++ b/apps/carbon/app/routes/api+/items.uoms.ts
@@ -1,10 +1,37 @@
import { requirePermissions } from "@carbon/auth/auth.server";
-import type { LoaderFunctionArgs } from "@vercel/remix";
+import type { ClientLoaderFunctionArgs } from "@remix-run/react";
+import type { LoaderFunctionArgs, SerializeFrom } from "@vercel/remix";
import { json } from "@vercel/remix";
import { getUnitOfMeasuresList } from "~/modules/items";
+import { getCompanyId, queryClient, uomsQuery } from "~/utils/react-query";
export async function loader({ request }: LoaderFunctionArgs) {
const { client, companyId } = await requirePermissions(request, {});
return json(await getUnitOfMeasuresList(client, companyId));
}
+
+export async function clientLoader({
+ serverLoader,
+ params,
+}: ClientLoaderFunctionArgs) {
+ const companyId = getCompanyId();
+ console.log("companyId", companyId);
+ if (!companyId) {
+ return await serverLoader<typeof loader>();
+ }
+
+ const query = uomsQuery(companyId);
+ const data = queryClient.getQueryData<SerializeFrom<typeof loader>>(
+ query.queryKey
+ );
+
+ if (!data) {
+ const serverData = await serverLoader<typeof loader>();
+ queryClient.setQueryData(query.queryKey, serverData);
+ return serverData;
+ }
+
+ return data;
+}
+clientLoader.hydrate = true;
diff --git a/apps/carbon/app/routes/onboarding+/company.tsx b/apps/carbon/app/routes/onboarding+/company.tsx
index 9191d37dde..bb6e2a30cd 100644
--- a/apps/carbon/app/routes/onboarding+/company.tsx
+++ b/apps/carbon/app/routes/onboarding+/company.tsx
@@ -1,5 +1,6 @@
import { assertIsPost, getCarbonServiceRole } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
+import { setCompanyId } from "@carbon/auth/company.server";
import { updateCompanySession } from "@carbon/auth/session.server";
import { ValidatedForm, validationError, validator } from "@carbon/form";
import {
@@ -149,10 +150,14 @@ export async function action({ request }: ActionFunctionArgs) {
}
}
+ const sessionCookie = await updateCompanySession(request, companyId!);
+ const companyIdCookie = setCompanyId(companyId!);
+
throw redirect(next, {
- headers: {
- "Set-Cookie": await updateCompanySession(request, companyId!),
- },
+ headers: [
+ ["Set-Cookie", sessionCookie],
+ ["Set-Cookie", companyIdCookie],
+ ],
});
}
diff --git a/apps/carbon/app/routes/x+/settings+/company.new.tsx b/apps/carbon/app/routes/x+/settings+/company.new.tsx
index 3629d32876..5e4c63624f 100644
--- a/apps/carbon/app/routes/x+/settings+/company.new.tsx
+++ b/apps/carbon/app/routes/x+/settings+/company.new.tsx
@@ -1,5 +1,6 @@
import { assertIsPost, getCarbonServiceRole } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
+import { setCompanyId } from "@carbon/auth/company.server";
import { updateCompanySession } from "@carbon/auth/session.server";
import { validationError, validator } from "@carbon/form";
import { redis } from "@carbon/kv";
@@ -82,9 +83,13 @@ export async function action({ request }: ActionFunctionArgs) {
throw new Error("Fatal: failed to insert job");
}
+ const sessionCookie = await updateCompanySession(request, companyId);
+ const companyIdCookie = setCompanyId(companyId);
+
throw redirect(path.to.authenticatedRoot, {
- headers: {
- "Set-Cookie": await updateCompanySession(request, companyId),
- },
+ headers: [
+ ["Set-Cookie", sessionCookie],
+ ["Set-Cookie", companyIdCookie],
+ ],
});
}
diff --git a/apps/carbon/app/routes/x+/settings+/company.switch.$companyId.tsx b/apps/carbon/app/routes/x+/settings+/company.switch.$companyId.tsx
index e2aba5f878..88ac8cddb6 100644
--- a/apps/carbon/app/routes/x+/settings+/company.switch.$companyId.tsx
+++ b/apps/carbon/app/routes/x+/settings+/company.switch.$companyId.tsx
@@ -1,5 +1,6 @@
import { error } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
+import { setCompanyId } from "@carbon/auth/company.server";
import {
destroyAuthSession,
flash,
@@ -32,9 +33,13 @@ export async function action({ request, params }: ActionFunctionArgs) {
await destroyAuthSession(request);
}
+ const sessionCookie = await updateCompanySession(request, companyId!);
+ const companyIdCookie = setCompanyId(companyId!);
+
throw redirect(path.to.authenticatedRoot, {
- headers: {
- "Set-Cookie": await updateCompanySession(request, companyId!),
- },
+ headers: [
+ ["Set-Cookie", sessionCookie],
+ ["Set-Cookie", companyIdCookie],
+ ],
});
}
diff --git a/apps/carbon/app/utils/react-query.ts b/apps/carbon/app/utils/react-query.ts
index a3d02c029b..01f0e9f0f4 100644
--- a/apps/carbon/app/utils/react-query.ts
+++ b/apps/carbon/app/utils/react-query.ts
@@ -1,4 +1,23 @@
import { remember } from "@epic-web/remember";
-import { QueryClient } from "@tanstack/react-query";
+import { QueryClient, queryOptions } from "@tanstack/react-query";
+import * as cookie from "cookie";
export const queryClient = remember("react-query", () => new QueryClient());
+
+export const getCompanyId = () => {
+ const cookieHeader = document.cookie;
+ const parsed = cookieHeader ? cookie.parse(cookieHeader)["companyId"] : null;
+ return parsed;
+};
+
+export const countriesQuery = () =>
+ queryOptions({
+ queryKey: ["countries"],
+ staleTime: Infinity,
+ });
+
+export const uomsQuery = (companyId: string) =>
+ queryOptions({
+ queryKey: ["uoms", companyId],
+ staleTime: Infinity,
+ });
diff --git a/apps/developers/app/routes/_public+/callback.tsx b/apps/developers/app/routes/_public+/callback.tsx
index ca5a95bc32..9cedf89972 100644
--- a/apps/developers/app/routes/_public+/callback.tsx
+++ b/apps/developers/app/routes/_public+/callback.tsx
@@ -6,11 +6,12 @@ import {
getCarbonServiceRole,
} from "@carbon/auth";
import { refreshAccessToken } from "@carbon/auth/auth.server";
+import { setCompanyId } from "@carbon/auth/company.server";
import {
- commitAuthSession,
destroyAuthSession,
flash,
getAuthSession,
+ setAuthSession,
} from "@carbon/auth/session.server";
import { validator } from "@carbon/form";
import { useFetcher } from "@remix-run/react";
@@ -63,12 +64,16 @@ export async function action({ request }: ActionFunctionArgs) {
);
}
+ const sessionCookie = await setAuthSession(request, {
+ authSession,
+ });
+ const companyIdCookie = setCompanyId(authSession.companyId);
+
return redirect(path.to.authenticatedRoot, {
- headers: {
- "Set-Cookie": await commitAuthSession(request, {
- authSession,
- }),
- },
+ headers: [
+ ["Set-Cookie", sessionCookie],
+ ["Set-Cookie", companyIdCookie],
+ ],
});
}
diff --git a/apps/developers/app/routes/_public+/login.tsx b/apps/developers/app/routes/_public+/login.tsx
index c8bbeb778f..a9660d35c5 100644
--- a/apps/developers/app/routes/_public+/login.tsx
+++ b/apps/developers/app/routes/_public+/login.tsx
@@ -6,7 +6,8 @@ import {
safeRedirect,
} from "@carbon/auth";
import { signInWithEmail, verifyAuthSession } from "@carbon/auth/auth.server";
-import { commitAuthSession, getAuthSession } from "@carbon/auth/session.server";
+import { setCompanyId } from "@carbon/auth/company.server";
+import { getAuthSession, setAuthSession } from "@carbon/auth/session.server";
import {
Hidden,
Input,
@@ -70,12 +71,16 @@ export async function action({ request }: ActionFunctionArgs) {
});
}
+ const sessionCookie = await setAuthSession(request, {
+ authSession,
+ });
+ const companyIdCookie = setCompanyId(authSession.companyId);
+
throw redirect(safeRedirect(redirectTo), {
- headers: {
- "Set-Cookie": await commitAuthSession(request, {
- authSession,
- }),
- },
+ headers: [
+ ["Set-Cookie", sessionCookie],
+ ["Set-Cookie", companyIdCookie],
+ ],
});
}
diff --git a/apps/developers/app/routes/_public+/refresh-session.tsx b/apps/developers/app/routes/_public+/refresh-session.tsx
index 9b512a3289..d3eed92acd 100644
--- a/apps/developers/app/routes/_public+/refresh-session.tsx
+++ b/apps/developers/app/routes/_public+/refresh-session.tsx
@@ -1,7 +1,8 @@
import { assertIsPost } from "@carbon/auth";
+import { setCompanyId } from "@carbon/auth/company.server";
import {
- commitAuthSession,
refreshAuthSession,
+ setAuthSession,
} from "@carbon/auth/session.server";
import { useNavigate } from "@remix-run/react";
import type { ActionFunctionArgs } from "@vercel/remix";
@@ -18,14 +19,18 @@ export async function action({ request }: ActionFunctionArgs) {
const authSession = await refreshAuthSession(request);
+ const sessionCookie = await setAuthSession(request, {
+ authSession,
+ });
+ const companyIdCookie = setCompanyId(authSession.companyId);
+
return json(
{ success: true },
{
- headers: {
- "Set-Cookie": await commitAuthSession(request, {
- authSession,
- }),
- },
+ headers: [
+ ["Set-Cookie", sessionCookie],
+ ["Set-Cookie", companyIdCookie],
+ ],
}
);
}
diff --git a/apps/developers/app/routes/x+/company.switch.$companyId.tsx b/apps/developers/app/routes/x+/company.switch.$companyId.tsx
index 81d6ff1e8b..2448144578 100644
--- a/apps/developers/app/routes/x+/company.switch.$companyId.tsx
+++ b/apps/developers/app/routes/x+/company.switch.$companyId.tsx
@@ -1,5 +1,6 @@
import { error, getCompanies } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
+import { setCompanyId } from "@carbon/auth/company.server";
import {
destroyAuthSession,
flash,
@@ -32,9 +33,13 @@ export async function action({ request, params }: ActionFunctionArgs) {
await destroyAuthSession(request);
}
+ const sessionCookie = await updateCompanySession(request, companyId!);
+ const companyIdCookie = setCompanyId(companyId!);
+
throw redirect(path.to.authenticatedRoot, {
- headers: {
- "Set-Cookie": await updateCompanySession(request, companyId!),
- },
+ headers: [
+ ["Set-Cookie", sessionCookie],
+ ["Set-Cookie", companyIdCookie],
+ ],
});
}
diff --git a/apps/mes/app/routes/_public+/callback.tsx b/apps/mes/app/routes/_public+/callback.tsx
index ca5a95bc32..9cedf89972 100644
--- a/apps/mes/app/routes/_public+/callback.tsx
+++ b/apps/mes/app/routes/_public+/callback.tsx
@@ -6,11 +6,12 @@ import {
getCarbonServiceRole,
} from "@carbon/auth";
import { refreshAccessToken } from "@carbon/auth/auth.server";
+import { setCompanyId } from "@carbon/auth/company.server";
import {
- commitAuthSession,
destroyAuthSession,
flash,
getAuthSession,
+ setAuthSession,
} from "@carbon/auth/session.server";
import { validator } from "@carbon/form";
import { useFetcher } from "@remix-run/react";
@@ -63,12 +64,16 @@ export async function action({ request }: ActionFunctionArgs) {
);
}
+ const sessionCookie = await setAuthSession(request, {
+ authSession,
+ });
+ const companyIdCookie = setCompanyId(authSession.companyId);
+
return redirect(path.to.authenticatedRoot, {
- headers: {
- "Set-Cookie": await commitAuthSession(request, {
- authSession,
- }),
- },
+ headers: [
+ ["Set-Cookie", sessionCookie],
+ ["Set-Cookie", companyIdCookie],
+ ],
});
}
diff --git a/apps/mes/app/routes/_public+/login.tsx b/apps/mes/app/routes/_public+/login.tsx
index 4e1675640a..584448ca43 100644
--- a/apps/mes/app/routes/_public+/login.tsx
+++ b/apps/mes/app/routes/_public+/login.tsx
@@ -6,7 +6,8 @@ import {
safeRedirect,
} from "@carbon/auth";
import { signInWithEmail, verifyAuthSession } from "@carbon/auth/auth.server";
-import { commitAuthSession, getAuthSession } from "@carbon/auth/session.server";
+import { setCompanyId } from "@carbon/auth/company.server";
+import { getAuthSession, setAuthSession } from "@carbon/auth/session.server";
import {
Hidden,
Input,
@@ -70,12 +71,16 @@ export async function action({ request }: ActionFunctionArgs) {
});
}
+ const sessionCookie = await setAuthSession(request, {
+ authSession,
+ });
+ const companyIdCookie = setCompanyId(authSession.companyId);
+
throw redirect(safeRedirect(redirectTo), {
- headers: {
- "Set-Cookie": await commitAuthSession(request, {
- authSession,
- }),
- },
+ headers: [
+ ["Set-Cookie", sessionCookie],
+ ["Set-Cookie", companyIdCookie],
+ ],
});
}
diff --git a/apps/mes/app/routes/_public+/refresh-session.tsx b/apps/mes/app/routes/_public+/refresh-session.tsx
index 9b512a3289..d3eed92acd 100644
--- a/apps/mes/app/routes/_public+/refresh-session.tsx
+++ b/apps/mes/app/routes/_public+/refresh-session.tsx
@@ -1,7 +1,8 @@
import { assertIsPost } from "@carbon/auth";
+import { setCompanyId } from "@carbon/auth/company.server";
import {
- commitAuthSession,
refreshAuthSession,
+ setAuthSession,
} from "@carbon/auth/session.server";
import { useNavigate } from "@remix-run/react";
import type { ActionFunctionArgs } from "@vercel/remix";
@@ -18,14 +19,18 @@ export async function action({ request }: ActionFunctionArgs) {
const authSession = await refreshAuthSession(request);
+ const sessionCookie = await setAuthSession(request, {
+ authSession,
+ });
+ const companyIdCookie = setCompanyId(authSession.companyId);
+
return json(
{ success: true },
{
- headers: {
- "Set-Cookie": await commitAuthSession(request, {
- authSession,
- }),
- },
+ headers: [
+ ["Set-Cookie", sessionCookie],
+ ["Set-Cookie", companyIdCookie],
+ ],
}
);
}
diff --git a/apps/mes/app/routes/x+/company.switch.$companyId.tsx b/apps/mes/app/routes/x+/company.switch.$companyId.tsx
index 21015377ae..72da1a99b7 100644
--- a/apps/mes/app/routes/x+/company.switch.$companyId.tsx
+++ b/apps/mes/app/routes/x+/company.switch.$companyId.tsx
@@ -1,5 +1,6 @@
import { error, getCompanies } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
+import { setCompanyId } from "@carbon/auth/company.server";
import {
destroyAuthSession,
flash,
@@ -33,27 +34,31 @@ export async function action({ request, params }: ActionFunctionArgs) {
}
const sessionCookie = await updateCompanySession(request, companyId!);
+ const companyIdCookie = setCompanyId(companyId!);
const storedLocations = await getLocation(request, client, {
userId,
companyId: companyId!,
});
if (storedLocations.updated) {
- const workCenterLocationCookie = await setLocation(
+ const locationCookie = await setLocation(
companyId!,
storedLocations.location
);
throw redirect(path.to.authenticatedRoot, {
- headers: {
- "Set-Cookie": `${sessionCookie} ${workCenterLocationCookie}`,
- },
+ headers: [
+ ["Set-Cookie", sessionCookie],
+ ["Set-Cookie", companyIdCookie],
+ ["Set-Cookie", locationCookie],
+ ],
});
}
throw redirect(path.to.authenticatedRoot, {
- headers: {
- "Set-Cookie": sessionCookie,
- },
+ headers: [
+ ["Set-Cookie", sessionCookie],
+ ["Set-Cookie", companyIdCookie],
+ ],
});
}
diff --git a/packages/auth/package.json b/packages/auth/package.json
index b92e5a7b3f..1c04bfb206 100644
--- a/packages/auth/package.json
+++ b/packages/auth/package.json
@@ -16,6 +16,7 @@
".": "./src/index.ts",
"./auth.server": "./src/services/auth.server.ts",
"./autodesk.server": "./src/services/autodesk.server.ts",
+ "./company.server": "./src/services/company.server.ts",
"./session.server": "./src/services/session.server.ts",
"./users.server": "./src/services/users.server.ts"
},
diff --git a/packages/auth/src/services/company.server.ts b/packages/auth/src/services/company.server.ts
new file mode 100644
index 0000000000..6d282c89d7
--- /dev/null
+++ b/packages/auth/src/services/company.server.ts
@@ -0,0 +1,20 @@
+import { DOMAIN } from "@carbon/auth";
+import * as cookie from "cookie";
+
+const cookieName = "companyId";
+
+export function setCompanyId(companyId: string | null) {
+ if (!companyId) {
+ return cookie.serialize(cookieName, "", {
+ path: "/",
+ expires: new Date(0),
+ domain: DOMAIN,
+ });
+ }
+
+ return cookie.serialize(cookieName, companyId, {
+ path: "/",
+ maxAge: 31536000, // 1 year
+ domain: DOMAIN,
+ });
+}
diff --git a/packages/auth/src/services/session.server.ts b/packages/auth/src/services/session.server.ts
index 692079839f..cd08e5bc64 100644
--- a/packages/auth/src/services/session.server.ts
+++ b/packages/auth/src/services/session.server.ts
@@ -13,6 +13,7 @@ import type { AuthSession, Result } from "../types";
import { getCurrentPath, isGet, makeRedirectToFromHere } from "../utils/http";
import { path } from "../utils/path";
import { refreshAccessToken, verifyAuthSession } from "./auth.server";
+import { setCompanyId } from "./company.server";
import { getPermissionCacheKey } from "./users";
async function assertAuthSession(
@@ -42,7 +43,7 @@ const sessionStorage = createCookieSessionStorage({
},
});
-export async function commitAuthSession(
+export async function setAuthSession(
request: Request,
{
authSession,
@@ -62,10 +63,14 @@ export async function commitAuthSession(
export async function destroyAuthSession(request: Request) {
const session = await getSession(request);
+ const sessionCookie = await sessionStorage.destroySession(session);
+ const companyIdCookie = setCompanyId(null);
+
return redirect(path.to.login, {
- headers: {
- "Set-Cookie": await sessionStorage.destroySession(session),
- },
+ headers: [
+ ["Set-Cookie", sessionCookie],
+ ["Set-Cookie", companyIdCookie],
+ ],
});
}
@@ -148,22 +153,30 @@ export async function refreshAuthSession(
if (!refreshedAuthSession) {
const redirectUrl = `${path.to.login}?${makeRedirectToFromHere(request)}`;
+ const sessionCookie = await setAuthSession(request, {
+ authSession: null,
+ });
+ const companyIdCookie = setCompanyId(null);
+
throw redirect(redirectUrl, {
- headers: {
- "Set-Cookie": await commitAuthSession(request, {
- authSession: null,
- }),
- },
+ headers: [
+ ["Set-Cookie", sessionCookie],
+ ["Set-Cookie", companyIdCookie],
+ ],
});
}
if (isGet(request)) {
+ const sessionCookie = await setAuthSession(request, {
+ authSession: refreshedAuthSession,
+ });
+ const companyIdCookie = setCompanyId(refreshedAuthSession.companyId);
+
throw redirect(getCurrentPath(request), {
- headers: {
- "Set-Cookie": await commitAuthSession(request, {
- authSession: refreshedAuthSession,
- }),
- },
+ headers: [
+ ["Set-Cookie", sessionCookie],
+ ["Set-Cookie", companyIdCookie],
+ ],
});
}