81 lines
2.8 KiB
TypeScript
81 lines
2.8 KiB
TypeScript
const ACCESS_TOKEN_KEY = "marketplace_access_token";
|
|
const REFRESH_TOKEN_KEY = "marketplace_refresh_token";
|
|
const TOKEN_MAX_AGE_SEC = 60 * 60 * 24 * 14;
|
|
|
|
function cookieDomain(): string {
|
|
const root = (process.env.NEXT_PUBLIC_ROOT_DOMAIN || "localhost").trim();
|
|
if (!root || root === "localhost" || root.startsWith("127.")) {
|
|
return "localhost";
|
|
}
|
|
return root.startsWith(".") ? root : `.${root}`;
|
|
}
|
|
|
|
function readCookie(name: string): string | null {
|
|
if (typeof document === "undefined") return null;
|
|
const prefix = `${name}=`;
|
|
for (const part of document.cookie.split(";")) {
|
|
const trimmed = part.trim();
|
|
if (trimmed.startsWith(prefix)) {
|
|
return decodeURIComponent(trimmed.slice(prefix.length));
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function writeCookie(name: string, value: string) {
|
|
if (typeof document === "undefined") return;
|
|
const secure =
|
|
typeof window !== "undefined" && window.location.protocol === "https:"
|
|
? "; Secure"
|
|
: "";
|
|
document.cookie = `${name}=${encodeURIComponent(value)}; Path=/; Max-Age=${TOKEN_MAX_AGE_SEC}; SameSite=Lax; Domain=${cookieDomain()}${secure}`;
|
|
}
|
|
|
|
export function getAccessToken() {
|
|
if (typeof window === "undefined") return null;
|
|
return readCookie(ACCESS_TOKEN_KEY) || localStorage.getItem(ACCESS_TOKEN_KEY);
|
|
}
|
|
|
|
export function getRefreshToken() {
|
|
if (typeof window === "undefined") return null;
|
|
return (
|
|
readCookie(REFRESH_TOKEN_KEY) || localStorage.getItem(REFRESH_TOKEN_KEY)
|
|
);
|
|
}
|
|
|
|
export function setTokens(accessToken: string, refreshToken: string) {
|
|
localStorage.setItem(ACCESS_TOKEN_KEY, accessToken);
|
|
localStorage.setItem(REFRESH_TOKEN_KEY, refreshToken);
|
|
writeCookie(ACCESS_TOKEN_KEY, accessToken);
|
|
writeCookie(REFRESH_TOKEN_KEY, refreshToken);
|
|
}
|
|
|
|
export function clearTokens() {
|
|
localStorage.removeItem(ACCESS_TOKEN_KEY);
|
|
localStorage.removeItem(REFRESH_TOKEN_KEY);
|
|
if (typeof document === "undefined") return;
|
|
const domain = cookieDomain();
|
|
document.cookie = `${ACCESS_TOKEN_KEY}=; Path=/; Max-Age=0; SameSite=Lax; Domain=${domain}`;
|
|
document.cookie = `${REFRESH_TOKEN_KEY}=; Path=/; Max-Age=0; SameSite=Lax; Domain=${domain}`;
|
|
}
|
|
|
|
export function getWebsiteOrigin() {
|
|
return (
|
|
process.env.NEXT_PUBLIC_WEBSITE_URL?.replace(/\/$/, "") ||
|
|
(typeof window !== "undefined" ? window.location.origin : "http://localhost:3001")
|
|
);
|
|
}
|
|
|
|
export function getCustomerLoginUrl(returnPath = "/register") {
|
|
const customer =
|
|
process.env.NEXT_PUBLIC_CUSTOMER_DASHBOARD_URL?.replace(/\/$/, "") ||
|
|
"http://customer.localhost:3002";
|
|
const website = getWebsiteOrigin();
|
|
const next = returnPath.startsWith("http")
|
|
? returnPath
|
|
: `${website}${returnPath.startsWith("/") ? returnPath : `/${returnPath}`}`;
|
|
const url = new URL(`${customer}/login`);
|
|
url.searchParams.set("next", next);
|
|
return url.toString();
|
|
}
|