Customize Session with User claims (roles)

Hi everyone!

I’ve implemented Microsoft authentication (server-side with Next.js), and everything works fine.
However, I’d like to add my user’s roles to the Better-Auth session, and I can’t figure out how to do it.

The roles are present in my idToken generated by Microsoft Entra ID, but I can’t find a way to customize the session so that they’re included.
I did find the customSession plugin, but it doesn’t seem to provide access to the token.

Here’s what I’m currently doing on the server side:
  • Retrieve the user session via auth.api.getSession
  • Retrieve the roles via auth.api.getAccessToken and parse the idToken
I’m hoping there’s a more optimal way to do this, but I couldn’t find anything in the documentation.
If anyone has done this before or has a suggestion, that would be great

Thanks!
Was this page helpful?