Files

132 lines
3.9 KiB
TypeScript

import type { DashboardType } from "@/types/auth";
export type HostApp = "admin" | "customer" | "business";
const BUSINESS_PREFIX = "business.";
const CUSTOMER_PREFIX = "customer.";
/** Host without port, lowercased. */
export function normalizeHost(hostHeader: string | null | undefined): string {
if (!hostHeader) return "";
return hostHeader.split(":")[0]?.toLowerCase() ?? "";
}
export function isBusinessHost(hostHeader: string | null | undefined): boolean {
const host = normalizeHost(hostHeader);
if (!host) return false;
if (host === "business.localhost") return true;
return host.startsWith(BUSINESS_PREFIX);
}
export function isCustomerHost(hostHeader: string | null | undefined): boolean {
const host = normalizeHost(hostHeader);
if (!host) return false;
if (host === "customer.localhost") return true;
return host.startsWith(CUSTOMER_PREFIX);
}
export function hostAppFor(hostHeader: string | null | undefined): HostApp {
if (isBusinessHost(hostHeader)) return "business";
if (isCustomerHost(hostHeader)) return "customer";
return "admin";
}
/** @deprecated Prefer hostAppFor + access checks. */
export function expectedDashboardForHost(
hostHeader: string | null | undefined,
): DashboardType {
const app = hostAppFor(hostHeader);
if (app === "business") return "business";
if (app === "customer") return "customer";
return "super_admin";
}
function originFromEnv(
envValue: string | undefined,
fallbackLocal: string,
fallbackProd: (root: string) => string,
): string {
const fromEnv = envValue?.replace(/\/$/, "");
if (fromEnv) {
if (fromEnv.startsWith("http://") || fromEnv.startsWith("https://")) {
return fromEnv;
}
const protocol =
fromEnv.includes("localhost") || fromEnv.startsWith("127.")
? "http"
: "https";
return `${protocol}://${fromEnv}`;
}
const root = process.env.NEXT_PUBLIC_ROOT_DOMAIN || "localhost";
if (root === "localhost" || root.startsWith("127.")) {
return fallbackLocal;
}
return fallbackProd(root);
}
/** Absolute origin for the business dashboard (no trailing slash). */
export function getBusinessDashboardOrigin(): string {
return originFromEnv(
process.env.NEXT_PUBLIC_BUSINESS_DASHBOARD_HOST,
"http://business.localhost:3002",
(root) => `https://business.${root}`,
);
}
/** Absolute origin for the customer dashboard. */
export function getCustomerDashboardOrigin(): string {
return originFromEnv(
process.env.NEXT_PUBLIC_CUSTOMER_DASHBOARD_HOST,
"http://customer.localhost:3002",
(root) => `https://customer.${root}`,
);
}
/** Absolute origin for the superadmin dashboard. */
export function getAdminDashboardOrigin(): string {
return originFromEnv(
process.env.NEXT_PUBLIC_ADMIN_DASHBOARD_HOST,
"http://localhost:3002",
(root) => `https://admin.${root}`,
);
}
/** Map public business URL path → internal /business/* path. */
export function rewriteBusinessPath(pathname: string): string | null {
if (pathname === "/") return "/business";
if (pathname === "/login") return "/login";
if (pathname === "/forgot-password") return "/forgot-password";
if (pathname === "/auth/handoff") return "/auth/handoff";
const known = [
"/profile",
"/products",
"/blog",
"/portfolios",
"/gallery",
] as const;
for (const path of known) {
if (pathname === path || pathname.startsWith(`${path}/`)) {
return `/business${pathname}`;
}
}
return null;
}
/** Map public customer URL path → internal /customer/* path. */
export function rewriteCustomerPath(pathname: string): string | null {
if (pathname === "/") return "/customer";
if (pathname === "/login") return "/login";
if (pathname === "/forgot-password") return "/forgot-password";
if (pathname === "/auth/handoff") return "/auth/handoff";
if (pathname === "/profile" || pathname.startsWith("/profile/")) {
return `/customer${pathname}`;
}
return null;
}