diff --git a/public/index.html b/public/index.html index 1a15e64..30befe3 100644 --- a/public/index.html +++ b/public/index.html @@ -34,15 +34,15 @@ - - - - - - - - - - + + + + + + + + + + diff --git a/public/js/api.js b/public/js/api.js index 78d5321..c5c8a3f 100644 --- a/public/js/api.js +++ b/public/js/api.js @@ -40,7 +40,7 @@ const Store = { }, }; -async function api(path, opts = {}) { +async function api(path, opts = {}, _retry = 0) { opts.headers = opts.headers || {}; if (Store.access && !opts.headers.Authorization) { opts.headers.Authorization = `Bearer ${Store.access}`; @@ -49,23 +49,41 @@ async function api(path, opts = {}) { opts.headers['Content-Type'] = 'application/json'; opts.body = JSON.stringify(opts.body); } - let r = await fetch(API_BASE + path, opts); - if (r.status === 401 && Store.refresh) { + let r; + try { + r = await fetch(API_BASE + path, opts); + } catch (networkErr) { + // сеть упала — ретрай через 2с + if (_retry < 1) { + await new Promise((res) => setTimeout(res, 2000)); + return api(path, opts, _retry + 1); + } + throw new Error('Нет связи с сервером'); + } + if (r.status === 401 && Store.refresh && !opts._isRefresh) { // refresh const r2 = await fetch(API_BASE + '/api/v1/auth/refresh', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ Refresh: Store.refresh }), + _isRefresh: true, }); if (r2.ok) { const tokens = await r2.json(); Store.save(tokens); opts.headers.Authorization = `Bearer ${Store.access}`; - r = await fetch(API_BASE + path, opts); + return api(path, opts, _retry + 1); } else { Store.clear(); } } + if (r.status === 502 || r.status === 503 || r.status === 504) { + if (_retry < 1) { + await new Promise((res) => setTimeout(res, 2000)); + return api(path, opts, _retry + 1); + } + throw new Error('Сервер перезапускается, попробуй через минуту'); + } const text = await r.text(); let data = null; try { data = text ? JSON.parse(text) : null; } catch { data = text; } diff --git a/public/js/app.js b/public/js/app.js index 3aeb5a9..db51f9c 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -41,13 +41,16 @@ const App = { this.showError('Не удалось войти через Telegram: ' + e.message); } } else if (API.Store.access) { + // есть токен — пробуем try { await API.Auth.me(); await this.checkConsent(); this.showView('map'); } catch (e) { + // токен протух или сеть — сразу на welcome, без показа ошибки + console.warn('init me() failed:', e); API.Store.clear(); - this.showError('Сессия истекла: ' + e.message); + this.showWelcome(); } } else { // Вне Telegram — показать email login (но сначала согласия)