fix(web): route avatar to profile management
All checks were successful
CI / validate (push) Successful in 19m38s
All checks were successful
CI / validate (push) Successful in 19m38s
Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
@@ -211,6 +211,44 @@ export function useDngAuth() {
|
||||
return user
|
||||
}
|
||||
|
||||
async function updateProfile(displayName: string) {
|
||||
const nextDisplayName = displayName.trim()
|
||||
if (nextDisplayName.length < 2 || nextDisplayName.length > 80) {
|
||||
throw new Error('Display name must contain between 2 and 80 characters.')
|
||||
}
|
||||
|
||||
const token = await accessToken()
|
||||
if (!session.value) throw new Error('Enter the alpha to continue.')
|
||||
const activeSession = session.value
|
||||
|
||||
await $fetch(`${config.public.supabaseUrl}/rest/v1/profiles?id=eq.${encodeURIComponent(activeSession.user.id)}`, {
|
||||
method: 'PATCH',
|
||||
headers: { ...authHeaders(token), Prefer: 'return=minimal' },
|
||||
body: { display_name: nextDisplayName },
|
||||
})
|
||||
|
||||
try {
|
||||
const user = normalizeUser(await $fetch<DngAuthUser>(`${config.public.supabaseUrl}/auth/v1/user`, {
|
||||
method: 'PUT',
|
||||
headers: authHeaders(token),
|
||||
body: { data: { display_name: nextDisplayName } },
|
||||
}))
|
||||
persist({ ...activeSession, user })
|
||||
return user
|
||||
} catch (cause) {
|
||||
const previousDisplayName = activeSession.user.user_metadata?.display_name
|
||||
const rollbackDisplayName = typeof previousDisplayName === 'string' && previousDisplayName.trim()
|
||||
? previousDisplayName.trim()
|
||||
: activeSession.user.email?.split('@')[0] || 'Adventurer'
|
||||
await $fetch(`${config.public.supabaseUrl}/rest/v1/profiles?id=eq.${encodeURIComponent(activeSession.user.id)}`, {
|
||||
method: 'PATCH',
|
||||
headers: { ...authHeaders(token), Prefer: 'return=minimal' },
|
||||
body: { display_name: rollbackDisplayName },
|
||||
}).catch(() => undefined)
|
||||
throw cause
|
||||
}
|
||||
}
|
||||
|
||||
async function accessToken() {
|
||||
await restore()
|
||||
if (!session.value) throw new Error('Enter the alpha to continue.')
|
||||
@@ -238,5 +276,5 @@ export function useDngAuth() {
|
||||
hydrated.value = true
|
||||
}
|
||||
|
||||
return { session, hydrated, restore, refresh, signUp, signIn, requestPasswordReset, updatePassword, accessToken, signOut, invalidate }
|
||||
return { session, hydrated, restore, refresh, signUp, signIn, requestPasswordReset, updatePassword, updateProfile, accessToken, signOut, invalidate }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user