implemented login
This commit is contained in:
+27
-4
@@ -1,14 +1,24 @@
|
||||
const BASE = '/api'
|
||||
|
||||
function buildAuthHeaders() {
|
||||
const token = sessionStorage.getItem('auth_token')
|
||||
return token ? { Authorization: `Bearer ${token}` } : {}
|
||||
}
|
||||
|
||||
async function request(method, path, body) {
|
||||
const res = await fetch(`${BASE}${path}`, {
|
||||
method,
|
||||
headers: body !== undefined ? { 'Content-Type': 'application/json' } : {},
|
||||
headers: {
|
||||
...(body !== undefined ? { 'Content-Type': 'application/json' } : {}),
|
||||
...buildAuthHeaders(),
|
||||
},
|
||||
body: body !== undefined ? JSON.stringify(body) : undefined,
|
||||
})
|
||||
if (!res.ok) {
|
||||
const err = await res.json().catch(() => ({ detail: res.statusText }))
|
||||
throw new Error(err.detail || `HTTP ${res.status}`)
|
||||
const error = new Error(err.detail || `HTTP ${res.status}`)
|
||||
error.status = res.status
|
||||
throw error
|
||||
}
|
||||
return res.json()
|
||||
}
|
||||
@@ -17,6 +27,13 @@ export const api = {
|
||||
// Health
|
||||
health: () => request('GET', '/health'),
|
||||
|
||||
// Auth
|
||||
login: (username, password) => request('POST', '/auth/login', { username, password }),
|
||||
me: () => request('GET', '/auth/me'),
|
||||
listUsers: () => request('GET', '/users'),
|
||||
createUser: (username, password, role) => request('POST', '/users', { username, password, role }),
|
||||
deleteUser: (username) => request('DELETE', `/users/${encodeURIComponent(username)}`),
|
||||
|
||||
// Settings
|
||||
getSettings: () => request('GET', '/settings'),
|
||||
saveSettings: (settings) => request('POST', '/settings', { settings }),
|
||||
@@ -42,10 +59,16 @@ export const api = {
|
||||
|
||||
exportWindow: async (windowId) => {
|
||||
const params = new URLSearchParams({ window_id: windowId })
|
||||
const res = await fetch(`${BASE}/schedule/export?${params}`)
|
||||
const res = await fetch(`${BASE}/schedule/export?${params}`, {
|
||||
headers: {
|
||||
...buildAuthHeaders(),
|
||||
},
|
||||
})
|
||||
if (!res.ok) {
|
||||
const err = await res.json().catch(() => ({ detail: res.statusText }))
|
||||
throw new Error(err.detail || `HTTP ${res.status}`)
|
||||
const error = new Error(err.detail || `HTTP ${res.status}`)
|
||||
error.status = res.status
|
||||
throw error
|
||||
}
|
||||
const blob = await res.blob()
|
||||
const disposition = res.headers.get('Content-Disposition') ?? ''
|
||||
|
||||
Reference in New Issue
Block a user