fetch auth session data from token
This commit is contained in:
42
frontend/lib/api/auth/getSession.ts
Normal file
42
frontend/lib/api/auth/getSession.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import type { AuthSessionType, AuthUser } from '~/types/auth';
|
||||
import type { ApiResponse } from '~/types/api';
|
||||
|
||||
export async function getAuthSessionData(params: {
|
||||
sessionType: AuthSessionType;
|
||||
sessionId: string;
|
||||
}): Promise<
|
||||
{ success: true; user: AuthUser; expiresAt: number } | { success: false }
|
||||
> {
|
||||
const { data, status } = await useFetch<
|
||||
ApiResponse<{
|
||||
user: AuthUser;
|
||||
expiresAt: number;
|
||||
}>
|
||||
>(getApiUrl('auth/session'), {
|
||||
method: 'GET',
|
||||
key: new Date().getTime().toString(),
|
||||
headers: {
|
||||
authorization: `${params.sessionType} ${params.sessionId}`,
|
||||
},
|
||||
});
|
||||
|
||||
if (status.value !== 'success' || data.value == null) {
|
||||
return {
|
||||
success: false,
|
||||
};
|
||||
}
|
||||
|
||||
const { id, name, email, admin } = data.value.data.user;
|
||||
const expiresAt = data.value.data.expiresAt;
|
||||
|
||||
return {
|
||||
success: true,
|
||||
user: {
|
||||
id,
|
||||
name,
|
||||
email,
|
||||
admin,
|
||||
},
|
||||
expiresAt,
|
||||
};
|
||||
}
|
||||
@@ -1,10 +1,12 @@
|
||||
import { toast } from 'vue-sonner';
|
||||
import type { ApiResponse } from '~/types/api';
|
||||
import { getAuthSessionData } from './getSession';
|
||||
|
||||
export async function loginUser(
|
||||
email: string,
|
||||
password: string
|
||||
): Promise<{ success: boolean }> {
|
||||
console.log('logging in...', email, password);
|
||||
const { data, error } = await useFetch<ApiResponse<{ token: string }>>(
|
||||
getApiUrl('auth/login'),
|
||||
{
|
||||
@@ -30,7 +32,24 @@ export async function loginUser(
|
||||
};
|
||||
}
|
||||
|
||||
useAuthSession().value = { type: 'WarrenAuth', id: data.value.data.token };
|
||||
// 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,
|
||||
};
|
||||
}
|
||||
|
||||
useAuthSession().value = {
|
||||
type: 'WarrenAuth',
|
||||
id: data.value.data.token,
|
||||
user: sessionData.user,
|
||||
expiresAt: sessionData.expiresAt,
|
||||
};
|
||||
|
||||
toast.success('Login', {
|
||||
description: `Successfully logged in`,
|
||||
|
||||
Reference in New Issue
Block a user