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.
This commit is contained in:
root 2026-08-20 18:36:41 +00:00
parent e5dceb250b
commit cb05669c7c

View File

@ -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();