diff --git a/components/profile/ProfileInformation.vue b/components/profile/ProfileInformation.vue index 0ba0a5e..9254ce8 100644 --- a/components/profile/ProfileInformation.vue +++ b/components/profile/ProfileInformation.vue @@ -1,42 +1,33 @@ + + Error occurred: {{error}} - - - {{ username }} - + + + {{ loading ? 'Loading.' : user?.username }} + - - Streak - Games played - Placeholder - Date joined - + + Games played + Streak: {{ user?.daily_streak }} + Spotify {{user?.spotify_visibility ? 'public' : 'private'}} + Joined {{ new Date(session?.user.created_at).toDateString() }} + + diff --git a/composables/useUser.ts b/composables/useUser.ts new file mode 100644 index 0000000..7f997cb --- /dev/null +++ b/composables/useUser.ts @@ -0,0 +1,34 @@ +import type {GetUserResponse} from "@/types/api/users"; + +export const useUser = () => { + const user = useState('user', () => null) + const loading = useState('userLoading', () => false) + const error = useState('userError', () => null) + + const fetchUser = async () => { + // Only fetch if we don't have user data + if (!user.value) { + loading.value = true + error.value = null + try { + user.value = await useProfileInformation() + } catch (err) { + error.value = 'Failed to fetch user' + console.error(err) + } finally { + loading.value = false + } + } + } + + return { + user, + loading, + error, + fetchUser + } +} + +async function useProfileInformation() { + return await $fetch('http://localhost:3000/api/v1/user/') +} \ No newline at end of file diff --git a/server/api/v1/user/index.get.ts b/server/api/v1/user/index.get.ts new file mode 100644 index 0000000..9a52add --- /dev/null +++ b/server/api/v1/user/index.get.ts @@ -0,0 +1,34 @@ +import {serverSupabaseServiceRole, serverSupabaseUser} from "#supabase/server"; +import type {GetUserResponse} from "~/types/api/users"; + +export default defineEventHandler(async (event) => { + + // Require user to be authenticated + const user = await serverSupabaseUser(event); + if (!user?.id) { + setResponseStatus(event, 401); + return {error: 'unauthenticated'}; + } + + // Send request + const client = serverSupabaseServiceRole(event); + const {data, error}:{ data: GetUserResponse|null, error: any} = await client.from('users').select('*').eq('id', user.id).maybeSingle(); + + if (!data) { + setResponseStatus(event, 404); + return {error: 'User not found'}; + } + + // Handle errors + if (error) { + setResponseStatus(event, 500); + return {error: error.message}; + } + + // Hide spotify id if not visible + if (!data.spotify_visibility) { + delete data.spotify_id; + } + + return data; +}) \ No newline at end of file diff --git a/types/api/users.ts b/types/api/users.ts index 44f7eda..abffbcb 100644 --- a/types/api/users.ts +++ b/types/api/users.ts @@ -1,8 +1,6 @@ -import type url from "node:url"; - export interface GetUserResponse { id: string, - avatar_url?: url.URL | null, + avatar_url?: string, username: string, spotify_id?: string, spotify_visibility: boolean,
{{ username }}
{{ loading ? 'Loading.' : user?.username }}