supabase icon indicating copy to clipboard operation
supabase copied to clipboard

Need help with "Store supabase user role using useState in global composable"?

Open nuwanvc opened this issue 2 years ago • 0 comments

Hi,

I'm trying to get the user role from superbase and store it in composable. I'm Getting error " [nuxt] [useState] key must be a string:"

Any help would be highly appreciated. Below is the code in composable.

export const useUserRole = function () {
    const user = useSupabaseUser()
    const email = user.value.email
    const client = useSupabaseClient()
    const [role, setRole] = useState('')

    const fetchUserRole = async () => {
        const { data, error } = await client
            .from('people')
            .select('role')
            .eq('email', email)
            .single()

        if (error) {
            console.error(error)
            return
        }

        if (data) {
            setRole(data.role)
        }
    }

    onMounted(fetchUserRole)

    return role
}

nuwanvc avatar Mar 28 '23 18:03 nuwanvc