wcz-layout
Version:
131 lines (130 loc) • 4.52 kB
JavaScript
import { n as getUser } from "./user-Bc2nLjAG.mjs";
import { manifest, permissions } from "virtual:wcz-layout";
import { redirect } from "@tanstack/react-router";
// src/lib/utils.ts
const WISTRON_PRIMARY_COLOR = "#00506E";
const WISTRON_SECONDARY_COLOR = "#64DC00";
var Platform = class {
static get isAndroid() {
return /android/i.test(this.userAgent);
}
static get isIOS() {
return /iPad|iPhone|iPod/.test(this.userAgent);
}
static get isWindows() {
return /windows/i.test(this.userAgent);
}
static get isMacOS() {
return /Macintosh|MacIntel|MacPPC|Mac68K/.test(this.userAgent);
}
static get userAgent() {
return typeof navigator === "undefined" ? "" : navigator.userAgent;
}
};
const rootRouteHead = (options) => ({
meta: [
{ charSet: "utf-8" },
{
name: "viewport",
content: "width=device-width, initial-scale=1"
},
{ title: manifest.name },
{
name: "og:type",
content: "website"
},
{
name: "og:title",
content: manifest.name
},
{
name: "og:image",
content: "/favicon-32x32.png"
}
],
links: [
{
rel: "apple-touch-icon",
sizes: "180x180",
href: "/apple-touch-icon.png"
},
{
rel: "icon",
type: "image/png",
sizes: "32x32",
href: "/favicon-32x32.png"
},
{
rel: "icon",
type: "image/png",
sizes: "16x16",
href: "/favicon-16x16.png"
},
{
rel: "manifest",
href: options?.manifest || "/manifest.json"
},
{
rel: "icon",
href: "/favicon.ico"
}
]
});
/**
* Route guard for `beforeLoad`. Routes are public by default; add this to close one.
*
* - `requireAuth()` — signed in with any provider. The only gate a population
* without permission groups (e.g. AWS users) can pass.
* - `requireAuth("admin")` — signed in *and* in a group listed under that key.
*
* `reloadDocument` is required: `/auth/login` is a component-less server route and
* must be reached with a full-document navigation. It starts the login directly when
* only one provider is registered, otherwise it forwards to the `/login` chooser.
*/
const requireAuth = (permissionKey) => {
return async ({ location, context }) => {
const user = await getUser({ queryClient: context.queryClient });
if (!user) throw redirect({
href: `/auth/login?returnTo=${encodeURIComponent(location.href)}`,
reloadDocument: true
});
if (permissionKey && !hasPermission(user, permissionKey)) throw new Error("You do not have permission to access this page.");
return { user };
};
};
const NON_PRODUCTION = /(?:^|[.-])(?:dev|qas|uat|test)(?:[.-]|$)/i;
/** True only for a dev/qas/uat/test domain — localhost and production are not. */
const isTestEnv = (origin) => NON_PRODUCTION.test((origin ?? "").split(".").slice(1).join("."));
const getFieldStatus = (field) => {
const { meta } = field.state;
return {
isTouched: meta.isTouched,
hasError: !!meta.errors.length,
helperText: meta.errors[0]?.message
};
};
/**
* Only same-origin paths are allowed, so `returnTo` cannot become an open
* redirect. Backslash counts as a separator too: browsers normalize `/\evil`
* to the protocol-relative `//evil`.
*/
const sanitizeReturnTo = (value) => value && /^\/(?![/\\])/.test(value) ? value : "/";
const foldForSearch = (value) => String(value ?? "").toLowerCase().normalize("NFD").replaceAll(/[\u{300}-\u{36F}]/gu, "").normalize("NFC");
const permissionGroups = new Set(Object.values(permissions).flat());
/**
* Maps id/access-token claims to the serializable `User`. Groups are trimmed to
* those named in `permissions.ts` — an unfiltered group list would blow the
* cookie. Cognito's claim names differ, so both are read.
*/
const buildUser = (payload, provider) => ({
provider,
name: (payload.name ?? payload["cognito:username"] ?? "").split("/")[0],
email: (payload.preferred_username ?? payload.email ?? "").toLowerCase(),
department: payload.department?.toUpperCase() || "",
employeeId: (payload.employeeId ?? payload["cognito:username"] ?? "").toUpperCase(),
companyName: payload.companyName || "",
groups: (payload.groups ?? payload["cognito:groups"])?.filter((group) => permissionGroups.has(group)) ?? []
});
const hasPermission = (user, key) => user ? permissions[key].some((group) => user.groups.includes(group)) : false;
export { foldForSearch as a, isTestEnv as c, sanitizeReturnTo as d, buildUser as i, requireAuth as l, WISTRON_PRIMARY_COLOR as n, getFieldStatus as o, WISTRON_SECONDARY_COLOR as r, hasPermission as s, Platform as t, rootRouteHead as u };
//# sourceMappingURL=utils-CgabUlDD.mjs.map