[GH-ISSUE #2769] EntraID token refresh not working #26665

Closed
opened 2026-04-17 17:18:57 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @CarbonNeuron on GitHub (May 24, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/2769

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

social sign on user with entra ID, attempt to refresh the access token. get bad request

Current vs. Expected behavior

following the previous steps, i expected the token refresh to behave normally. but it does not. we get 400 bad request error from Microsoft

What version of Better Auth are you using?

1.2.8

Provide environment information

- OS windows 11
- Browser latest chrome canary (not important)

Which area(s) are affected? (Select all that apply)

Package

Auth config (if applicable)

import { betterAuth } from "better-auth"

export const auth = betterAuth({
  database: prismaAdapter(prisma, {
    provider: 'sqlserver',
  }),
  socialProviders: {
    microsoft: {
      clientId: env.MICROSOFT_ENTRA_ID_ID,
      clientSecret: env.MICROSOFT_ENTRA_ID_SECRET,
      tenantId: env.MICROSOFT_ENTRA_ID_TENANT_ID,
    },
  },
  account: {
    updateAccountOnSignIn: true,
  },
  plugins: [nextCookies()],
  session: {
    cookieCache: {
      enabled: true,
      maxAge: 5 * 60, // Cache duration in seconds
    },
  },
});

Additional context

Looking at the guide here: https://learn.microsoft.com/en-us/graph/auth-v2-user?tabs=http#step-4-use-the-refresh-token-to-renew-an-expired-access-token

and from my previous experiences writing this logic manually, we are missing the scope url parameters. we cannot omit this. This looks like a fairly simple fix here: 43513e98d1/packages/better-auth/src/social-providers/microsoft-entra-id.ts (L119)

also, the documentation on this is a little obscure. But, in order to get a refresh token, we need to have offline_access in our scopes. Stated here: https://learn.microsoft.com/en-us/graph/auth-v2-user?tabs=http#authorization-request

One other small thing is that if we chose to disable the profile picture, we still are opening and establishing a connection with microsoft at least until we finish getting the headers here: 43513e98d1/packages/better-auth/src/social-providers/microsoft-entra-id.ts (L87)

This operation is cheap (although not free) since we are fully establishing a TLS session only to tare it down. although we dont finish reading the body. there isn't a good reason i see to be doing this.

Originally created by @CarbonNeuron on GitHub (May 24, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/2769 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce social sign on user with entra ID, attempt to refresh the access token. get bad request ### Current vs. Expected behavior following the previous steps, i expected the token refresh to behave normally. but it does not. we get 400 bad request error from Microsoft ### What version of Better Auth are you using? 1.2.8 ### Provide environment information ```bash - OS windows 11 - Browser latest chrome canary (not important) ``` ### Which area(s) are affected? (Select all that apply) Package ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ database: prismaAdapter(prisma, { provider: 'sqlserver', }), socialProviders: { microsoft: { clientId: env.MICROSOFT_ENTRA_ID_ID, clientSecret: env.MICROSOFT_ENTRA_ID_SECRET, tenantId: env.MICROSOFT_ENTRA_ID_TENANT_ID, }, }, account: { updateAccountOnSignIn: true, }, plugins: [nextCookies()], session: { cookieCache: { enabled: true, maxAge: 5 * 60, // Cache duration in seconds }, }, }); ``` ### Additional context Looking at the guide here: https://learn.microsoft.com/en-us/graph/auth-v2-user?tabs=http#step-4-use-the-refresh-token-to-renew-an-expired-access-token and from my previous experiences writing this logic manually, we are missing the scope url parameters. we cannot omit this. This looks like a fairly simple fix here: https://github.com/better-auth/better-auth/blob/43513e98d1973c15dd2285404955e6475145aa49/packages/better-auth/src/social-providers/microsoft-entra-id.ts#L119 also, the documentation on this is a little obscure. But, in order to get a refresh token, we need to have offline_access in our scopes. Stated here: https://learn.microsoft.com/en-us/graph/auth-v2-user?tabs=http#authorization-request One other small thing is that if we chose to disable the profile picture, we still are opening and establishing a connection with microsoft at least until we finish getting the headers here: https://github.com/better-auth/better-auth/blob/43513e98d1973c15dd2285404955e6475145aa49/packages/better-auth/src/social-providers/microsoft-entra-id.ts#L87 This operation is cheap (although not free) since we are fully establishing a TLS session only to tare it down. although we dont finish reading the body. there isn't a good reason i see to be doing this.
GiteaMirror added the locked label 2026-04-17 17:18:57 -05:00
Author
Owner

@CarbonNeuron commented on GitHub (May 24, 2025):

Sorry, I should add that if no one gets around to fixing this, I will open a PR to fix these bugs. They're pretty small. Still business critical though haha.

<!-- gh-comment-id:2906241899 --> @CarbonNeuron commented on GitHub (May 24, 2025): Sorry, I should add that if no one gets around to fixing this, I will open a PR to fix these bugs. They're pretty small. Still business critical though haha.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#26665