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:
parent
e5dceb250b
commit
cb05669c7c
@ -7,15 +7,27 @@ const Store = {
|
|||||||
user: null,
|
user: null,
|
||||||
|
|
||||||
save(tokens) {
|
save(tokens) {
|
||||||
this.access = tokens.access;
|
if (!tokens) return;
|
||||||
this.refresh = tokens.refresh;
|
// бэкенд отдаёт {Access, Refresh} — поддержим оба варианта
|
||||||
try { localStorage.setItem('buhapp', JSON.stringify({ access, refresh, user: this.user })); } catch {}
|
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() {
|
load() {
|
||||||
try {
|
try {
|
||||||
const s = JSON.parse(localStorage.getItem('buhapp') || 'null');
|
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 {}
|
} catch {}
|
||||||
return this.access;
|
return this.access;
|
||||||
},
|
},
|
||||||
@ -43,12 +55,12 @@ async function api(path, opts = {}) {
|
|||||||
const r2 = await fetch(API_BASE + '/api/v1/auth/refresh', {
|
const r2 = await fetch(API_BASE + '/api/v1/auth/refresh', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({ refresh: Store.refresh }),
|
body: JSON.stringify({ Refresh: Store.refresh }),
|
||||||
});
|
});
|
||||||
if (r2.ok) {
|
if (r2.ok) {
|
||||||
const tokens = await r2.json();
|
const tokens = await r2.json();
|
||||||
Store.save(tokens);
|
Store.save(tokens);
|
||||||
opts.headers.Authorization = `Bearer ${tokens.access}`;
|
opts.headers.Authorization = `Bearer ${Store.access}`;
|
||||||
r = await fetch(API_BASE + path, opts);
|
r = await fetch(API_BASE + path, opts);
|
||||||
} else {
|
} else {
|
||||||
Store.clear();
|
Store.clear();
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user