Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parseUserData Support #44

Open
Shaxine opened this issue Mar 23, 2024 · 5 comments
Open

parseUserData Support #44

Shaxine opened this issue Mar 23, 2024 · 5 comments
Labels
question Further information is requested

Comments

@Shaxine
Copy link

Shaxine commented Mar 23, 2024

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.

Copy link

codeautopilot bot commented Mar 23, 2024

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]

@tachibana-shin
Copy link
Owner

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 tachibana-shin added the question Further information is requested label Mar 24, 2024
@Shaxine
Copy link
Author

Shaxine commented Mar 26, 2024

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.

@tachibana-shin
Copy link
Owner

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

@Shaxine
Copy link
Author

Shaxine commented Mar 27, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants