[GH-ISSUE #5801] getAccessToken not working in stateless mode #10354

Closed
opened 2026-04-13 06:25:55 -05:00 by GiteaMirror · 7 comments
Owner

Originally created by @bartaugust on GitHub (Nov 6, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/5801

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. Create Next.js project without db connection
  2. setup better auth in stateless mode
  3. call authClient.getAccessToken

Current vs. Expected behavior

Current
When in stateless mode using:
const response = await authClient.getAccessToken({
providerId: 'microsoft',
});
throws an error:
{
"code": "ACCOUNT_NOT_FOUND",
"message": "Account not found"
}

Expected
Response with access token

What version of Better Auth are you using?

1.4.0-beta.16

System info

{
  "system": {
    "platform": "darwin",
    "arch": "arm64",
    "version": "Darwin Kernel Version 25.0.0: Wed Sep 17 21:41:50 PDT 2025; root:xnu-12377.1.9~141/RELEASE_ARM64_T6030",
    "release": "25.0.0",
    "cpuCount": 12,
    "cpuModel": "Apple M3 Pro",
    "totalMemory": "18.00 GB",
    "freeMemory": "0.11 GB"
  },
  "node": {
    "version": "v25.1.0",
    "env": "development"
  },
  "packageManager": {
    "name": "pnpm",
    "version": "10.20.0"
  },
  "frameworks": [
    {
      "name": "next",
      "version": "16.0.1"
    },
    {
      "name": "react",
      "version": "19.2.0"
    }
  ],
  "databases": null,
  "betterAuth": {
    "version": "1.4.0-beta.16",
    "config": {
      "plugins": [
        {
          "name": "next-cookies",
          "config": {
            "id": "next-cookies",
            "hooks": {
              "after": [
                {}
              ]
            }
          }
        }
      ],
      "socialProviders": {
        "microsoft": {
          "clientSecret": "",
          "tenantId": "",
          "authority": "https://login.microsoftonline.com",
          "prompt": "select_account"
        }
      },
      "session": {
        "expiresIn": 604800,
        "cookieCache": {
          "enabled": true,
          "maxAge": 2592000,
          "strategy": "jwt",
          "refreshCache": true
        }
      }
    }
  }
}

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

Client

Auth config (if applicable)

const createAuth = () =>
    betterAuth({
        plugins: [nextCookies()],
        socialProviders: {
            microsoft: {
                clientId: process.env.NEXT_PUBLIC_MICROSOFT_CLIENT_ID!,
                clientSecret: process.env.MICROSOFT_CLIENT_SECRET!,
                tenantId: process.env.MICROSOFT_TENANT_ID!,
                authority: 'https://login.microsoftonline.com',
                prompt: 'select_account',
            },
        },
        session: {
            expiresIn: 60 * 60 * 24 * 7,
            cookieCache: {
                enabled: true,
                maxAge: 30 * 24 * 60 * 60,
                strategy: 'jwt',
                refreshCache: true,
            },
        },
    });

Additional context

No response

Originally created by @bartaugust on GitHub (Nov 6, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/5801 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce 1. Create Next.js project without db connection 2. setup better auth in stateless mode 3. call authClient.getAccessToken ### Current vs. Expected behavior Current When in stateless mode using: const response = await authClient.getAccessToken({ providerId: 'microsoft', }); throws an error: { "code": "ACCOUNT_NOT_FOUND", "message": "Account not found" } Expected Response with access token ### What version of Better Auth are you using? 1.4.0-beta.16 ### System info ```bash { "system": { "platform": "darwin", "arch": "arm64", "version": "Darwin Kernel Version 25.0.0: Wed Sep 17 21:41:50 PDT 2025; root:xnu-12377.1.9~141/RELEASE_ARM64_T6030", "release": "25.0.0", "cpuCount": 12, "cpuModel": "Apple M3 Pro", "totalMemory": "18.00 GB", "freeMemory": "0.11 GB" }, "node": { "version": "v25.1.0", "env": "development" }, "packageManager": { "name": "pnpm", "version": "10.20.0" }, "frameworks": [ { "name": "next", "version": "16.0.1" }, { "name": "react", "version": "19.2.0" } ], "databases": null, "betterAuth": { "version": "1.4.0-beta.16", "config": { "plugins": [ { "name": "next-cookies", "config": { "id": "next-cookies", "hooks": { "after": [ {} ] } } } ], "socialProviders": { "microsoft": { "clientSecret": "", "tenantId": "", "authority": "https://login.microsoftonline.com", "prompt": "select_account" } }, "session": { "expiresIn": 604800, "cookieCache": { "enabled": true, "maxAge": 2592000, "strategy": "jwt", "refreshCache": true } } } } } ``` ### Which area(s) are affected? (Select all that apply) Client ### Auth config (if applicable) ```typescript const createAuth = () => betterAuth({ plugins: [nextCookies()], socialProviders: { microsoft: { clientId: process.env.NEXT_PUBLIC_MICROSOFT_CLIENT_ID!, clientSecret: process.env.MICROSOFT_CLIENT_SECRET!, tenantId: process.env.MICROSOFT_TENANT_ID!, authority: 'https://login.microsoftonline.com', prompt: 'select_account', }, }, session: { expiresIn: 60 * 60 * 24 * 7, cookieCache: { enabled: true, maxAge: 30 * 24 * 60 * 60, strategy: 'jwt', refreshCache: true, }, }, }); ``` ### Additional context _No response_
GiteaMirror added the lockedbug labels 2026-04-13 06:25:56 -05:00
Author
Owner

@better-auth-agent[bot] commented on GitHub (Nov 6, 2025):

Hi there, thanks for opening this issue! It looks like when using stateless mode without a database connection, getAccessToken is throwing an ACCOUNT_NOT_FOUND error because no persisted account data is available to generate the token. In stateless setups, Better Auth relies on the session’s cookie cache (using the JWT strategy as in your configuration) to manage and refresh tokens. If the social sign-in process (here with Microsoft) doesn’t create or populate the in-memory account details properly, then the API call won’t find the expected account and will return that error.

Here are some troubleshooting steps you might try:
• Verify that your cookieCache is correctly enabled and configured, and that your JWT strategy is consistently applied throughout the auth flow.
• Double-check that your Microsoft provider configuration supplies all the necessary details for account creation during sign in.
• Consider testing the flow with a temporary account setup or with another provider to see if the issue is specific to Microsoft in this stateless context.

If you continue to experience the issue or need more detailed guidance, please tag @bekacru or @ping__ for further investigation.

tag @better-auth-agent if you would like an answer from the Agent

Does this help clarify the next steps for your setup?

DiagramDiscordGitHub

Diagram Join Star

<!-- gh-comment-id:3496767050 --> @better-auth-agent[bot] commented on GitHub (Nov 6, 2025): Hi there, thanks for opening this issue! It looks like when using stateless mode without a database connection, getAccessToken is throwing an ACCOUNT_NOT_FOUND error because no persisted account data is available to generate the token. In stateless setups, Better Auth relies on the session’s cookie cache (using the JWT strategy as in your configuration) to manage and refresh tokens. If the social sign-in process (here with Microsoft) doesn’t create or populate the in-memory account details properly, then the API call won’t find the expected account and will return that error. Here are some troubleshooting steps you might try: • Verify that your cookieCache is correctly enabled and configured, and that your JWT strategy is consistently applied throughout the auth flow. • Double-check that your Microsoft provider configuration supplies all the necessary details for account creation during sign in. • Consider testing the flow with a temporary account setup or with another provider to see if the issue is specific to Microsoft in this stateless context. If you continue to experience the issue or need more detailed guidance, please tag @bekacru or @ping__ for further investigation. tag @better-auth-agent if you would like an answer from the Agent Does this help clarify the next steps for your setup? <!-- bot:webhook reply v1 --> [Diagram](https://repodiagrams.s3.eu-north-1.amazonaws.com/better-auth_ultra_detailed_interactive.html) • [Discord](https://discord.gg/better-auth) • [GitHub](https://github.com/better-auth/better-auth) [![Diagram](https://img.shields.io/badge/Diagram-2b3137?style=flat-square)](https://repodiagrams.s3.eu-north-1.amazonaws.com/better-auth_ultra_detailed_interactive.html) [![Join](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&style=flat-square)](https://discord.gg/better-auth) [![Star](https://img.shields.io/badge/star-181717?logo=github&logoColor=white&style=flat-square)](https://github.com/better-auth/better-auth)
Author
Owner

@bartaugust commented on GitHub (Nov 6, 2025):

@Bekacru

<!-- gh-comment-id:3496831395 --> @bartaugust commented on GitHub (Nov 6, 2025): @Bekacru
Author
Owner

@rovertrack commented on GitHub (Nov 12, 2025):

@bartaugust is the issue resolved for you?

<!-- gh-comment-id:3521535011 --> @rovertrack commented on GitHub (Nov 12, 2025): @bartaugust is the issue resolved for you?
Author
Owner

@SferaDev commented on GitHub (Nov 12, 2025):

@rovertrack It still happened to me with v1.4.0-beta.18

<!-- gh-comment-id:3521731614 --> @SferaDev commented on GitHub (Nov 12, 2025): @rovertrack It still happened to me with v1.4.0-beta.18
Author
Owner

@bartaugust commented on GitHub (Nov 12, 2025):

@bartaugust is the issue resolved for you?

Unfortunately it is still not resolved

<!-- gh-comment-id:3523990550 --> @bartaugust commented on GitHub (Nov 12, 2025): > [@bartaugust](https://github.com/bartaugust) is the issue resolved for you? Unfortunately it is still not resolved
Author
Owner

@ryami333 commented on GitHub (Nov 17, 2025):

Still broken in v1.4.0-beta.21

By the way – seems to be an issue specific to Next 16, right? I am only having issues since upgrading from 15 to 16.

<!-- gh-comment-id:3542241024 --> @ryami333 commented on GitHub (Nov 17, 2025): Still broken in v1.4.0-beta.21 By the way – seems to be an issue specific to Next 16, right? I am only having issues since upgrading from 15 to 16.
Author
Owner

@SferaDev commented on GitHub (Nov 18, 2025):

I believe this PR will fix it: https://github.com/better-auth/better-auth/pull/6013

<!-- gh-comment-id:3546486648 --> @SferaDev commented on GitHub (Nov 18, 2025): I believe this PR will fix it: https://github.com/better-auth/better-auth/pull/6013
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#10354