feat(frontend): add apiFetch wrapper for automatic auth token injection
Centralizes auth token management into a reusable apiFetch() helper and getAuthHeaders() utility, eliminating inline Bearer token logic across all frontend files. Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
<script lang="ts">
|
||||
import { authStore } from '$lib/stores/auth.svelte';
|
||||
import { SvelteDate, SvelteURLSearchParams } from 'svelte/reactivity';
|
||||
import { toast } from 'svelte-sonner';
|
||||
import { apiFetch } from '$lib/utils/api';
|
||||
import * as Modal from '$lib/components/ui/dialog';
|
||||
import { Button } from '$lib/components/ui/button';
|
||||
import PatchTestModal from './PatchTestModal.svelte';
|
||||
@@ -104,11 +104,9 @@
|
||||
if (!userId) return;
|
||||
|
||||
try {
|
||||
const response = await fetch(`/api/admin/users/${userId}`, {
|
||||
method: 'GET',
|
||||
const response = await apiFetch(`/api/admin/users/${userId}`, {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${authStore.currentToken}`
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
@@ -127,11 +125,9 @@
|
||||
async function fetchEligiblePatchTests() {
|
||||
if (!userId) return;
|
||||
try {
|
||||
const response = await fetch(`/api/admin/users/${userId}/patch-tests/eligible`, {
|
||||
method: 'GET',
|
||||
const response = await apiFetch(`/api/admin/users/${userId}/patch-tests/eligible`, {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${authStore.currentToken}`
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
if (response.ok) {
|
||||
@@ -163,11 +159,9 @@
|
||||
params.set('cursor', cursor);
|
||||
}
|
||||
|
||||
const response = await fetch(`/api/admin/bookings/user/${userId}?${params}`, {
|
||||
method: 'GET',
|
||||
const response = await apiFetch(`/api/admin/bookings/user/${userId}?${params}`, {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${authStore.currentToken}`
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
@@ -221,11 +215,9 @@
|
||||
|
||||
loadingRelationship = true;
|
||||
try {
|
||||
const response = await fetch(`/api/admin/users/${userId}/relationship`, {
|
||||
method: 'GET',
|
||||
const response = await apiFetch(`/api/admin/users/${userId}/relationship`, {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${authStore.currentToken}`
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
@@ -245,9 +237,7 @@
|
||||
async function fetchGiftCardBalance() {
|
||||
if (!userId) return;
|
||||
try {
|
||||
const res = await fetch(`/api/admin/users/${userId}/giftcard-balance`, {
|
||||
headers: { Authorization: `Bearer ${authStore.currentToken}` }
|
||||
});
|
||||
const res = await apiFetch(`/api/admin/users/${userId}/giftcard-balance`);
|
||||
if (res.ok) {
|
||||
const data = await res.json();
|
||||
giftCardBalance = data.balance;
|
||||
|
||||
Reference in New Issue
Block a user