Next.js Discord

Discord Forum

Better Auth - Microsoft Entra

Unanswered
Harlequin posted this in #help-forum
Open in Discord
HarlequinOP
Hey, I switched to Better Auth to see if my application works faster than next-auth. I configured it, setup config for Microsoft Entra (External Id) as provider, get rid of authjs middleware etc. Seems to be working fine, it's moving me to microsoft login page but I have an problem with using access_token. My Next.js consumes external .NET api and I need to send access_token to it. Problem is that my access_token (which get by using getAccessToken method) is not in proper shape and throws that signature is invalid, I can see that for example issuer is set wrong but config needs to be fine because I can login and get session. I'm using stateless approach without database. I tried to use customSession plugin and there extend session object with manual fetch to /oauth/2.0/token to get actual (and proper) access_token but not sure if it's correct approach. Other thing is that approach required to add getUserInfo to provider config object, get refresh_token from token parameter and pass it to user to use it in customSession 😄 I don't think this is correct approach. I hope that you also faced it and have solution as I spent like 3 days on it.

Greetings

8 Replies

HarlequinOP
bump
HarlequinOP
bump
Sun bear
If I understand correctly, u don't know how to properly obtain a valid Microsoft Entra access_token in Better Auth in order to use it in an external .NET API?
HarlequinOP
Yes exactly
Sun bear
Could u send config file?
HarlequinOP
Generally it was something like this
so it's more like extending custom session with access_token, I don't think is it secure way - getAccessToken method gives me wrong one - like I checked for example it contains "iss" property for issuer and it use sts.microsoft endpoint instead {tenantId}.ciamlogin.com - looks like using v1 instead v2 of microsoft's api