feat(auth): 接入 Casdoor 登录并让前端持有本服务会话
管理台可以密码登录、按角色进入,并把 Watchman 会话 token 交给浏览器。HXOPS 真实跳转仍未联调。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
21338337a1
commit
ab31d828d4
64 files changed
+4140
-339
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import { flushPromises, mount } from "@vue/test-utils";
|
||||
import { createPinia } from "pinia";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { createI18n } from "vue-i18n";
|
||||
import { BusinessError } from "@watchman/api";
|
||||
@@ -18,6 +19,21 @@ import { requestJson } from "@watchman/api";
|
||||
|
||||
const mockedRequestJson = vi.mocked(requestJson);
|
||||
|
||||
const adminSession = {
|
||||
subject: "admin-1",
|
||||
display_name: "Admin",
|
||||
roles: ["watchman-admin"],
|
||||
};
|
||||
|
||||
function mockSessionThenHealth(health: () => Promise<unknown> | unknown) {
|
||||
mockedRequestJson.mockImplementation((input: string) => {
|
||||
if (String(input).includes("/auth/me")) {
|
||||
return Promise.resolve(adminSession);
|
||||
}
|
||||
return Promise.resolve(health());
|
||||
});
|
||||
}
|
||||
|
||||
function mountApp(locale = "zh-CN") {
|
||||
const i18n = createI18n({
|
||||
legacy: false,
|
||||
@@ -31,7 +47,7 @@ function mountApp(locale = "zh-CN") {
|
||||
});
|
||||
return mount(App, {
|
||||
global: {
|
||||
plugins: [i18n],
|
||||
plugins: [i18n, createPinia()],
|
||||
stubs: {
|
||||
ElConfigProvider: { template: "<div><slot /></div>" },
|
||||
ElCard: {
|
||||
@@ -54,29 +70,33 @@ function mountApp(locale = "zh-CN") {
|
||||
|
||||
describe("App health three states", () => {
|
||||
beforeEach(() => {
|
||||
sessionStorage.clear();
|
||||
mockedRequestJson.mockReset();
|
||||
});
|
||||
|
||||
it("shows loading before the request settles", async () => {
|
||||
let resolveRequest: (value: unknown) => void = () => undefined;
|
||||
mockedRequestJson.mockImplementation(
|
||||
() =>
|
||||
new Promise((resolve) => {
|
||||
resolveRequest = resolve;
|
||||
}),
|
||||
);
|
||||
it("shows loading before the health request settles", async () => {
|
||||
let resolveHealth: (value: unknown) => void = () => undefined;
|
||||
mockedRequestJson.mockImplementation((input: string) => {
|
||||
if (String(input).includes("/auth/me")) {
|
||||
return Promise.resolve(adminSession);
|
||||
}
|
||||
return new Promise((resolve) => {
|
||||
resolveHealth = resolve;
|
||||
});
|
||||
});
|
||||
const wrapper = mountApp();
|
||||
await flushPromises();
|
||||
expect(wrapper.get("[data-testid=health-loading]").text()).toContain("健康检查加载中");
|
||||
resolveRequest({ status: "ok", app: "watchman", version: "0.1.0" });
|
||||
resolveHealth({ status: "ok", app: "watchman", version: "0.1.0" });
|
||||
await flushPromises();
|
||||
});
|
||||
|
||||
it("shows success after a healthy response", async () => {
|
||||
mockedRequestJson.mockResolvedValue({
|
||||
mockSessionThenHealth(() => ({
|
||||
status: "ok",
|
||||
app: "watchman",
|
||||
version: "0.1.0",
|
||||
});
|
||||
}));
|
||||
const wrapper = mountApp();
|
||||
await flushPromises();
|
||||
expect(wrapper.get("[data-testid=health-tag]").text()).toContain("健康检查成功");
|
||||
@@ -84,14 +104,19 @@ describe("App health three states", () => {
|
||||
});
|
||||
|
||||
it("shows failure after a BusinessError", async () => {
|
||||
mockedRequestJson.mockRejectedValue(
|
||||
new BusinessError({
|
||||
code: 50000,
|
||||
message: "Internal server error",
|
||||
status: 500,
|
||||
requestId: "req-fail",
|
||||
}),
|
||||
);
|
||||
mockedRequestJson.mockImplementation((input: string) => {
|
||||
if (String(input).includes("/auth/me")) {
|
||||
return Promise.resolve(adminSession);
|
||||
}
|
||||
return Promise.reject(
|
||||
new BusinessError({
|
||||
code: 50000,
|
||||
message: "Internal server error",
|
||||
status: 500,
|
||||
requestId: "req-fail",
|
||||
}),
|
||||
);
|
||||
});
|
||||
const wrapper = mountApp();
|
||||
await flushPromises();
|
||||
expect(wrapper.get("[data-testid=health-tag]").text()).toContain("健康检查失败");
|
||||
@@ -99,25 +124,36 @@ describe("App health three states", () => {
|
||||
});
|
||||
|
||||
it("falls back to zh-CN when locale messages are missing", async () => {
|
||||
mockedRequestJson.mockResolvedValue({
|
||||
mockSessionThenHealth(() => ({
|
||||
status: "ok",
|
||||
app: "watchman",
|
||||
version: "0.1.0",
|
||||
});
|
||||
}));
|
||||
const wrapper = mountApp("th");
|
||||
await flushPromises();
|
||||
expect(wrapper.get("[data-testid=health-tag]").text()).toContain("健康检查成功");
|
||||
});
|
||||
|
||||
it("shows english health copy when locale is en", async () => {
|
||||
mockedRequestJson.mockResolvedValue({
|
||||
mockSessionThenHealth(() => ({
|
||||
status: "ok",
|
||||
app: "watchman",
|
||||
version: "0.1.0",
|
||||
});
|
||||
}));
|
||||
const wrapper = mountApp("en");
|
||||
await flushPromises();
|
||||
expect(wrapper.get("[data-testid=health-tag]").text()).toContain("Health check succeeded");
|
||||
expect(wrapper.get("[data-testid=health-success]").text()).toContain("Service available");
|
||||
});
|
||||
|
||||
it("shows the no-access screen when the session has no Watchman role", async () => {
|
||||
mockedRequestJson.mockResolvedValue({
|
||||
subject: "user-1",
|
||||
display_name: "No Role",
|
||||
roles: [],
|
||||
});
|
||||
const wrapper = mountApp();
|
||||
await flushPromises();
|
||||
expect(wrapper.text()).toContain("没有访问权限");
|
||||
});
|
||||
});
|
||||
@@ -1,18 +1,22 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, ref } from "vue";
|
||||
import { useI18n } from "vue-i18n";
|
||||
import { ElAlert, ElCard, ElConfigProvider, ElTag } from "element-plus";
|
||||
import { ElAlert, ElButton, ElCard, ElConfigProvider, ElTag } from "element-plus";
|
||||
import zhCn from "element-plus/es/locale/lang/zh-cn";
|
||||
import zhTw from "element-plus/es/locale/lang/zh-tw";
|
||||
import en from "element-plus/es/locale/lang/en";
|
||||
import th from "element-plus/es/locale/lang/th";
|
||||
import { requestJson } from "@watchman/api";
|
||||
import { BusinessError } from "@watchman/api";
|
||||
import { api } from "./api";
|
||||
import {
|
||||
healthFailureView,
|
||||
healthSuccessView,
|
||||
type HealthPayload,
|
||||
type HealthState,
|
||||
} from "./health";
|
||||
import LoginPanel from "./LoginPanel.vue";
|
||||
import { allowsEnter, loadSession, type SessionUser } from "./session";
|
||||
import { useAuthStore } from "./stores/auth";
|
||||
|
||||
const { t, locale } = useI18n();
|
||||
const elementLocale = computed(() => {
|
||||
@@ -28,12 +32,34 @@ const elementLocale = computed(() => {
|
||||
}
|
||||
});
|
||||
|
||||
type Screen = "booting" | "login" | "forbidden" | "shell";
|
||||
|
||||
const auth = useAuthStore();
|
||||
const screen = ref<Screen>("booting");
|
||||
const identity = ref<SessionUser | null>(null);
|
||||
const state = ref<HealthState>("loading");
|
||||
const detail = ref("");
|
||||
|
||||
onMounted(async () => {
|
||||
function applyIdentity(user: SessionUser): void {
|
||||
const stored = loadSession();
|
||||
const next = {
|
||||
...user,
|
||||
access_token: user.access_token ?? stored?.access_token,
|
||||
expires_in: user.expires_in ?? stored?.expires_in,
|
||||
token_expires_at: user.token_expires_at ?? stored?.token_expires_at,
|
||||
};
|
||||
auth.login(next);
|
||||
identity.value = auth.user;
|
||||
screen.value = allowsEnter(next.roles) ? "shell" : "forbidden";
|
||||
if (screen.value === "shell") {
|
||||
void loadHealth();
|
||||
}
|
||||
}
|
||||
|
||||
async function loadHealth(): Promise<void> {
|
||||
state.value = "loading";
|
||||
try {
|
||||
const data = await requestJson<HealthPayload>("/health");
|
||||
const data = await api<HealthPayload>("/health");
|
||||
const view = healthSuccessView(data);
|
||||
state.value = view.state;
|
||||
detail.value = view.detail;
|
||||
@@ -42,20 +68,65 @@ onMounted(async () => {
|
||||
state.value = view.state;
|
||||
detail.value = view.detail;
|
||||
}
|
||||
}
|
||||
|
||||
async function signOut(): Promise<void> {
|
||||
try {
|
||||
await api("/auth/logout", { method: "POST" });
|
||||
} catch {
|
||||
// The local screen still returns to the login form.
|
||||
}
|
||||
auth.logout();
|
||||
identity.value = null;
|
||||
screen.value = "login";
|
||||
}
|
||||
|
||||
onMounted(async () => {
|
||||
try {
|
||||
applyIdentity(await api<SessionUser>("/auth/me"));
|
||||
} catch (error) {
|
||||
if (error instanceof BusinessError && error.status !== 401) {
|
||||
detail.value = String(error.code);
|
||||
}
|
||||
screen.value = "login";
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<ElConfigProvider :locale="elementLocale">
|
||||
<main class="min-h-screen p-8 bg-[var(--bg-surface-soft)] text-[length:var(--font-size-base)] text-[var(--text-primary)]">
|
||||
<p v-if="screen === 'booting'" data-testid="session-loading">{{ t("health.loading") }}</p>
|
||||
<LoginPanel v-else-if="screen === 'login'" @authenticated="applyIdentity" />
|
||||
<main
|
||||
v-else-if="screen === 'forbidden'"
|
||||
class="flex min-h-screen items-center justify-center bg-[var(--bg-surface-soft)] p-6"
|
||||
>
|
||||
<section class="w-full max-w-2xl rounded-3xl bg-white p-8 shadow-xl">
|
||||
<ElAlert
|
||||
type="warning"
|
||||
data-testid="forbidden"
|
||||
:title="t('login.forbiddenTitle')"
|
||||
:description="t('login.forbiddenBody')"
|
||||
show-icon
|
||||
:closable="false"
|
||||
/>
|
||||
<ElButton class="mt-6" data-testid="logout" @click="signOut">{{ t("login.logout") }}</ElButton>
|
||||
</section>
|
||||
</main>
|
||||
<main
|
||||
v-else
|
||||
class="min-h-screen bg-[var(--bg-surface-soft)] p-8 text-[length:var(--font-size-base)] text-[var(--text-primary)]"
|
||||
>
|
||||
<div class="mb-4 flex items-center justify-between gap-4">
|
||||
<p data-testid="shell-welcome">{{ t("shell.welcome", { name: identity?.display_name }) }}</p>
|
||||
<ElButton data-testid="logout" @click="signOut">{{ t("login.logout") }}</ElButton>
|
||||
</div>
|
||||
<ElCard>
|
||||
<template #header>
|
||||
<div class="flex items-center justify-between gap-4">
|
||||
<span>{{ t("appName") }}</span>
|
||||
<span data-testid="health-tag">
|
||||
<ElTag
|
||||
:type="state === 'loading' ? 'info' : state === 'success' ? 'success' : 'danger'"
|
||||
>
|
||||
<ElTag :type="state === 'loading' ? 'info' : state === 'success' ? 'success' : 'danger'">
|
||||
{{
|
||||
state === "loading"
|
||||
? t("health.loading")
|
||||
|
||||
@@ -0,0 +1,310 @@
|
||||
<script setup lang="ts">
|
||||
import { ref } from "vue";
|
||||
import { useI18n } from "vue-i18n";
|
||||
import { ElAlert } from "element-plus";
|
||||
import { BusinessError } from "@watchman/api";
|
||||
import { api } from "./api";
|
||||
import type { SessionUser } from "./session";
|
||||
import logoUrl from "./assets/logo.jpg";
|
||||
|
||||
const emit = defineEmits<{
|
||||
authenticated: [user: SessionUser];
|
||||
}>();
|
||||
|
||||
const { t, locale } = useI18n();
|
||||
const locales = ["zh-CN", "zh-TW", "en", "th"] as const;
|
||||
const localeLabel: Record<(typeof locales)[number], string> = {
|
||||
"zh-CN": "中文",
|
||||
"zh-TW": "繁體",
|
||||
en: "EN",
|
||||
th: "ไทย",
|
||||
};
|
||||
const theme = ref<"light" | "dark">(
|
||||
localStorage.getItem("watchman.theme") === "dark" ? "dark" : "light",
|
||||
);
|
||||
const username = ref("");
|
||||
const password = ref("");
|
||||
const showPassword = ref(false);
|
||||
const submitting = ref(false);
|
||||
const errorKey = ref("");
|
||||
const fixtureMode = import.meta.env.VITE_AUTH_MODE === "fixture";
|
||||
|
||||
async function signIn(): Promise<void> {
|
||||
errorKey.value = "";
|
||||
submitting.value = true;
|
||||
try {
|
||||
const user = await api<SessionUser>("/auth/login", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ username: username.value, password: password.value }),
|
||||
});
|
||||
password.value = "";
|
||||
emit("authenticated", user);
|
||||
} catch (error) {
|
||||
const unavailable = !(error instanceof BusinessError) || error.status === 503 || error.status >= 500;
|
||||
errorKey.value = unavailable ? "login.unavailable" : "login.failed";
|
||||
} finally {
|
||||
submitting.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
function toggleTheme(): void {
|
||||
theme.value = theme.value === "light" ? "dark" : "light";
|
||||
localStorage.setItem("watchman.theme", theme.value);
|
||||
}
|
||||
|
||||
function cycleLocale(): void {
|
||||
const index = locales.indexOf(locale.value as (typeof locales)[number]);
|
||||
const next = locales[(index + 1) % locales.length];
|
||||
locale.value = next;
|
||||
localStorage.setItem("watchman.lang", next);
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="pulse" :data-theme="theme">
|
||||
<div class="stage" aria-hidden="true">
|
||||
<div class="sky-glow sg1"></div>
|
||||
<div class="sky-glow sg2"></div>
|
||||
<svg class="arcs" viewBox="0 0 1440 320" preserveAspectRatio="xMidYMax slice">
|
||||
<path class="arc dim" d="M70 218 Q 170 130 300 178" />
|
||||
<path class="arc" d="M300 178 Q 400 96 520 148" />
|
||||
<path class="arc dim" d="M520 148 Q 610 60 760 92" />
|
||||
<path class="arc" d="M760 92 Q 880 46 990 128" />
|
||||
<path class="arc dim" d="M990 128 Q 1120 60 1250 158" />
|
||||
<path class="arc" d="M1250 158 Q 1330 110 1400 168" />
|
||||
<circle class="nd" cx="300" cy="178" r="3.2" />
|
||||
<circle class="nd" cx="760" cy="92" r="3.2" />
|
||||
<circle class="nd" cx="1250" cy="158" r="3.2" />
|
||||
</svg>
|
||||
<svg class="skyline" viewBox="0 0 1440 260" preserveAspectRatio="xMidYMax slice">
|
||||
<defs>
|
||||
<linearGradient id="watchmanSil" x1="0" y1="0" x2="0" y2="1">
|
||||
<stop offset="0" stop-color="var(--skyline-a)" />
|
||||
<stop offset="1" stop-color="var(--skyline-b)" />
|
||||
</linearGradient>
|
||||
</defs>
|
||||
<path
|
||||
class="sil"
|
||||
d="M0 260 V196 H70 V120 h42 V196 H250 V158 h82 V196 H360 V128 h84 V196 H580 V88 h72 V196 H770 V110 h210 V196 H1190 V104 h76 V196 H1440 V260 Z"
|
||||
/>
|
||||
</svg>
|
||||
<div class="groundline"></div>
|
||||
</div>
|
||||
|
||||
<header class="topbar">
|
||||
<div class="logo-mini">
|
||||
<img class="logo-img" :src="logoUrl" alt="" />
|
||||
<span>{{ t("appName") }}</span>
|
||||
</div>
|
||||
<div class="tools">
|
||||
<button class="tool-btn" type="button" :aria-label="t('login.theme')" @click="toggleTheme">
|
||||
{{ theme === "light" ? "☾" : "☀" }}
|
||||
</button>
|
||||
<button class="tool-btn" type="button" data-testid="login-lang" @click="cycleLocale">
|
||||
{{ localeLabel[locale as (typeof locales)[number]] ?? "中文" }}
|
||||
</button>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<main class="frame">
|
||||
<div class="hero">
|
||||
<span class="cap">{{ t("login.eyebrow") }}</span>
|
||||
<h1>
|
||||
{{ t("login.heroBefore") }}<em>{{ t("login.heroEm") }}</em>
|
||||
</h1>
|
||||
<p>{{ t("login.subtitle") }}</p>
|
||||
</div>
|
||||
|
||||
<form class="card" @submit.prevent="signIn">
|
||||
<div class="card-head">
|
||||
<img class="lg" :src="logoUrl" alt="" />
|
||||
<div>
|
||||
<div class="tt">{{ t("login.cardTitle") }}</div>
|
||||
<div class="ss">{{ t("login.subtitle") }}</div>
|
||||
</div>
|
||||
</div>
|
||||
<ElAlert
|
||||
v-if="fixtureMode"
|
||||
class="mb-3"
|
||||
type="warning"
|
||||
data-testid="local-identity"
|
||||
:title="t('login.fixture')"
|
||||
show-icon
|
||||
:closable="false"
|
||||
/>
|
||||
<ElAlert
|
||||
v-if="errorKey"
|
||||
class="mb-3"
|
||||
type="error"
|
||||
data-testid="login-error"
|
||||
:title="t(errorKey)"
|
||||
show-icon
|
||||
:closable="false"
|
||||
/>
|
||||
<div class="field">
|
||||
<label for="watchman-user">{{ t("login.account") }}</label>
|
||||
<div class="control" data-testid="login-username">
|
||||
<input
|
||||
id="watchman-user"
|
||||
v-model="username"
|
||||
name="username"
|
||||
type="text"
|
||||
autocomplete="username"
|
||||
:placeholder="t('login.accountPlaceholder')"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div class="field">
|
||||
<label for="watchman-pass">{{ t("login.password") }}</label>
|
||||
<div class="control" data-testid="login-password">
|
||||
<input
|
||||
id="watchman-pass"
|
||||
v-model="password"
|
||||
name="current-password"
|
||||
:type="showPassword ? 'text' : 'password'"
|
||||
autocomplete="current-password"
|
||||
:placeholder="t('login.passwordPlaceholder')"
|
||||
@keyup.enter="signIn"
|
||||
/>
|
||||
<button type="button" class="eye" :aria-label="showPassword ? 'hide' : 'show'" @click="showPassword = !showPassword">
|
||||
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.7">
|
||||
<path d="M2.5 12S6 5.8 12 5.8 21.5 12 21.5 12 18 18.2 12 18.2 2.5 12 2.5 12Z" />
|
||||
<circle cx="12" cy="12" r="2.8" />
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<button class="btn-primary" type="submit" data-testid="login-submit" :disabled="submitting">
|
||||
<span>{{ submitting ? t("login.submitting") : t("login.submit") }}</span>
|
||||
</button>
|
||||
</form>
|
||||
</main>
|
||||
<footer class="foot">{{ t("login.footer") }}</footer>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.pulse {
|
||||
--brand-600: #2563eb;
|
||||
--brand-400: #60a5fa;
|
||||
--bg: #eef3f9;
|
||||
--sky-hi: rgba(37, 99, 235, 0.1);
|
||||
--sky-lo: rgba(34, 211, 238, 0.06);
|
||||
--skyline-a: #243244;
|
||||
--skyline-b: #182231;
|
||||
--arc: #2563eb;
|
||||
--arc-dim: rgba(37, 99, 235, 0.3);
|
||||
--node: #0891b2;
|
||||
--text-hi: #0f172a;
|
||||
--text-mid: #475569;
|
||||
--text-low: #94a3b8;
|
||||
--card-bg: rgba(255, 255, 255, 0.86);
|
||||
--card-border: rgba(15, 23, 42, 0.08);
|
||||
--field-bg: #ffffff;
|
||||
--field-border: #cbd5e1;
|
||||
--tool-bg: rgba(255, 255, 255, 0.8);
|
||||
position: fixed;
|
||||
inset: 0;
|
||||
overflow: hidden;
|
||||
background: var(--bg);
|
||||
color: var(--text-mid);
|
||||
font-size: 14px;
|
||||
}
|
||||
.pulse[data-theme="dark"] {
|
||||
--bg: #0a1120;
|
||||
--sky-hi: rgba(37, 99, 235, 0.16);
|
||||
--sky-lo: rgba(34, 211, 238, 0.07);
|
||||
--skyline-a: #0e1729;
|
||||
--skyline-b: #060b16;
|
||||
--arc: #60a5fa;
|
||||
--arc-dim: rgba(96, 165, 250, 0.35);
|
||||
--node: #22d3ee;
|
||||
--text-hi: #f8fafc;
|
||||
--text-mid: #a3b1c6;
|
||||
--text-low: #8b99af;
|
||||
--card-bg: rgba(13, 20, 38, 0.66);
|
||||
--card-border: rgba(255, 255, 255, 0.12);
|
||||
--field-bg: rgba(255, 255, 255, 0.05);
|
||||
--field-border: rgba(255, 255, 255, 0.14);
|
||||
--tool-bg: rgba(13, 20, 38, 0.6);
|
||||
}
|
||||
.stage { position: absolute; inset: 0; }
|
||||
.sky-glow { position: absolute; width: 900px; height: 900px; border-radius: 50%; filter: blur(90px); }
|
||||
.sg1 { left: -240px; top: -380px; background: radial-gradient(circle, var(--sky-hi), transparent 65%); }
|
||||
.sg2 { right: -260px; top: -320px; background: radial-gradient(circle, var(--sky-lo), transparent 65%); }
|
||||
.arcs { position: absolute; left: 0; right: 0; bottom: 15vh; height: 40vh; width: 100%; }
|
||||
.arc { stroke: var(--arc); stroke-width: 1.4; fill: none; stroke-dasharray: 4 8; animation: flow 5s linear infinite; }
|
||||
.arc.dim { stroke: var(--arc-dim); animation-duration: 7.5s; }
|
||||
.nd { fill: var(--node); }
|
||||
@keyframes flow { to { stroke-dashoffset: -96; } }
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.arc { animation: none; }
|
||||
}
|
||||
.skyline { position: absolute; left: 0; right: 0; bottom: 0; width: 100%; height: 28vh; min-height: 170px; }
|
||||
.sil { fill: url(#watchmanSil); stroke: rgba(148, 163, 184, 0.14); }
|
||||
.groundline {
|
||||
position: absolute; left: 0; right: 0; bottom: 0; height: 2px;
|
||||
background: linear-gradient(90deg, transparent, rgba(96, 165, 250, 0.35) 25%, rgba(96, 165, 250, 0.35) 75%, transparent);
|
||||
}
|
||||
.topbar { position: absolute; top: 0; left: 0; right: 0; z-index: 3; display: flex; justify-content: space-between; align-items: center; padding: 22px 32px; }
|
||||
.logo-mini { display: flex; align-items: center; gap: 10px; color: var(--text-hi); font-weight: 650; }
|
||||
.logo-img { width: 36px; height: 36px; object-fit: contain; background: white; border-radius: 8px; }
|
||||
.tools { display: flex; gap: 8px; }
|
||||
.tool-btn {
|
||||
display: flex; align-items: center; justify-content: center; height: 36px; min-width: 36px; padding: 0 11px;
|
||||
border-radius: 999px; border: 1px solid var(--card-border); background: var(--tool-bg);
|
||||
color: var(--text-mid); font-size: 12.5px;
|
||||
}
|
||||
.lg { width: 36px; height: 36px; object-fit: contain; background: white; border-radius: 10px; }
|
||||
.frame {
|
||||
position: relative; z-index: 2; height: 100%;
|
||||
display: flex; flex-direction: column; align-items: center; justify-content: center;
|
||||
padding: 64px 24px 18vh;
|
||||
}
|
||||
.hero { text-align: center; margin-bottom: 28px; }
|
||||
.cap { font-size: 12px; font-weight: 600; letter-spacing: 0.28em; color: var(--brand-400); }
|
||||
.hero h1 { font-size: clamp(30px, 3.8vw, 46px); font-weight: 700; letter-spacing: -0.02em; color: var(--text-hi); margin: 14px 0 10px; }
|
||||
.hero em {
|
||||
font-style: normal;
|
||||
background: linear-gradient(100deg, #3b82f6, #22d3ee);
|
||||
-webkit-background-clip: text;
|
||||
background-clip: text;
|
||||
color: transparent;
|
||||
}
|
||||
.card {
|
||||
width: 396px; max-width: 100%; padding: 30px 32px 24px; border-radius: 18px;
|
||||
background: var(--card-bg); border: 1px solid var(--card-border);
|
||||
backdrop-filter: blur(20px) saturate(150%);
|
||||
box-shadow: 0 1px 0 rgba(255, 255, 255, 0.08) inset, 0 28px 72px -20px rgba(0, 0, 0, 0.6);
|
||||
}
|
||||
.card-head { display: flex; align-items: center; gap: 12px; margin-bottom: 20px; }
|
||||
.tt { font-size: 17px; font-weight: 650; color: var(--text-hi); }
|
||||
.ss { font-size: 12.5px; color: var(--text-low); }
|
||||
.field { margin-bottom: 14px; }
|
||||
.field label { display: block; font-size: 13px; font-weight: 500; margin-bottom: 6px; }
|
||||
.control input {
|
||||
width: 100%; height: 44px; border-radius: 10px; border: 1px solid var(--field-border);
|
||||
background: var(--field-bg); color: var(--text-hi); font-size: 14.5px; padding: 0 44px 0 14px;
|
||||
}
|
||||
.control input:focus { outline: none; border-color: #3b82f6; box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.16); }
|
||||
.control { position: relative; }
|
||||
.eye {
|
||||
position: absolute; right: 8px; top: 6px; width: 32px; height: 32px; border: 0; border-radius: 8px;
|
||||
background: transparent; color: var(--text-low); display: grid; place-items: center;
|
||||
}
|
||||
.eye svg { width: 17px; height: 17px; }
|
||||
.btn-primary {
|
||||
width: 100%; height: 44px; margin-top: 8px; border: 0; border-radius: 10px;
|
||||
background: var(--brand-600); color: white; font-size: 15px; font-weight: 600;
|
||||
}
|
||||
.btn-primary:disabled { opacity: 0.7; }
|
||||
.foot {
|
||||
position: absolute; left: 0; right: 0; bottom: 14px; z-index: 3;
|
||||
padding: 0 32px; font-size: 11.5px; color: var(--text-low);
|
||||
}
|
||||
@media (max-width: 760px) {
|
||||
.frame { justify-content: flex-start; padding-top: 88px; }
|
||||
.skyline { min-height: 120px; }
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* Console API calls attach the stored Watchman session token.
|
||||
* The token is the one returned by login, not a Casdoor credential.
|
||||
*/
|
||||
|
||||
import { requestJson } from "@watchman/api";
|
||||
import { authHeaders } from "./session";
|
||||
|
||||
export async function api<T>(input: string, init: RequestInit = {}): Promise<T> {
|
||||
const headers = new Headers(init.headers);
|
||||
for (const [key, value] of Object.entries(authHeaders())) {
|
||||
if (!headers.has(key)) {
|
||||
headers.set(key, value);
|
||||
}
|
||||
}
|
||||
return requestJson<T>(input, { ...init, headers });
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 54 KiB |
@@ -0,0 +1,24 @@
|
||||
import { beforeEach, describe, expect, it } from "vitest";
|
||||
import { authHeaders, clearSession, loadSession, saveSession } from "./session";
|
||||
|
||||
describe("session token storage", () => {
|
||||
beforeEach(() => {
|
||||
sessionStorage.clear();
|
||||
});
|
||||
|
||||
it("keeps the Watchman access token for later requests", () => {
|
||||
saveSession({
|
||||
subject: "user-1",
|
||||
display_name: "Ada",
|
||||
roles: ["watchman-admin"],
|
||||
access_token: "session-token",
|
||||
expires_in: 60,
|
||||
});
|
||||
|
||||
expect(loadSession()?.access_token).toBe("session-token");
|
||||
expect(authHeaders()).toEqual({ Authorization: "Bearer session-token" });
|
||||
clearSession();
|
||||
expect(loadSession()).toBeNull();
|
||||
expect(authHeaders()).toEqual({});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,57 @@
|
||||
/** Browser copy of the Watchman session. This is not a Casdoor token. */
|
||||
|
||||
export type SessionUser = {
|
||||
subject: string;
|
||||
display_name: string;
|
||||
login_name?: string;
|
||||
roles: string[];
|
||||
access_token?: string;
|
||||
token_type?: string;
|
||||
expires_in?: number;
|
||||
token_expires_at?: number;
|
||||
};
|
||||
|
||||
const STORAGE_KEY = "watchman_user";
|
||||
const ENTER_ROLES = new Set(["watchman-admin", "watchman-operator", "watchman-viewer"]);
|
||||
|
||||
export function allowsEnter(roles: string[]): boolean {
|
||||
return roles.some((role) => ENTER_ROLES.has(role));
|
||||
}
|
||||
|
||||
export function saveSession(user: SessionUser): void {
|
||||
const next = { ...user };
|
||||
if (next.expires_in && !next.token_expires_at) {
|
||||
next.token_expires_at = Date.now() + next.expires_in * 1000;
|
||||
}
|
||||
sessionStorage.setItem(STORAGE_KEY, JSON.stringify(next));
|
||||
}
|
||||
|
||||
export function loadSession(): SessionUser | null {
|
||||
const raw = sessionStorage.getItem(STORAGE_KEY);
|
||||
if (!raw) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
const user = JSON.parse(raw) as SessionUser;
|
||||
if (user.token_expires_at && user.token_expires_at <= Date.now()) {
|
||||
sessionStorage.removeItem(STORAGE_KEY);
|
||||
return null;
|
||||
}
|
||||
return user;
|
||||
} catch {
|
||||
sessionStorage.removeItem(STORAGE_KEY);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function clearSession(): void {
|
||||
sessionStorage.removeItem(STORAGE_KEY);
|
||||
}
|
||||
|
||||
export function authHeaders(): Record<string, string> {
|
||||
const token = loadSession()?.access_token;
|
||||
if (!token) {
|
||||
return {};
|
||||
}
|
||||
return { Authorization: `Bearer ${token}` };
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* Login state for the console. Storage stays in session.ts so requests can read it
|
||||
* without waiting for Pinia.
|
||||
*/
|
||||
|
||||
import { defineStore } from "pinia";
|
||||
import { computed, ref } from "vue";
|
||||
import { clearSession, loadSession, saveSession, type SessionUser } from "../session";
|
||||
|
||||
export const useAuthStore = defineStore("auth", () => {
|
||||
const user = ref<SessionUser | null>(loadSession());
|
||||
const token = computed(() => user.value?.access_token ?? "");
|
||||
|
||||
function login(next: SessionUser): void {
|
||||
saveSession(next);
|
||||
user.value = loadSession();
|
||||
}
|
||||
|
||||
function logout(): void {
|
||||
clearSession();
|
||||
user.value = null;
|
||||
}
|
||||
|
||||
return { user, token, login, logout };
|
||||
});
|
||||
Reference in new issue
Block a user