add user auth

This commit is contained in:
2026-08-31 21:08:46 +08:00
parent edc0349708
commit 85266f3879
11 changed files with 569 additions and 19 deletions
+6 -2
View File
@@ -21,7 +21,11 @@
</svg>
<h2>Agent 任务台</h2>
</div>
<div style="display:flex; gap:2px;">
<div style="display:flex; gap:2px; align-items:center;">
<span id="whoami" class="hidden" style="color:rgba(255,255,255,0.85); font-size:12px; margin-right:6px; max-width:80px; overflow:hidden; text-overflow:ellipsis; white-space:nowrap;"></span>
<button class="icon-btn hidden" id="logout-btn" onclick="ui.logout()" title="退出登录">
<svg viewBox="0 0 24 24"><path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"></path><polyline points="16 17 21 12 16 7"></polyline><line x1="21" y1="12" x2="9" y2="12"></line></svg>
</button>
<button class="icon-btn" id="create-session-btn" onclick="ui.openCreate()" title="新建任务">
<svg viewBox="0 0 24 24"><line x1="12" y1="5" x2="12" y2="19"></line><line x1="5" y1="12" x2="19" y2="12"></line></svg>
</button>
@@ -111,6 +115,6 @@
<div class="toast hidden" id="toast"></div>
<!-- 发版时更新 v 参数,避免移动端浏览器缓存旧 JS -->
<script src="/js/app.js?v=20260831d"></script>
<script src="/js/app.js?v=20260831e"></script>
</body>
</html>
+16 -1
View File
@@ -30,6 +30,7 @@ async function req(method, url, body, isForm) {
opt.body = body;
}
const res = await fetch(url, opt);
if (res.status === 401) { location.href = '/login'; throw new Error('未登录'); }
if (!res.ok) {
let msg = 'HTTP ' + res.status;
try { msg = (await res.json()).error || msg; } catch (_) {}
@@ -361,7 +362,7 @@ function updatePhaseUI(d) {
// ---------- 文件 ----------
// 下载:先换一枚短时效签名链接,再 window.location.href 交给浏览器原生下载。
// (fetch+blob 在移动端浏览器会被改名 uuid.bin;原生导航下载按服务端
// Content-Disposition 保留原名,同源自动带 Basic 认证头。)
// Content-Disposition 保留原名,同源自动携带登录 cookie。)
async function downloadFile(name) {
if (!state.sessionId) return;
try {
@@ -496,8 +497,21 @@ const ui = {
loadFiles();
} catch (e) { toast('删除失败:' + e.message); }
},
logout() { location.href = '/logout'; },
};
// ---------- 登录态:显示当前用户;未启用认证(/api/me 404)则隐藏退出按钮 ----------
async function initAuth() {
try {
const me = await req('GET', '/api/me');
if (me && me.user) {
$('whoami').textContent = me.user;
$('whoami').classList.remove('hidden');
$('logout-btn').classList.remove('hidden');
}
} catch (_) { /* 未启用认证 */ }
}
// ---------- 输入框行为 ----------
document.addEventListener('input', e => {
if (e.target.id === 'msg-input') {
@@ -519,4 +533,5 @@ document.addEventListener('keydown', e => {
showList();
loadSessions();
loadModels();
initAuth();
setInterval(loadSessions, 8000);
+71
View File
@@ -0,0 +1,71 @@
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Agent 任务台 · 登录</title>
<meta name="theme-color" content="#1e3c72">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<link rel="stylesheet" href="/css/style.css">
<style>
.login-wrap {
position: fixed; inset: 0; display: flex; align-items: center; justify-content: center;
background: linear-gradient(135deg, #1e3c72, #2a5299);
}
.login-card {
background: #fff; border-radius: 14px; padding: 28px 22px 22px; width: 84%; max-width: 340px;
}
.login-card h2 { font-size: 18px; margin-bottom: 18px; text-align: center; color: #223; }
.login-card input {
display: block; width: 100%; box-sizing: border-box;
border: 1px solid #dde; border-radius: 8px; padding: 11px 12px;
font-size: 15px; margin-bottom: 12px; outline: none;
}
.login-card input:focus { border-color: #2a5299; }
.login-error { color: #d2483c; font-size: 13px; min-height: 18px; margin: 6px 0 0; text-align: center; }
</style>
</head>
<body>
<div class="login-wrap">
<form class="login-card" id="login-form" onsubmit="return doLogin(event)">
<h2>Agent 任务台</h2>
<input type="text" id="login-user" placeholder="用户名" autocomplete="username" autocapitalize="none" autocorrect="off">
<input type="password" id="login-pass" placeholder="密码" autocomplete="current-password">
<button class="primary-btn" id="login-btn" type="submit">登录</button>
<p class="login-error" id="login-error"></p>
</form>
</div>
<script>
'use strict';
async function doLogin(e) {
e.preventDefault();
const btn = document.getElementById('login-btn');
const errEl = document.getElementById('login-error');
errEl.textContent = '';
btn.disabled = true;
try {
const res = await fetch('/api/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
user: document.getElementById('login-user').value.trim(),
password: document.getElementById('login-pass').value,
}),
});
if (!res.ok) {
let msg = 'HTTP ' + res.status;
try { msg = (await res.json()).error || msg; } catch (_) {}
throw new Error(msg);
}
location.href = '/';
} catch (e2) {
errEl.textContent = e2.message || '登录失败';
btn.disabled = false;
}
return false;
}
document.getElementById('login-user').focus();
</script>
</body>
</html>