2026-05-09 23:09:13 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* First-run setup routes.
|
|
|
|
|
|
*/
|
|
|
|
|
|
import { type H3, readBody, html } from "h3";
|
|
|
|
|
|
import type { AdminDeps } from "./index.js";
|
|
|
|
|
|
import { SetupPage } from "../../web-templates/auth-pages.js";
|
|
|
|
|
|
|
|
|
|
|
|
export function registerSetupRoutes(app: H3, deps: AdminDeps): void {
|
|
|
|
|
|
app.get("/setup", () => {
|
refactor: collapse 6 non-service plugins into shared modules
BSB plugins should be actual services (own port, lifecycle, resource
ownership). Moved secrets, auth, pairing, bundle, nodered-bridge, and
cec-relay from plugin folders to shared modules under server/src/shared/.
4 BSB plugins remain: service-store, service-admin-http,
service-api-http, service-coordinator-ws.
service-admin-http now initializes secrets + auth as plain modules in
init() using the store repo from the plugin-registry singleton. No
more setSiblings() hack or inter-plugin wiring.
sec-config.yaml updated: secrets/auth config moved into
service-admin-http, pairing config into service-api-http, nodered
config into service-coordinator-ws.
2026-05-10 00:29:25 +00:00
|
|
|
|
if (deps.repo.isSetupComplete()) {
|
2026-05-09 23:09:13 +00:00
|
|
|
|
return new Response(null, { status: 302, headers: { location: "/admin/" } });
|
|
|
|
|
|
}
|
|
|
|
|
|
return html(SetupPage({}));
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
app.post("/setup", async (event) => {
|
refactor: collapse 6 non-service plugins into shared modules
BSB plugins should be actual services (own port, lifecycle, resource
ownership). Moved secrets, auth, pairing, bundle, nodered-bridge, and
cec-relay from plugin folders to shared modules under server/src/shared/.
4 BSB plugins remain: service-store, service-admin-http,
service-api-http, service-coordinator-ws.
service-admin-http now initializes secrets + auth as plain modules in
init() using the store repo from the plugin-registry singleton. No
more setSiblings() hack or inter-plugin wiring.
sec-config.yaml updated: secrets/auth config moved into
service-admin-http, pairing config into service-api-http, nodered
config into service-coordinator-ws.
2026-05-10 00:29:25 +00:00
|
|
|
|
if (deps.repo.isSetupComplete()) {
|
2026-05-09 23:09:13 +00:00
|
|
|
|
return new Response(null, { status: 302, headers: { location: "/admin/" } });
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const body = await readBody<{ username?: string; password?: string }>(event);
|
|
|
|
|
|
const username = (body?.username ?? "").trim();
|
|
|
|
|
|
const password = body?.password ?? "";
|
|
|
|
|
|
const errors: string[] = [];
|
|
|
|
|
|
|
|
|
|
|
|
if (!username || username.length < 3 || username.length > 64) {
|
|
|
|
|
|
errors.push("Username must be 3–64 characters.");
|
|
|
|
|
|
} else if (!/^[a-zA-Z0-9_-]+$/.test(username)) {
|
|
|
|
|
|
errors.push("Username may only contain letters, digits, underscore, or hyphen.");
|
|
|
|
|
|
}
|
|
|
|
|
|
if (password.length < 12) {
|
|
|
|
|
|
errors.push("Password must be at least 12 characters.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (errors.length > 0) {
|
|
|
|
|
|
return html(SetupPage({ error: errors.join(" "), username }));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const hash = await deps.auth.hashPassword(password);
|
refactor: collapse 6 non-service plugins into shared modules
BSB plugins should be actual services (own port, lifecycle, resource
ownership). Moved secrets, auth, pairing, bundle, nodered-bridge, and
cec-relay from plugin folders to shared modules under server/src/shared/.
4 BSB plugins remain: service-store, service-admin-http,
service-api-http, service-coordinator-ws.
service-admin-http now initializes secrets + auth as plain modules in
init() using the store repo from the plugin-registry singleton. No
more setSiblings() hack or inter-plugin wiring.
sec-config.yaml updated: secrets/auth config moved into
service-admin-http, pairing config into service-api-http, nodered
config into service-coordinator-ws.
2026-05-10 00:29:25 +00:00
|
|
|
|
deps.repo.createUser({ username, password_hash: hash, role: "admin" });
|
2026-05-09 23:09:13 +00:00
|
|
|
|
|
|
|
|
|
|
const clusterKey = deps.secrets.generateClusterKey();
|
|
|
|
|
|
const encryptedCluster = deps.secrets.encryptString(clusterKey, "cluster");
|
refactor: collapse 6 non-service plugins into shared modules
BSB plugins should be actual services (own port, lifecycle, resource
ownership). Moved secrets, auth, pairing, bundle, nodered-bridge, and
cec-relay from plugin folders to shared modules under server/src/shared/.
4 BSB plugins remain: service-store, service-admin-http,
service-api-http, service-coordinator-ws.
service-admin-http now initializes secrets + auth as plain modules in
init() using the store repo from the plugin-registry singleton. No
more setSiblings() hack or inter-plugin wiring.
sec-config.yaml updated: secrets/auth config moved into
service-admin-http, pairing config into service-api-http, nodered
config into service-coordinator-ws.
2026-05-10 00:29:25 +00:00
|
|
|
|
deps.repo.setSetupExtra("cluster_key_encrypted", encryptedCluster);
|
|
|
|
|
|
deps.repo.markClusterKeyProvisioned();
|
2026-05-09 23:09:13 +00:00
|
|
|
|
|
refactor: collapse 6 non-service plugins into shared modules
BSB plugins should be actual services (own port, lifecycle, resource
ownership). Moved secrets, auth, pairing, bundle, nodered-bridge, and
cec-relay from plugin folders to shared modules under server/src/shared/.
4 BSB plugins remain: service-store, service-admin-http,
service-api-http, service-coordinator-ws.
service-admin-http now initializes secrets + auth as plain modules in
init() using the store repo from the plugin-registry singleton. No
more setSiblings() hack or inter-plugin wiring.
sec-config.yaml updated: secrets/auth config moved into
service-admin-http, pairing config into service-api-http, nodered
config into service-coordinator-ws.
2026-05-10 00:29:25 +00:00
|
|
|
|
deps.repo.createDefaultDisplay();
|
|
|
|
|
|
deps.repo.markSetupComplete();
|
2026-05-09 23:09:13 +00:00
|
|
|
|
|
|
|
|
|
|
return new Response(null, {
|
|
|
|
|
|
status: 302,
|
|
|
|
|
|
headers: { location: "/auth/login?welcome=1" },
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|