get complete session from login request instead of fetching again
This commit is contained in:
@@ -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', {
|
||||
|
||||
Reference in New Issue
Block a user