Files
edr-platform/apps/edr-freight-web/backoffice/src/auth/cookies.ts
Nathnael 7fd5616188 fix(backoffice): stop sharing the position cookie with Smart Office
Both apps wrote a cookie named `current-position-id` but stored
different ids in it — freight the `employeePositionId`, Smart Office the
`position.id`. On a shared domain each login overwrote the other's desk
selection, and the loser silently fell back to the first position.

Freight now uses `freight-current-position-id` through a small helper
that reads the old name once, so a session live across the deploy keeps
its desk, and clears it on every write.

Also mounts the position switcher in the freight dashboard header. It
only existed under /performance-management, so on every other page a
two-desk user had no way to switch and was stuck on whatever
`useAuthUser` defaulted to. It now hides below two positions rather than
showing a one-option dropdown to the single-desk majority.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-01 07:06:05 +00:00

43 lines
1.2 KiB
TypeScript

import { POSITION_COOKIE } from "@/shared/utils/positionCookie";
const DEFAULT_PATH = "/";
const SEVEN_DAYS_IN_SECONDS = 60 * 60 * 24 * 7;
export const AUTH_TOKEN_COOKIE = "auth-token";
export const REFRESH_TOKEN_COOKIE = "refresh-token";
export const AUTH_USER_COOKIE = "auth-user";
export const AUTH_COOKIE_MAX_AGE = SEVEN_DAYS_IN_SECONDS;
export const getCookie = (name: string) => {
const match = document.cookie
.split("; ")
.find((entry) => entry.startsWith(`${name}=`));
return match ? decodeURIComponent(match.split("=").slice(1).join("=")) : null;
};
export const setCookie = (
name: string,
value: string,
maxAge = AUTH_COOKIE_MAX_AGE,
) => {
document.cookie = `${name}=${encodeURIComponent(value)}; Max-Age=${maxAge}; path=${DEFAULT_PATH}; SameSite=Lax`;
};
export const clearCookie = (name: string) => {
document.cookie = `${name}=; Max-Age=0; path=${DEFAULT_PATH}`;
};
export const clearSessionCookies = () => {
[
AUTH_TOKEN_COOKIE,
REFRESH_TOKEN_COOKIE,
AUTH_USER_COOKIE,
POSITION_COOKIE,
// Pre-rename name, still cleared so a stale value cannot outlive logout.
"current-position-id",
"selected-position-id",
].forEach(clearCookie);
};