From 85c787f428439420c17ee556235fc0a2ea06de88 Mon Sep 17 00:00:00 2001 From: axolotle Date: Tue, 25 Jul 2023 22:25:27 +0200 Subject: [PATCH] add useUserInfo that auto fetch user data --- composables/api.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/composables/api.ts b/composables/api.ts index d565ffe..a6f6882 100644 --- a/composables/api.ts +++ b/composables/api.ts @@ -35,3 +35,25 @@ export async function useApi( return result as AsyncData } + +export interface UserData { + username: string + fullname: string + mail: string + 'mail-aliases': string[] + 'mail-forward': string[] + groups: string[] + apps: Record +} + +export const useUserData = () => useState('userData') + +export async function useUserInfo() { + const userData = useUserData() + + if (!userData.value) { + const { data } = await useApi('/me') + userData.value = data.value as UserData + } + return userData as Ref +}