This commit is contained in:
2025-07-17 16:36:36 +02:00
parent a4cf1064d4
commit 8d707535fd
29 changed files with 751 additions and 26 deletions

View File

@@ -0,0 +1,42 @@
import { toast } from 'vue-sonner';
import type { ApiResponse } from '~/types/api';
export async function loginUser(
email: string,
password: string
): Promise<{ success: boolean }> {
const { data, error } = await useFetch<ApiResponse<{ token: string }>>(
getApiUrl('auth/login'),
{
method: 'POST',
headers: {
'content-type': 'application/json',
},
body: JSON.stringify({
email: email,
password: password,
}),
responseType: 'json',
}
);
if (data.value == null) {
toast.error('Login', {
description: error.value?.data ?? 'Failed to login',
});
return {
success: false,
};
}
useAuthSession().value = { type: 'WarrenAuth', id: data.value.data.token };
toast.success('Login', {
description: `Successfully logged in`,
});
return {
success: true,
};
}

15
frontend/lib/api/index.ts Normal file
View File

@@ -0,0 +1,15 @@
export function getApiHeaders(
includeAuth: boolean = true
): Record<string, string> {
const headers: Record<string, string> = {};
if (includeAuth) {
const authSession = useAuthSession().value;
if (authSession != null) {
headers['authorization'] = `${authSession.type} ${authSession.id}`;
}
}
return headers;
}

View File

@@ -2,12 +2,14 @@ import { toast } from 'vue-sonner';
import type { DirectoryEntry } from '~/types';
import type { ApiResponse } from '~/types/api';
import type { Warren } from '~/types/warrens';
import { getApiHeaders } from '.';
export async function getWarrens(): Promise<Record<string, Warren>> {
const { data, error } = await useFetch<ApiResponse<{ warrens: Warren[] }>>(
getApiUrl('warrens'),
{
method: 'GET',
headers: getApiHeaders(),
}
);