feat(client): swap auth client to session API; add guestsApi
This commit is contained in:
74
lib/api.ts
74
lib/api.ts
@@ -23,14 +23,23 @@ async function handleResponse<T>(response: Response): Promise<T> {
|
|||||||
|
|
||||||
// Auth API
|
// Auth API
|
||||||
export const authApi = {
|
export const authApi = {
|
||||||
async login(username: string, password: string) {
|
async session(payload: { adm?: string; usr?: string }) {
|
||||||
const response = await fetch(`${API_BASE_URL}/auth/login`, {
|
const response = await fetch(`${API_BASE_URL}/auth/session`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
credentials: 'include',
|
credentials: 'include',
|
||||||
body: JSON.stringify({ username, password }),
|
body: JSON.stringify(payload),
|
||||||
});
|
});
|
||||||
return handleResponse<{ accessToken: string; refreshToken: string }>(response);
|
return handleResponse<{ success: true; role: 'admin' | 'guest'; guestId: string | null }>(response);
|
||||||
|
},
|
||||||
|
|
||||||
|
async whoami() {
|
||||||
|
const response = await fetch(`${API_BASE_URL}/auth/session`, { credentials: 'include' });
|
||||||
|
return handleResponse<
|
||||||
|
| { role: 'admin'; guest: { id: string; name: string } | null }
|
||||||
|
| { role: 'guest'; guest: { id: string; name: string } }
|
||||||
|
| { role: 'none' }
|
||||||
|
>(response);
|
||||||
},
|
},
|
||||||
|
|
||||||
async logout() {
|
async logout() {
|
||||||
@@ -38,22 +47,52 @@ export const authApi = {
|
|||||||
method: 'POST',
|
method: 'POST',
|
||||||
credentials: 'include',
|
credentials: 'include',
|
||||||
});
|
});
|
||||||
return handleResponse<void>(response);
|
return handleResponse<{ success: true }>(response);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export interface Guest {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
createdAt: string;
|
||||||
|
updatedAt: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const guestsApi = {
|
||||||
|
async list() {
|
||||||
|
const response = await fetch(`${API_BASE_URL}/admin/guests`, { credentials: 'include' });
|
||||||
|
const data = await handleResponse<{ success: true; guests: Guest[] }>(response);
|
||||||
|
return data.guests;
|
||||||
},
|
},
|
||||||
|
|
||||||
async refresh() {
|
async create(name: string) {
|
||||||
const response = await fetch(`${API_BASE_URL}/auth/refresh`, {
|
const response = await fetch(`${API_BASE_URL}/admin/guests`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
credentials: 'include',
|
credentials: 'include',
|
||||||
|
body: JSON.stringify({ name }),
|
||||||
});
|
});
|
||||||
return handleResponse<{ accessToken: string }>(response);
|
const data = await handleResponse<{ success: true; guest: Guest }>(response);
|
||||||
|
return data.guest;
|
||||||
},
|
},
|
||||||
|
|
||||||
async me() {
|
async rename(id: string, name: string) {
|
||||||
const response = await fetch(`${API_BASE_URL}/auth/me`, {
|
const response = await fetch(`${API_BASE_URL}/admin/guests/${id}`, {
|
||||||
|
method: 'PATCH',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
credentials: 'include',
|
||||||
|
body: JSON.stringify({ name }),
|
||||||
|
});
|
||||||
|
const data = await handleResponse<{ success: true; guest: Guest }>(response);
|
||||||
|
return data.guest;
|
||||||
|
},
|
||||||
|
|
||||||
|
async delete(id: string) {
|
||||||
|
const response = await fetch(`${API_BASE_URL}/admin/guests/${id}`, {
|
||||||
|
method: 'DELETE',
|
||||||
credentials: 'include',
|
credentials: 'include',
|
||||||
});
|
});
|
||||||
return handleResponse<{ username: string }>(response);
|
return handleResponse<{ success: true }>(response);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -246,23 +285,24 @@ export const itemsApi = {
|
|||||||
|
|
||||||
// Claiming API (public)
|
// Claiming API (public)
|
||||||
export const claimingApi = {
|
export const claimingApi = {
|
||||||
async claim(itemId: string, name?: string, note?: string) {
|
async claim(itemId: string, opts: { quantity?: number; note?: string } = {}) {
|
||||||
const response = await fetch(`${API_BASE_URL}/public/items/${itemId}/claim`, {
|
const response = await fetch(`${API_BASE_URL}/public/items/${itemId}/claim`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
credentials: 'include',
|
credentials: 'include',
|
||||||
body: JSON.stringify({ name, note }),
|
body: JSON.stringify({ quantity: opts.quantity ?? 1, note: opts.note }),
|
||||||
});
|
});
|
||||||
return handleResponse<{ claimToken: string; message: string }>(response);
|
return handleResponse<{ success: true; item: Item }>(response);
|
||||||
},
|
},
|
||||||
|
|
||||||
async unclaim(itemId: string) {
|
async unclaim(itemId: string, opts: { guestId?: string } = {}) {
|
||||||
const response = await fetch(`${API_BASE_URL}/public/items/${itemId}/unclaim`, {
|
const response = await fetch(`${API_BASE_URL}/public/items/${itemId}/unclaim`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
credentials: 'include',
|
credentials: 'include',
|
||||||
|
body: JSON.stringify(opts.guestId ? { guestId: opts.guestId } : {}),
|
||||||
});
|
});
|
||||||
return handleResponse<{ success: boolean; message: string }>(response);
|
return handleResponse<{ success: true }>(response);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -293,8 +333,6 @@ export const scrapingApi = {
|
|||||||
export interface Settings {
|
export interface Settings {
|
||||||
siteTitle: string;
|
siteTitle: string;
|
||||||
homepageSubtext: string;
|
homepageSubtext: string;
|
||||||
passwordLockEnabled?: boolean;
|
|
||||||
passwordLock?: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const settingsApi = {
|
export const settingsApi = {
|
||||||
|
|||||||
Reference in New Issue
Block a user