fix: проверка токена при загрузке batch3, ssr, admin
Все страницы теперь проверяют валидность токена через API при загрузке. Просроченный токен → refresh → если fail → redirect. Предотвращает "вечные сессии" на всех страницах. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
+18
-2
@@ -86,8 +86,24 @@ a { color: var(--accent); text-decoration: none; }
|
||||
</div>
|
||||
|
||||
<script>
|
||||
const token = localStorage.getItem('token');
|
||||
if (!token) window.location.href = '/';
|
||||
let token = localStorage.getItem('token');
|
||||
// Validate token on load
|
||||
(async () => {
|
||||
if (!token) { location.href = '/'; return; }
|
||||
try {
|
||||
const r = await fetch('/api/history?limit=1', {headers:{'Authorization':'Bearer '+token}});
|
||||
if (r.ok) return;
|
||||
} catch {}
|
||||
const rt = localStorage.getItem('refresh_token');
|
||||
if (rt) {
|
||||
try {
|
||||
const r = await fetch('/api/refresh', {method:'POST', headers:{'Content-Type':'application/json'}, body:JSON.stringify({refresh_token:rt})});
|
||||
if (r.ok) { const d = await r.json(); token = d.token; localStorage.setItem('token', token); return; }
|
||||
} catch {}
|
||||
}
|
||||
localStorage.removeItem('token'); localStorage.removeItem('refresh_token');
|
||||
location.href = '/';
|
||||
})();
|
||||
const H = {'Authorization': 'Bearer ' + token};
|
||||
|
||||
// Check token validity — redirect to login on 401
|
||||
|
||||
+18
-12
@@ -104,18 +104,24 @@ pre { background: #2c2c2c; color: #f5f0eb; padding: .8rem; border-radius: 6px; o
|
||||
|
||||
<script>
|
||||
let token = localStorage.getItem('token');
|
||||
if (!token) {
|
||||
// Quick inline login instead of redirect
|
||||
const u = prompt('Логин:');
|
||||
const p = prompt('Пароль:');
|
||||
if (u && p) {
|
||||
fetch('/api/login', {method:'POST', headers:{'Content-Type':'application/json'}, body:JSON.stringify({username:u,password:p})})
|
||||
.then(r => r.json()).then(d => {
|
||||
if (d.token) { localStorage.setItem('token',d.token); token=d.token; location.reload(); }
|
||||
else { alert('Неверные данные'); window.location.href='/'; }
|
||||
}).catch(() => window.location.href='/');
|
||||
} else { window.location.href='/'; }
|
||||
}
|
||||
// Validate token on load (don't trust localStorage blindly)
|
||||
(async () => {
|
||||
if (!token) { window.location.href = '/'; return; }
|
||||
try {
|
||||
const r = await fetch('/api/history?limit=1', {headers:{'Authorization':'Bearer '+token}});
|
||||
if (r.ok) return; // token valid
|
||||
} catch {}
|
||||
// Try refresh
|
||||
const rt = localStorage.getItem('refresh_token');
|
||||
if (rt) {
|
||||
try {
|
||||
const r = await fetch('/api/refresh', {method:'POST', headers:{'Content-Type':'application/json'}, body:JSON.stringify({refresh_token:rt})});
|
||||
if (r.ok) { const d = await r.json(); token = d.token; localStorage.setItem('token', token); return; }
|
||||
} catch {}
|
||||
}
|
||||
localStorage.removeItem('token'); localStorage.removeItem('refresh_token');
|
||||
window.location.href = '/';
|
||||
})();
|
||||
|
||||
const API = '/api/batch-v3';
|
||||
const H = {'Authorization': 'Bearer ' + token};
|
||||
|
||||
+18
-2
@@ -94,8 +94,24 @@ button:disabled { background: var(--border); cursor: not-allowed; }
|
||||
</div>
|
||||
|
||||
<script>
|
||||
const token = localStorage.getItem('token');
|
||||
if (!token) location.href = '/';
|
||||
let token = localStorage.getItem('token');
|
||||
// Validate token on load
|
||||
(async () => {
|
||||
if (!token) { location.href = '/'; return; }
|
||||
try {
|
||||
const r = await fetch('/api/history?limit=1', {headers:{'Authorization':'Bearer '+token}});
|
||||
if (r.ok) return;
|
||||
} catch {}
|
||||
const rt = localStorage.getItem('refresh_token');
|
||||
if (rt) {
|
||||
try {
|
||||
const r = await fetch('/api/refresh', {method:'POST', headers:{'Content-Type':'application/json'}, body:JSON.stringify({refresh_token:rt})});
|
||||
if (r.ok) { const d = await r.json(); token = d.token; localStorage.setItem('token', token); return; }
|
||||
} catch {}
|
||||
}
|
||||
localStorage.removeItem('token'); localStorage.removeItem('refresh_token');
|
||||
location.href = '/';
|
||||
})();
|
||||
|
||||
let currentJobId = null;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user