mirror of
https://github.com/dat515-2025/Group-8.git
synced 2026-03-22 15:12:08 +01:00
feat(backend): fixed build errors regarding token in headers
This commit is contained in:
@@ -23,9 +23,21 @@ function getBaseUrl() {
|
||||
return base || '';
|
||||
}
|
||||
|
||||
function authHeaders() {
|
||||
function getHeaders(contentType: 'json' | 'form' | 'none' = 'json'): Record<string, string> {
|
||||
const token = localStorage.getItem('token');
|
||||
return token ? { Authorization: `Bearer ${token}` } : {};
|
||||
const headers: Record<string, string> = {};
|
||||
|
||||
if (contentType === 'json') {
|
||||
headers['Content-Type'] = 'application/json';
|
||||
} else if (contentType === 'form') {
|
||||
headers['Content-Type'] = 'application/x-www-form-urlencoded';
|
||||
}
|
||||
|
||||
if (token) {
|
||||
headers['Authorization'] = `Bearer ${token}`;
|
||||
}
|
||||
|
||||
return headers;
|
||||
}
|
||||
|
||||
export async function login(email: string, password: string): Promise<void> {
|
||||
@@ -62,7 +74,7 @@ export async function register(email: string, password: string, first_name?: str
|
||||
|
||||
export async function getCategories(): Promise<Category[]> {
|
||||
const res = await fetch(`${getBaseUrl()}/categories/`, {
|
||||
headers: { 'Content-Type': 'application/json', ...authHeaders() },
|
||||
headers: getHeaders(),
|
||||
});
|
||||
if (!res.ok) throw new Error('Failed to load categories');
|
||||
return res.json();
|
||||
@@ -77,7 +89,7 @@ export type CreateTransactionInput = {
|
||||
export async function createTransaction(input: CreateTransactionInput): Promise<Transaction> {
|
||||
const res = await fetch(`${getBaseUrl()}/transactions/create`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', ...authHeaders() },
|
||||
headers: getHeaders(),
|
||||
body: JSON.stringify(input),
|
||||
});
|
||||
if (!res.ok) {
|
||||
@@ -89,7 +101,7 @@ export async function createTransaction(input: CreateTransactionInput): Promise<
|
||||
|
||||
export async function getTransactions(): Promise<Transaction[]> {
|
||||
const res = await fetch(`${getBaseUrl()}/transactions/`, {
|
||||
headers: { 'Content-Type': 'application/json', ...authHeaders() },
|
||||
headers: getHeaders(),
|
||||
});
|
||||
if (!res.ok) throw new Error('Failed to load transactions');
|
||||
return res.json();
|
||||
@@ -107,7 +119,7 @@ export type User = {
|
||||
|
||||
export async function getMe(): Promise<User> {
|
||||
const res = await fetch(`${getBaseUrl()}/users/me`, {
|
||||
headers: { 'Content-Type': 'application/json', ...authHeaders() },
|
||||
headers: getHeaders(),
|
||||
});
|
||||
if (!res.ok) throw new Error('Failed to load user');
|
||||
return res.json();
|
||||
@@ -117,7 +129,7 @@ export type UpdateMeInput = Partial<Pick<User, 'first_name' | 'last_name'>> & {
|
||||
export async function updateMe(input: UpdateMeInput): Promise<User> {
|
||||
const res = await fetch(`${getBaseUrl()}/users/me`, {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json', ...authHeaders() },
|
||||
headers: getHeaders(),
|
||||
body: JSON.stringify(input),
|
||||
});
|
||||
if (!res.ok) {
|
||||
@@ -130,7 +142,7 @@ export async function updateMe(input: UpdateMeInput): Promise<User> {
|
||||
export async function deleteMe(): Promise<void> {
|
||||
const res = await fetch(`${getBaseUrl()}/users/me`, {
|
||||
method: 'DELETE',
|
||||
headers: { ...authHeaders() },
|
||||
headers: getHeaders(),
|
||||
});
|
||||
if (!res.ok) {
|
||||
const text = await res.text();
|
||||
|
||||
@@ -5,7 +5,6 @@ const THEME_KEY = 'app_theme';
|
||||
const FONT_KEY = 'app_font_size';
|
||||
|
||||
export function applyTheme(theme: Theme) {
|
||||
const root = document.documentElement;
|
||||
const body = document.body;
|
||||
const effective = theme === 'system' ? (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light') : theme;
|
||||
body.setAttribute('data-theme', effective);
|
||||
|
||||
Reference in New Issue
Block a user