feat: add demo auth flow

This commit is contained in:
3252a8
2026-05-28 16:40:16 +03:00
parent cd469ae2bb
commit da28b69461
10 changed files with 220 additions and 16 deletions
+2
View File
@@ -16,5 +16,7 @@
/demo/runtime/support/* /demo/runtime/app.html 200
/demo/runtime/settings /demo/runtime/app.html 200
/demo/runtime/settings/* /demo/runtime/app.html 200
/demo/runtime/login /demo/runtime/app.html 200
/demo/runtime/login/* /demo/runtime/app.html 200
/demo/runtime/admin /demo/runtime/app.html 200
/demo/runtime/admin/* /demo/runtime/app.html 200
+34 -11
View File
@@ -8,6 +8,7 @@ const stateMocks = new Set([
"no-subscription",
"trial",
"devices",
"auth",
]);
const routeMocks = new Set([...stateMocks, "guides", "install"]);
const params = new URLSearchParams(window.location.search);
@@ -20,7 +21,9 @@ const normalizePath = (value) => {
};
const normalizeRouteMock = (value) => {
const mock = String(value || "").trim().toLowerCase();
const mock = String(value || "")
.trim()
.toLowerCase();
return routeMocks.has(mock) ? mock : defaultMock;
};
@@ -36,7 +39,8 @@ const routeFromPublicPath = () => {
if (!lowerPathname.startsWith(`${demoBase}/`)) return "";
const publicRoute = pathname.slice(demoBase.length);
if (!publicRoute || publicRoute.toLowerCase().startsWith("/runtime")) return "";
if (!publicRoute || publicRoute.toLowerCase().startsWith("/runtime"))
return "";
return normalizePath(publicRoute);
};
@@ -65,6 +69,7 @@ const routeFromParams = () => {
"devices",
"support",
"settings",
"login",
].includes(screen)
) {
return `/${screen}`;
@@ -73,7 +78,13 @@ const routeFromParams = () => {
};
let initialRoute = routeFromParams();
const mockForRoute = (route) => (normalizePath(route) === "/devices" ? "devices" : "");
const mockForRoute = (route) => {
const normalized = normalizePath(route);
if (normalized === "/devices") return "devices";
if (normalized === "/login" || normalized.startsWith("/login/"))
return "auth";
return "";
};
const initialMock = params.has("mock")
? normalizeRouteMock(params.get("mock"))
: normalizeRouteMock(mockForRoute(initialRoute) || defaultMock);
@@ -84,7 +95,7 @@ if (initialMock === "trial" && initialRoute === "/trial") {
window.history.replaceState(
null,
"",
`${normalizedUrl.pathname}${normalizedUrl.search}${normalizedUrl.hash}`
`${normalizedUrl.pathname}${normalizedUrl.search}${normalizedUrl.hash}`,
);
}
params.set("mock", initialMock);
@@ -96,8 +107,11 @@ frame.src = `${runtimeBase}/app.html?${params.toString()}${window.location.hash
const routeFromRuntimeUrl = (url) => {
if (url.origin !== window.location.origin) return "";
if (!url.pathname.toLowerCase().startsWith(runtimeBase.toLowerCase())) return "";
const runtimePath = normalizePath(url.pathname.slice(runtimeBase.length) || "/home");
if (!url.pathname.toLowerCase().startsWith(runtimeBase.toLowerCase()))
return "";
const runtimePath = normalizePath(
url.pathname.slice(runtimeBase.length) || "/home",
);
if (runtimePath === "/app.html") {
return normalizePath(url.searchParams.get("path") || "/home");
}
@@ -108,14 +122,20 @@ const materializedRouteFromRuntime = (route) => {
const normalized = normalizePath(route);
if (/^\/admin\/users\/-?\d+$/i.test(normalized)) return "/admin/users";
if (/^\/admin\/payments\/\d+$/i.test(normalized)) return "/admin/payments";
if (/^\/admin\/payments\/users\/-?\d+$/i.test(normalized)) return "/admin/payments";
if (/^\/admin\/payments\/users\/-?\d+$/i.test(normalized))
return "/admin/payments";
if (/^\/admin\/support\/\d+$/i.test(normalized)) return "/admin/support";
if (/^\/support\/\d+$/i.test(normalized)) return "/support";
return normalized;
};
const publicPathFromRoute = (route) => `${demoBase}${materializedRouteFromRuntime(route)}`;
const routeForStateMock = (mock) => (mock === "devices" ? "/devices" : "/home");
const publicPathFromRoute = (route) =>
`${demoBase}${materializedRouteFromRuntime(route)}`;
const routeForStateMock = (mock) => {
if (mock === "devices") return "/devices";
if (mock === "auth") return "/login";
return "/home";
};
const runtimeSrc = (route, searchParams = new URLSearchParams()) => {
const nextParams = new URLSearchParams(searchParams);
nextParams.delete("screen");
@@ -138,7 +158,9 @@ const syncParentUrlFromFrame = () => {
nextUrl.pathname = publicPathFromRoute(route);
nextUrl.searchParams.delete("path");
const mock = normalizeRouteMock(frameUrl.searchParams.get("mock") || params.get("mock"));
const mock = normalizeRouteMock(
frameUrl.searchParams.get("mock") || params.get("mock"),
);
if (mock === defaultMock) nextUrl.searchParams.delete("mock");
else nextUrl.searchParams.set("mock", mock);
if (stateSelect) stateSelect.value = normalizeStateMock(mock);
@@ -147,7 +169,8 @@ const syncParentUrlFromFrame = () => {
nextUrl.searchParams.delete("admin_section");
const nextPath = `${nextUrl.pathname}${nextUrl.search}${nextUrl.hash}`;
const currentPath = `${window.location.pathname}${window.location.search}${window.location.hash}`;
if (nextPath !== currentPath) window.history.replaceState(null, "", nextPath);
if (nextPath !== currentPath)
window.history.replaceState(null, "", nextPath);
} catch (_error) {
// The iframe is same-origin in docs builds; this keeps local oddities harmless.
}
@@ -13,6 +13,8 @@ const userRoutes = [
"devices",
"support",
"settings",
"login",
"login/password",
];
const adminRoutes = [
+1
View File
@@ -294,6 +294,7 @@ const docsHref = '/getting-started/demo/';
<option value="no-subscription">Нет подписки</option>
<option value="trial">Доступна пробная подписка</option>
<option value="devices">Лимит и докупка устройств</option>
<option value="auth">Вход и регистрация</option>
</select>
</label>
<div class="demo-topbar__actions">
+11 -1
View File
@@ -2,7 +2,17 @@
import DemoShell from '../demo.astro';
export function getStaticPaths() {
const userRoutes = ['home', 'install', 'trial', 'invite', 'devices', 'support', 'settings'];
const userRoutes = [
'home',
'install',
'trial',
'invite',
'devices',
'support',
'settings',
'login',
'login/password',
];
const adminRoutes = [
'stats',
'users',