Files
warren/frontend/lib/api/auth/login.ts

51 lines
1.1 KiB
TypeScript

import { toast } from 'vue-sonner';
import type { ApiResponse } from '~/types/api';
import type { AuthUser } from '~/types/auth';
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: {
'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,
};
}
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,
};
}