vue-auth3 icon indicating copy to clipboard operation
vue-auth3 copied to clipboard

parseUserData Support

Open Shaxine opened this issue 11 months ago • 5 comments

Coming from @websanova/vue-auth, I was wondering if there is an equivalent configuration of parseUserData to allow me to extract/parse the user data from within my API response, for example when my response looks something like this:

{
  "status": "success",
  "data": {
    "username": "admin",
    "role": "admin"
  }
}

And the meaningful user data is only

{
  "username": "admin",
  "role": "admin"
}

I searched the docs and source code and couldn't find an alternative approach.

Shaxine avatar Mar 23 '24 22:03 Shaxine

Your organization has reached the subscribed usage limit. You can upgrade your account by purchasing a subscription at Stripe payment link

Disclaimer: This comment was entirely generated using AI. Be aware that the information provided may be incorrect.

Current plan usage: 100.39%

Have feedback or need help? Discord Documentation [email protected]

codeautopilot[bot] avatar Mar 23 '24 22:03 codeautopilot[bot]

You have use keyUser. This option is available for all child options like login, refreshData....

const auth = createAuth({
    rolesKey: "type",
    notFoundRedirect: "/",
    fetchData: {
      enabled: true,
      cache: true,
      keyUser: "data"
    },
    refreshToken: {
      enabled: false,
    },

    plugins: {
      router,
    },
    drivers: {
      http: {
        request: app.config.globalProperties.$api,
      },
      auth: driverAuthBearer,
    },
  })

tachibana-shin avatar Mar 24 '24 05:03 tachibana-shin

Thank you! I got it working as intended.

When checking the docs, I was wondering what the keyUser option is for, since the description for it and the userKey options are missing.

After reading the source code and testing, I understood that the userKey defines the name for the cookie or storage entry of the cached user data.

Also, I'm not sure if this still applies to this thread, but I have another instance where the user contains a boolean "is_admin" field instead of a role field. I tried to set the rolesKey to "is_admin" and use $auth.check(true) to check for admin access, unfortunately without success. A workaround would be to append the role: "admin" to the user object when fetching the user details, but I didn't find a way of doing it.

Shaxine avatar Mar 26 '24 19:03 Shaxine

try setting the return from your api to include roles: ['admin'] then call .check('admin')

tachibana-shin avatar Mar 27 '24 05:03 tachibana-shin

Thank you for your time. I would like to keep the api unchanged, to ensure compatibility between clients (old and new).

In @websanova/vue-auth there was a parseUserData configuration function that would allow changing the user object when fetching the user.

Just to be sure, there is no way to change the user object right after the fetch, to append the roles: ['admin'] is case of is_admin: true, is there?

Shaxine avatar Mar 27 '24 19:03 Shaxine