From cb05669c7c2e12eeab14557fb7bd5dc58ee824bb Mon Sep 17 00:00:00 2001 From: root Date: Thu, 20 Aug 2026 18:36:41 +0000 Subject: [PATCH] Fix auth: backend returns Access/Refresh (CamelCase), JS expected access/refresh Store.save() now handles both cases. Refresh endpoint uses 'Refresh' key. This was causing /me 401 right after register. --- public/js/api.js | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/public/js/api.js b/public/js/api.js index 92aa11b..6df66e2 100644 --- a/public/js/api.js +++ b/public/js/api.js @@ -7,15 +7,27 @@ const Store = { user: null, save(tokens) { - this.access = tokens.access; - this.refresh = tokens.refresh; - try { localStorage.setItem('buhapp', JSON.stringify({ access, refresh, user: this.user })); } catch {} + if (!tokens) return; + // бэкенд отдаёт {Access, Refresh} — поддержим оба варианта + this.access = tokens.Access || tokens.access; + this.refresh = tokens.Refresh || tokens.refresh; + try { + localStorage.setItem('buhapp', JSON.stringify({ + access: this.access, + refresh: this.refresh, + user: this.user, + })); + } catch {} }, load() { try { const s = JSON.parse(localStorage.getItem('buhapp') || 'null'); - if (s) { this.access = s.access; this.refresh = s.refresh; this.user = s.user; } + if (s) { + this.access = s.access || s.Access; + this.refresh = s.refresh || s.Refresh; + this.user = s.user; + } } catch {} return this.access; }, @@ -43,12 +55,12 @@ async function api(path, opts = {}) { const r2 = await fetch(API_BASE + '/api/v1/auth/refresh', { method: 'POST', headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ refresh: Store.refresh }), + body: JSON.stringify({ Refresh: Store.refresh }), }); if (r2.ok) { const tokens = await r2.json(); Store.save(tokens); - opts.headers.Authorization = `Bearer ${tokens.access}`; + opts.headers.Authorization = `Bearer ${Store.access}`; r = await fetch(API_BASE + path, opts); } else { Store.clear();