fix architecture and write new tests
This commit is contained in:
@@ -46,26 +46,18 @@ export class RenterAuthError extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
function getRenterToken() {
|
||||
return window.localStorage.getItem('renter_token')
|
||||
}
|
||||
|
||||
async function renterFetch<T>(path: string, init?: RequestInit): Promise<T> {
|
||||
const token = getRenterToken()
|
||||
if (!token) throw new RenterAuthError()
|
||||
|
||||
const res = await fetch(`${API_BASE}${path}`, {
|
||||
...init,
|
||||
credentials: 'include',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${token}`,
|
||||
...(init?.headers ?? {}),
|
||||
},
|
||||
})
|
||||
|
||||
const json = await res.json().catch(() => null)
|
||||
if (res.status === 401) {
|
||||
window.localStorage.removeItem('renter_token')
|
||||
throw new RenterAuthError()
|
||||
}
|
||||
if (!res.ok) throw new Error(json?.message ?? 'Request failed')
|
||||
@@ -73,7 +65,7 @@ async function renterFetch<T>(path: string, init?: RequestInit): Promise<T> {
|
||||
}
|
||||
|
||||
export function clearRenterSession() {
|
||||
window.localStorage.removeItem('renter_token')
|
||||
void fetch(`${API_BASE}/auth/renter/logout`, { method: 'POST', credentials: 'include' })
|
||||
}
|
||||
|
||||
export function loadRenterProfile() {
|
||||
|
||||
Reference in New Issue
Block a user