get complete session from login request instead of fetching again

This commit is contained in:
2025-07-18 14:05:19 +02:00
parent fe6cf2fb53
commit 4c3e54daca
9 changed files with 102 additions and 76 deletions

View File

@@ -1,25 +1,25 @@
import { toast } from 'vue-sonner';
import type { ApiResponse } from '~/types/api';
import { getAuthSessionData } from './getSession';
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 }>>(
getApiUrl('auth/login'),
{
method: 'POST',
headers: {
'content-type': 'application/json',
},
body: JSON.stringify({
email: email,
password: password,
}),
responseType: 'json',
}
);
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', {
@@ -31,23 +31,14 @@ export async function loginUser(
};
}
// TODO: Get this data directly from the login request
const sessionData = await getAuthSessionData({
sessionType: 'WarrenAuth',
sessionId: data.value.data.token,
});
if (!sessionData.success) {
return {
success: false,
};
}
const token = data.value.data.token;
const { user, expiresAt } = data.value.data;
useAuthSession().value = {
type: 'WarrenAuth',
id: data.value.data.token,
user: sessionData.user,
expiresAt: sessionData.expiresAt,
id: token,
user,
expiresAt,
};
toast.success('Login', {