修复jwt过期后需要手动确定退出的bug
Browse files- public/app.js +28 -18
public/app.js
CHANGED
|
@@ -9,6 +9,17 @@ const SCOPES = [
|
|
| 9 |
'https://www.googleapis.com/auth/experimentsandconfigs'
|
| 10 |
].join(' ');
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
function showToast(message, type = 'info', title = '') {
|
| 13 |
const icons = { success: '✅', error: '❌', warning: '⚠️', info: 'ℹ️' };
|
| 14 |
const titles = { success: '成功', error: '错误', warning: '警告', info: '提示' };
|
|
@@ -184,7 +195,7 @@ async function processOAuthCallbackModal() {
|
|
| 184 |
return;
|
| 185 |
}
|
| 186 |
|
| 187 |
-
const response = await
|
| 188 |
method: 'POST',
|
| 189 |
headers: {
|
| 190 |
'Content-Type': 'application/json',
|
|
@@ -196,7 +207,7 @@ async function processOAuthCallbackModal() {
|
|
| 196 |
const result = await response.json();
|
| 197 |
if (result.success) {
|
| 198 |
const account = result.data;
|
| 199 |
-
const addResponse = await
|
| 200 |
method: 'POST',
|
| 201 |
headers: {
|
| 202 |
'Content-Type': 'application/json',
|
|
@@ -237,7 +248,7 @@ async function addTokenFromModal() {
|
|
| 237 |
|
| 238 |
showLoading('正在添加Token...');
|
| 239 |
try {
|
| 240 |
-
const response = await
|
| 241 |
method: 'POST',
|
| 242 |
headers: {
|
| 243 |
'Content-Type': 'application/json',
|
|
@@ -280,28 +291,27 @@ function switchTab(tab) {
|
|
| 280 |
}
|
| 281 |
}
|
| 282 |
|
| 283 |
-
|
| 284 |
-
const confirmed = await showConfirm('确定要退出登录吗?', '退出确认');
|
| 285 |
-
if (!confirmed) return;
|
| 286 |
-
|
| 287 |
localStorage.removeItem('authToken');
|
| 288 |
authToken = null;
|
| 289 |
document.getElementById('loginForm').classList.remove('hidden');
|
| 290 |
document.getElementById('mainContent').classList.add('hidden');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 291 |
showToast('已退出登录', 'info');
|
| 292 |
}
|
| 293 |
|
| 294 |
async function loadTokens() {
|
| 295 |
try {
|
| 296 |
-
const response = await
|
| 297 |
headers: { 'Authorization': `Bearer ${authToken}` }
|
| 298 |
});
|
| 299 |
|
| 300 |
-
if (response.status === 401) {
|
| 301 |
-
logout();
|
| 302 |
-
return;
|
| 303 |
-
}
|
| 304 |
-
|
| 305 |
const data = await response.json();
|
| 306 |
if (data.success) {
|
| 307 |
renderTokens(data.data);
|
|
@@ -374,7 +384,7 @@ async function toggleToken(refreshToken, enable) {
|
|
| 374 |
|
| 375 |
showLoading(`正在${action}Token...`);
|
| 376 |
try {
|
| 377 |
-
const response = await
|
| 378 |
method: 'PUT',
|
| 379 |
headers: {
|
| 380 |
'Content-Type': 'application/json',
|
|
@@ -403,7 +413,7 @@ async function deleteToken(refreshToken) {
|
|
| 403 |
|
| 404 |
showLoading('正在删除Token...');
|
| 405 |
try {
|
| 406 |
-
const response = await
|
| 407 |
method: 'DELETE',
|
| 408 |
headers: { 'Authorization': `Bearer ${authToken}` }
|
| 409 |
});
|
|
@@ -457,7 +467,7 @@ async function loadQuotaData(refreshToken, forceRefresh = false) {
|
|
| 457 |
|
| 458 |
try {
|
| 459 |
const url = `/admin/tokens/${encodeURIComponent(refreshToken)}/quotas${forceRefresh ? '?refresh=true' : ''}`;
|
| 460 |
-
const response = await
|
| 461 |
headers: { 'Authorization': `Bearer ${authToken}` }
|
| 462 |
});
|
| 463 |
|
|
@@ -564,7 +574,7 @@ async function refreshQuotaData(refreshToken) {
|
|
| 564 |
|
| 565 |
async function loadConfig() {
|
| 566 |
try {
|
| 567 |
-
const response = await
|
| 568 |
headers: { 'Authorization': `Bearer ${authToken}` }
|
| 569 |
});
|
| 570 |
const data = await response.json();
|
|
@@ -644,7 +654,7 @@ document.getElementById('configForm').addEventListener('submit', async (e) => {
|
|
| 644 |
|
| 645 |
showLoading('正在保存配置...');
|
| 646 |
try {
|
| 647 |
-
const response = await
|
| 648 |
method: 'PUT',
|
| 649 |
headers: {
|
| 650 |
'Content-Type': 'application/json',
|
|
|
|
| 9 |
'https://www.googleapis.com/auth/experimentsandconfigs'
|
| 10 |
].join(' ');
|
| 11 |
|
| 12 |
+
// 封装fetch,自动处理401
|
| 13 |
+
const authFetch = async (url, options = {}) => {
|
| 14 |
+
const response = await fetch(url, options);
|
| 15 |
+
if (response.status === 401) {
|
| 16 |
+
silentLogout();
|
| 17 |
+
showToast('登录已过期,请重新登录', 'warning');
|
| 18 |
+
throw new Error('Unauthorized');
|
| 19 |
+
}
|
| 20 |
+
return response;
|
| 21 |
+
};
|
| 22 |
+
|
| 23 |
function showToast(message, type = 'info', title = '') {
|
| 24 |
const icons = { success: '✅', error: '❌', warning: '⚠️', info: 'ℹ️' };
|
| 25 |
const titles = { success: '成功', error: '错误', warning: '警告', info: '提示' };
|
|
|
|
| 195 |
return;
|
| 196 |
}
|
| 197 |
|
| 198 |
+
const response = await authFetch('/admin/oauth/exchange', {
|
| 199 |
method: 'POST',
|
| 200 |
headers: {
|
| 201 |
'Content-Type': 'application/json',
|
|
|
|
| 207 |
const result = await response.json();
|
| 208 |
if (result.success) {
|
| 209 |
const account = result.data;
|
| 210 |
+
const addResponse = await authFetch('/admin/tokens', {
|
| 211 |
method: 'POST',
|
| 212 |
headers: {
|
| 213 |
'Content-Type': 'application/json',
|
|
|
|
| 248 |
|
| 249 |
showLoading('正在添加Token...');
|
| 250 |
try {
|
| 251 |
+
const response = await authFetch('/admin/tokens', {
|
| 252 |
method: 'POST',
|
| 253 |
headers: {
|
| 254 |
'Content-Type': 'application/json',
|
|
|
|
| 291 |
}
|
| 292 |
}
|
| 293 |
|
| 294 |
+
function silentLogout() {
|
|
|
|
|
|
|
|
|
|
| 295 |
localStorage.removeItem('authToken');
|
| 296 |
authToken = null;
|
| 297 |
document.getElementById('loginForm').classList.remove('hidden');
|
| 298 |
document.getElementById('mainContent').classList.add('hidden');
|
| 299 |
+
}
|
| 300 |
+
|
| 301 |
+
async function logout() {
|
| 302 |
+
const confirmed = await showConfirm('确定要退出登录吗?', '退出确认');
|
| 303 |
+
if (!confirmed) return;
|
| 304 |
+
|
| 305 |
+
silentLogout();
|
| 306 |
showToast('已退出登录', 'info');
|
| 307 |
}
|
| 308 |
|
| 309 |
async function loadTokens() {
|
| 310 |
try {
|
| 311 |
+
const response = await authFetch('/admin/tokens', {
|
| 312 |
headers: { 'Authorization': `Bearer ${authToken}` }
|
| 313 |
});
|
| 314 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
const data = await response.json();
|
| 316 |
if (data.success) {
|
| 317 |
renderTokens(data.data);
|
|
|
|
| 384 |
|
| 385 |
showLoading(`正在${action}Token...`);
|
| 386 |
try {
|
| 387 |
+
const response = await authFetch(`/admin/tokens/${encodeURIComponent(refreshToken)}`, {
|
| 388 |
method: 'PUT',
|
| 389 |
headers: {
|
| 390 |
'Content-Type': 'application/json',
|
|
|
|
| 413 |
|
| 414 |
showLoading('正在删除Token...');
|
| 415 |
try {
|
| 416 |
+
const response = await authFetch(`/admin/tokens/${encodeURIComponent(refreshToken)}`, {
|
| 417 |
method: 'DELETE',
|
| 418 |
headers: { 'Authorization': `Bearer ${authToken}` }
|
| 419 |
});
|
|
|
|
| 467 |
|
| 468 |
try {
|
| 469 |
const url = `/admin/tokens/${encodeURIComponent(refreshToken)}/quotas${forceRefresh ? '?refresh=true' : ''}`;
|
| 470 |
+
const response = await authFetch(url, {
|
| 471 |
headers: { 'Authorization': `Bearer ${authToken}` }
|
| 472 |
});
|
| 473 |
|
|
|
|
| 574 |
|
| 575 |
async function loadConfig() {
|
| 576 |
try {
|
| 577 |
+
const response = await authFetch('/admin/config', {
|
| 578 |
headers: { 'Authorization': `Bearer ${authToken}` }
|
| 579 |
});
|
| 580 |
const data = await response.json();
|
|
|
|
| 654 |
|
| 655 |
showLoading('正在保存配置...');
|
| 656 |
try {
|
| 657 |
+
const response = await authFetch('/admin/config', {
|
| 658 |
method: 'PUT',
|
| 659 |
headers: {
|
| 660 |
'Content-Type': 'application/json',
|