Updating profile information on sign-in #1138

Closed
opened 2026-03-13 08:24:31 -05:00 by GiteaMirror · 4 comments
Owner

Originally created by @Karakatiza666 on GitHub (Apr 30, 2025).

Is this suited for github?

  • Yes, this is suited for github

I am using a third party OAuth provider where I have RBAC configured, with generic-oauth better-auth plugin. As a part of the access/identity token I receive the JWT claim "permissions", which can be propagated with getUserInfo() and mapProfileToUser() and is stored as an additional custom field "permissions" in the User entity, which the backend can then use to authorize requests. The issue is once the User is created on the first login there is no way to automatically update user data based on UserInfo on every successive login. Triggering the update on the client does not cut it for security reasons.

Describe the solution you'd like

Either a

  1. An onLogin callback which exposes internal authentication context, which would allow calling context.internalAdapter.updateUser(), or
  2. Some way to configure the policy for refreshing User data based on UserInfo on every login.

Describe alternatives you've considered

I do not see a reasonable way to achieve this (server-side refresh of User data on login via oauth) with better-auth currently other than writing a custom plugin.
The workaround I will try out is manually updating the custom "permissions" column in the "user" built-in table with the latest profile.permissions inside the mapProfileToUser callback of the generic-oauth plugin

Additional context

No response

Originally created by @Karakatiza666 on GitHub (Apr 30, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### Is your feature request related to a problem? Please describe. I am using a third party OAuth provider where I have RBAC configured, with generic-oauth better-auth plugin. As a part of the access/identity token I receive the JWT claim "permissions", which can be propagated with `getUserInfo()` and `mapProfileToUser()` and is stored as an additional custom field "permissions" in the User entity, which the backend can then use to authorize requests. The issue is once the User is created on the first login there is no way to automatically update user data based on UserInfo on every successive login. Triggering the update on the client does not cut it for security reasons. ### Describe the solution you'd like Either a 1) An onLogin callback which exposes internal authentication context, which would allow calling `context.internalAdapter.updateUser()`, or 2) Some way to configure the policy for refreshing User data based on UserInfo on every login. ### Describe alternatives you've considered I do not see a reasonable way to achieve this (server-side refresh of User data on login via oauth) with better-auth currently other than writing a custom plugin. The workaround I will try out is manually updating the custom "permissions" column in the "user" built-in table with the latest profile.permissions inside the mapProfileToUser callback of the generic-oauth plugin ### Additional context _No response_
Author
Owner

@Kinfe123 commented on GitHub (May 1, 2025):

Instead of approaching the onLogin callback, you could use the database hook for newly created sessions and gain access to the internal adapter as well.

@Kinfe123 commented on GitHub (May 1, 2025): Instead of approaching the onLogin callback, you could use the database hook for newly created sessions and gain access to the internal adapter as well.
Author
Owner

@Bekacru commented on GitHub (May 1, 2025):

you can set overrideUserInfoOnSignIn to true

@Bekacru commented on GitHub (May 1, 2025): you can set `overrideUserInfoOnSignIn` to `true`
Author
Owner

@Karakatiza666 commented on GitHub (May 1, 2025):

I did not figure out how to access the new user info in the databaseHooks.sessionCreate.after hook.
But overrideUserInfoOnSignIn option in the generic-oauth plugin that I overlooked in the docs is the perfect solution for me!

@Karakatiza666 commented on GitHub (May 1, 2025): I did not figure out how to access the new user info in the databaseHooks.sessionCreate.after hook. But `overrideUserInfoOnSignIn` option in the generic-oauth plugin that I overlooked in the docs is the perfect solution for me!
Author
Owner

@Karakatiza666 commented on GitHub (May 1, 2025):

@Kinfe123 could you point me to the docs/example of how to access the internal adapter instance in user code?

@Karakatiza666 commented on GitHub (May 1, 2025): @Kinfe123 could you point me to the docs/example of how to access the internal adapter instance in user code?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1138