strapi icon indicating copy to clipboard operation
strapi copied to clipboard

How to add user "role" when call : 'await fetchUser()' ?

Open titoff002 opened this issue 3 years ago • 3 comments

Hello,

const { fetchUser } = useStrapiAuth()
const user2 = await fetchUser()

Question : How to add user "role" on the response FROM await fetchUser() ??

I would like use "populate" : but I don't understand how it works. Do you have an example ?

PS : i use nuxtjs 3.

Thanks.

titoff002 avatar Aug 12 '22 23:08 titoff002

Yeah, really interested in how to add populate=* to the user request. Cause I assume population of relations from /users/me has been fixed, but I can't figure out how to do it with the plugin

Aft1n avatar Sep 01 '22 07:09 Aft1n

I also need this. I tried using the auth.populate params in nuxt config but not luck. Also seems that populating other relations do not work. I just get the default /me/ properties.

fskarmeta avatar Oct 02 '22 18:10 fskarmeta

@titoff002 Hey, at least it works with useStrapiUser, you have to give permission to the role in strapi to find it's own role first. Then the nuxt.config.ts strapi config object add the auth: {populate: ['role']}} object.

fskarmeta avatar Oct 07 '22 01:10 fskarmeta

Here is a solution:

  • Modify your nuxt config :
strapi: {
    auth: {
      populate: ['role']
    }
},
  • Don't forget to enable find and findOne role permissions for authenticated users in the users permissions plugin. Screenshot from 2023-08-16 11-00-23

ZwurK avatar Aug 16 '23 09:08 ZwurK