Files
warren/frontend/lib/api/auth/login.ts
2025-07-20 13:14:31 +02:00

50 lines
1.2 KiB
TypeScript

import { toast } from 'vue-sonner';
import type { ApiResponse } from '#shared/types/api';
import type { AuthUser } from '#shared/types/auth';
import { getApiHeaders } from '..';
export async function loginUser(
email: string,
password: string
): Promise<{ success: boolean }> {
const { data, error } = await useFetch<
ApiResponse<{ token: string; user: AuthUser; expiresAt: number }>
>(getApiUrl('auth/login'), {
method: 'POST',
headers: getApiHeaders(false),
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,
};
}
const token = data.value.data.token;
const { user, expiresAt } = data.value.data;
useAuthSession().value = {
type: 'WarrenAuth',
id: token,
user,
expiresAt,
};
toast.success('Login', {
description: `Successfully logged in`,
});
return {
success: true,
};
}