proxy-go/web/static/js/auth.js
wood chen e89ef02205 特性(代理):通过端口和身份验证更新来增强服务器配置和安全性。
- 更新 docker-compose.yml 文件,使用端口 3336
- 修改 Dockerfile 以暴露端口 3336
- 重构 main.go 以支持新的路由和管理端点
- 在 auth.go 中实现健壮的身份验证中间件
- 通过加强错误检查来改进指标处理。
- 添加用于安全类型转换的实用函数
- 引入请求ID生成功能,以便更好地进行追踪。
2025-02-15 08:07:28 +08:00

61 lines
1.5 KiB
JavaScript

// 检查认证状态
function checkAuth() {
const token = localStorage.getItem('token');
if (!token) {
window.location.href = '/admin/login';
return false;
}
return true;
}
// 登录函数
async function login() {
const password = document.getElementById('password').value;
try {
const response = await fetch('/admin/auth', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ password })
});
if (!response.ok) {
throw new Error('登录失败');
}
const data = await response.json();
localStorage.setItem('token', data.token);
window.location.href = '/admin/metrics';
} catch (error) {
showToast(error.message, true);
}
}
// 退出登录
function logout() {
localStorage.removeItem('token');
window.location.href = '/admin/login';
}
// 获取认证头
function getAuthHeaders() {
const token = localStorage.getItem('token');
return {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
};
}
// 显示提示消息
function showToast(message, isError = false) {
const toast = document.createElement('div');
toast.className = `toast toast-end ${isError ? 'alert alert-error' : 'alert alert-success'}`;
toast.innerHTML = `<span>${message}</span>`;
document.body.appendChild(toast);
setTimeout(() => {
toast.remove();
}, 3000);
}