43 lines
1.1 KiB
TypeScript
43 lines
1.1 KiB
TypeScript
import type { AuthSessionType, AuthUser } from '#shared/types/auth';
|
|
import type { ApiResponse } from '#shared/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,
|
|
};
|
|
}
|