Stateless Session Management: Additional Fields for Session

Hello,

So I am trying to integrate stateless session management with BetterAuth where users sign in with a generic oauth provider (this is also managed by my company and uses Better Auth OIDC Provider plugin as well), and the general flow works as expected:

  • User initiates sign in
  • User is redirected to sign in page for provider
  • After sign in / authorization, user is redirected back to application and the session data is available
Now what I am curious about is how do we customize the session for this type of flow? Essentially, I want to have access to the sub claim that would be attached to the idToken in the session object. I have looked into the customSession plugin, but seemingly can't use that, as only the current
session
and
user
is available, and any async call I would need to make (i.e. to the user info endpoint of the upstream provider) would require the access token.

The end goal here would be that when the session data is stored, I could extend the fields to apply like an idpId to the
user
in the same way that it applies email and name (in this case I just want to access the sub claim instead).

Is there a means to do this through hooks or other methods currently?

Alternative solution would be to verify and decode the idToken whenever I need this information, which I can get from the getAccessToken method, but this seems quite verbose. This may be intended though for security reasons?

Anyhow, just curious if there is a specific workflow for this use case that is recommended.
Was this page helpful?