refactor: extract typed authentication API
This commit is contained in:
@@ -137,48 +137,73 @@
|
||||
bridge.decoderRegistry = decoderRegistry;
|
||||
bridge.onDecoderRegistryReady = onDecoderRegistryReady;
|
||||
|
||||
// src/api/auth.ts
|
||||
function decodeAuthSession(value) {
|
||||
if (typeof value !== "object" || value === null) {
|
||||
throw new TypeError("The authentication response is malformed");
|
||||
}
|
||||
const session = value;
|
||||
if (typeof session.authenticated !== "boolean") {
|
||||
throw new TypeError("The authentication response has no authenticated flag");
|
||||
}
|
||||
if (session.role !== void 0 && session.role !== "rx" && session.role !== "control") {
|
||||
throw new TypeError("The authentication response has an invalid role");
|
||||
}
|
||||
if (session.auth_disabled !== void 0 && typeof session.auth_disabled !== "boolean") {
|
||||
throw new TypeError("The authentication response has an invalid auth_disabled flag");
|
||||
}
|
||||
const decoded = { authenticated: session.authenticated };
|
||||
if (session.role !== void 0) decoded.role = session.role;
|
||||
if (session.auth_disabled !== void 0) decoded.auth_disabled = session.auth_disabled;
|
||||
return decoded;
|
||||
}
|
||||
var authDisabledSession = {
|
||||
authenticated: true,
|
||||
role: "control",
|
||||
auth_disabled: true
|
||||
};
|
||||
async function fetchAuthSession() {
|
||||
try {
|
||||
const response = await fetch("/auth/session");
|
||||
if (response.status === 404) return authDisabledSession;
|
||||
if (!response.ok) return { authenticated: false };
|
||||
return decodeAuthSession(await response.json());
|
||||
} catch (error) {
|
||||
console.error("Auth check failed:", error);
|
||||
return { authenticated: false };
|
||||
}
|
||||
}
|
||||
async function login(passphrase) {
|
||||
const response = await fetch("/auth/login", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ passphrase })
|
||||
});
|
||||
if (response.status === 404) return authDisabledSession;
|
||||
if (!response.ok) {
|
||||
const message = await response.text();
|
||||
throw new Error(message || "Login failed");
|
||||
}
|
||||
return decodeAuthSession(await response.json());
|
||||
}
|
||||
async function logout() {
|
||||
const response = await fetch("/auth/logout", { method: "POST" });
|
||||
if (response.status !== 404 && !response.ok) throw new Error("Logout failed");
|
||||
}
|
||||
|
||||
// src/app.js
|
||||
void loadDecoderRegistry(refreshOperatorLayoutCapabilities);
|
||||
var authRole = null;
|
||||
var authEnabled = true;
|
||||
async function checkAuthStatus() {
|
||||
try {
|
||||
const resp = await fetch("/auth/session");
|
||||
if (resp.status === 404) {
|
||||
return { authenticated: true, role: "control", auth_disabled: true };
|
||||
}
|
||||
if (!resp.ok) return { authenticated: false };
|
||||
const data = await resp.json();
|
||||
return data;
|
||||
} catch (e) {
|
||||
console.error("Auth check failed:", e);
|
||||
return { authenticated: false };
|
||||
}
|
||||
return fetchAuthSession();
|
||||
}
|
||||
async function authLogin(passphrase) {
|
||||
try {
|
||||
const resp = await fetch("/auth/login", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ passphrase })
|
||||
});
|
||||
if (resp.status === 404) {
|
||||
return { authenticated: true, role: "control", auth_disabled: true };
|
||||
}
|
||||
if (!resp.ok) {
|
||||
const text = await resp.text();
|
||||
throw new Error(text || "Login failed");
|
||||
}
|
||||
const data = await resp.json();
|
||||
return data;
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
return login(passphrase);
|
||||
}
|
||||
async function authLogout() {
|
||||
try {
|
||||
const resp = await fetch("/auth/logout", { method: "POST" });
|
||||
if (resp.status !== 404 && !resp.ok) throw new Error("Logout failed");
|
||||
await logout();
|
||||
authRole = null;
|
||||
disconnect();
|
||||
setDecodeHistoryOverlayVisible(false);
|
||||
|
||||
Reference in New Issue
Block a user