43 lines
1012 B
TypeScript
43 lines
1012 B
TypeScript
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,
|
|
};
|
|
}
|