analytica-frontend-lib
Version:
Repositório público dos componentes utilizados nas plataformas da Analytica Ensino
85 lines (84 loc) • 2.51 kB
JavaScript
// src/utils/domainUtils.ts
var resolveRootHostname = (hostname) => {
if (hostname === "localhost") {
return null;
}
const isIPv4 = /^\d{1,3}(?:\.\d{1,3}){3}$/.test(hostname);
const isIPv6 = hostname.includes(":");
if (isIPv4 || isIPv6) {
return null;
}
const parts = hostname.split(".");
const isHml = parts.some(
(label) => label === "hml" || label.startsWith("hml-")
);
if (parts.length >= 3 && parts[parts.length - 2] === "com" && parts[parts.length - 1] === "br") {
if (parts.length === 3) {
return hostname;
}
const base = parts.slice(-3).join(".");
return isHml && !base.startsWith("hml.") ? `hml.${base}` : base;
}
if (parts.length > 2) {
const base = parts.slice(-2).join(".");
return isHml && !base.startsWith("hml.") ? `hml.${base}` : base;
}
return hostname;
};
var extractSubdomainSlug = (hostname) => {
if (resolveRootHostname(hostname) === null) {
return null;
}
if (resolveRootHostname(hostname) === hostname) {
return null;
}
const firstLabel = hostname.split(".")[0];
return firstLabel.startsWith("hml-") ? firstLabel.slice("hml-".length) : firstLabel;
};
var buildLoginUrlWithReturnTo = (rootDomain) => {
if (typeof window === "undefined") {
return rootDomain;
}
if (isExplicitLogoutActive()) {
return rootDomain;
}
const { hostname, pathname, search } = window.location;
if (resolveRootHostname(hostname) === null) {
return rootDomain;
}
if (pathname === "/" && !search) {
return rootDomain;
}
const returnTo = encodeURIComponent(window.location.href);
return `${rootDomain}?returnTo=${returnTo}`;
};
var EXPLICIT_LOGOUT_KEY = "@auth:explicit-logout";
var EXPLICIT_LOGOUT_WINDOW_MS = 5e3;
var markExplicitLogout = () => {
if (typeof window === "undefined") return;
try {
sessionStorage.setItem(EXPLICIT_LOGOUT_KEY, String(Date.now()));
} catch {
}
};
var isExplicitLogoutActive = () => {
if (typeof window === "undefined") return false;
try {
const raw = sessionStorage.getItem(EXPLICIT_LOGOUT_KEY);
if (!raw) return false;
const ts = Number(raw);
if (!Number.isFinite(ts)) return false;
const active = Date.now() - ts < EXPLICIT_LOGOUT_WINDOW_MS;
if (!active) sessionStorage.removeItem(EXPLICIT_LOGOUT_KEY);
return active;
} catch {
return false;
}
};
export {
resolveRootHostname,
extractSubdomainSlug,
buildLoginUrlWithReturnTo,
markExplicitLogout
};
//# sourceMappingURL=chunk-RLGKQUBF.mjs.map