diff --git a/main.go b/main.go index 77534e3..41b9cb3 100644 --- a/main.go +++ b/main.go @@ -265,10 +265,16 @@ func handleIndex(w http.ResponseWriter, r *http.Request) { const token = localStorage.getItem('token') || prompt('请输入访问令牌:'); if(token) localStorage.setItem('token', token); const headers = {'Authorization': 'Bearer ' + token}; - function loadTasks() { fetch('/api/tasks', {headers}) - .then(r => r.json()) + .then(r => { + if (!r.ok) { + localStorage.removeItem('token'); + alert('认证失败,请刷新页面重新输入'); + throw new Error('Unauthorized'); + } + return r.json(); + }) .then(tasks => { const tbody = document.querySelector('#taskTable tbody'); tbody.innerHTML = tasks.map(t => {