mirror of
https://github.com/Paris-est-Ludique/ForceOrange.git
synced 2025-09-12 06:50:10 +02:00
✨ Complete all user management with forms and DB
This commit is contained in:
committed by
ChatonDeAru (Romain)
parent
37b2238b84
commit
5fa1f24caf
66
modules/app/composables/profile.ts
Normal file
66
modules/app/composables/profile.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
export const useProfile = () => {
|
||||
const user = useSupabaseUser()
|
||||
const client = useSupabaseClient()
|
||||
const toast = useToast()
|
||||
|
||||
const loading = ref(false)
|
||||
const profile = ref<{
|
||||
firstname: string
|
||||
lastname: string
|
||||
displayname?: string
|
||||
} | null>(null)
|
||||
|
||||
const displayName = computed(() => {
|
||||
if (!profile.value) {
|
||||
return '...'
|
||||
}
|
||||
|
||||
const { firstname, lastname, displayname } = profile.value || {}
|
||||
|
||||
if (!displayname) {
|
||||
return `${firstname || ''} ${lastname || ''}`.trim()
|
||||
}
|
||||
|
||||
return displayname
|
||||
})
|
||||
|
||||
const waitingMailValidation = computed(() => {
|
||||
return user.value && !user.value?.email_confirmed_at
|
||||
})
|
||||
|
||||
|
||||
async function getProfile() {
|
||||
loading.value = true
|
||||
|
||||
const { data, error } = await useAsyncData('profiles', async () => {
|
||||
if (!user.value) {
|
||||
return
|
||||
}
|
||||
|
||||
const { data, error } = await client.from('profiles').select('id,firstname,lastname,displayname,mail').eq('id', user.value.id).single()
|
||||
return data
|
||||
})
|
||||
|
||||
if (error.value) {
|
||||
toast.add({ color: 'red', description: error.value?.message, title: 'Error' })
|
||||
} else {
|
||||
profile.value = data.value
|
||||
}
|
||||
|
||||
loading.value = false
|
||||
}
|
||||
|
||||
watch(user, (user) => {
|
||||
if (user && !loading.value) {
|
||||
getProfile()
|
||||
}
|
||||
}, { immediate: true })
|
||||
|
||||
return {
|
||||
user,
|
||||
loading,
|
||||
profile,
|
||||
displayName,
|
||||
waitingMailValidation,
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user