[GH-ISSUE #8806] auth.api.oauth2UserInfo returns body: { error_description: "request not found", error: "invalid_request" }, #11196

Open
opened 2026-04-13 07:32:52 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @GitonioDev on GitHub (Mar 27, 2026).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/8806

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. Call auth.api.oauth2UserInfo

Current vs. Expected behavior

Currently it returns:
[APIError] { status: "UNAUTHORIZED", body: { error_description: "request not found", error: "invalid_request" }, headers: {}, statusCode: 401 }

Expected behavoir: return the user information like when calling via fetch("/oauth2/userinfo")

What version of Better Auth are you using?

1.5.4

System info

{
  "system": {
    "platform": "win32",
    "arch": "x64",
    "version": "Windows 10 IoT Enterprise LTSC 2021",
    "release": "10.0.19044",
    "cpuCount": 28,
    "cpuModel": "Intel(R) Core(TM) i7-14700K",       
    "totalMemory": "31.77 GB",
    "freeMemory": "15.38 GB"
  },
}

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

Backend

Auth config (if applicable)

import { betterAuth } from "better-auth"
const auth = betterAuth({
  disabledPaths: [
    "/token",
  ],
  plugins: [
    jwt(),
    oauthProvider({ 
      loginPage: "/sign-in", 
      consentPage: "/consent", 
      // ...other options
    }) 
  ],
});

Additional context

No response

Originally created by @GitonioDev on GitHub (Mar 27, 2026). Original GitHub issue: https://github.com/better-auth/better-auth/issues/8806 ### Is this suited for github? - [ ] Yes, this is suited for github ### To Reproduce 1. Call auth.api.oauth2UserInfo ### Current vs. Expected behavior Currently it returns: ` [APIError] { status: "UNAUTHORIZED", body: { error_description: "request not found", error: "invalid_request" }, headers: {}, statusCode: 401 } ` Expected behavoir: return the user information like when calling via fetch("/oauth2/userinfo") ### What version of Better Auth are you using? 1.5.4 ### System info ```bash { "system": { "platform": "win32", "arch": "x64", "version": "Windows 10 IoT Enterprise LTSC 2021", "release": "10.0.19044", "cpuCount": 28, "cpuModel": "Intel(R) Core(TM) i7-14700K", "totalMemory": "31.77 GB", "freeMemory": "15.38 GB" }, } ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" const auth = betterAuth({ disabledPaths: [ "/token", ], plugins: [ jwt(), oauthProvider({ loginPage: "/sign-in", consentPage: "/consent", // ...other options }) ], }); ``` ### Additional context _No response_
GiteaMirror added the bug label 2026-04-13 07:32:52 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Mar 27, 2026):

The error "request not found" occurs because the oauth2UserInfo endpoint checks if ctx.request exists and throws this error when it's missing. When calling auth.api.oauth2UserInfo programmatically (vs via HTTP fetch), the HTTP request context may not be properly available.

To fix this, you need to pass the headers explicitly:

await auth.api.oauth2UserInfo({
    headers: new Headers({
        "Authorization": `Bearer ${yourAccessToken}`
    })
});

Unlike fetch("/oauth2/userinfo") which automatically has access to the HTTP context, when using auth.api, you must explicitly pass headers since there's no automatic propagation from the calling environment.

If you're calling this from a server-side context (like Next.js API routes), make sure the HTTP request object is properly passed through to provide the necessary ctx.request context.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other

Chat with Dosu Open in Cursor Join Discord Share on X

<!-- gh-comment-id:4145029714 --> @dosubot[bot] commented on GitHub (Mar 27, 2026): <!-- Answer --> The error `"request not found"` occurs because [the `oauth2UserInfo` endpoint checks if `ctx.request` exists](https://github.com/better-auth/better-auth/blob/00246a473cbd8d37d31b5a5fd740339ade394997/packages/oauth-provider/src/userinfo.ts#L40-L45) and throws this error when it's missing. When calling `auth.api.oauth2UserInfo` programmatically (vs via HTTP fetch), the HTTP request context may not be properly available. To fix this, you need to pass the headers explicitly: ```typescript await auth.api.oauth2UserInfo({ headers: new Headers({ "Authorization": `Bearer ${yourAccessToken}` }) }); ``` Unlike `fetch("/oauth2/userinfo")` which automatically has access to the HTTP context, [when using `auth.api`, you must explicitly pass headers](https://github.com/better-auth/better-auth/issues/7558) since there's no automatic propagation from the calling environment. If you're calling this from a server-side context (like Next.js API routes), make sure the HTTP request object is properly passed through to provide the necessary `ctx.request` context. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot?utm_source=github&utm_medium=bot-comment&utm_campaign=github-assistant&utm_content=brand-link).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/dbac2206-b319-489c-a242-f895d7ce45c1?feedback_type=great_response&utm_source=github&utm_medium=bot-comment&utm_campaign=github-assistant&utm_content=feedback-great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/dbac2206-b319-489c-a242-f895d7ce45c1?feedback_type=irrelevant_answer&utm_source=github&utm_medium=bot-comment&utm_campaign=github-assistant&utm_content=feedback-irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/dbac2206-b319-489c-a242-f895d7ce45c1?feedback_type=incorrect_sources&utm_source=github&utm_medium=bot-comment&utm_campaign=github-assistant&utm_content=feedback-incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/dbac2206-b319-489c-a242-f895d7ce45c1?feedback_type=too_verbose&utm_source=github&utm_medium=bot-comment&utm_campaign=github-assistant&utm_content=feedback-too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/dbac2206-b319-489c-a242-f895d7ce45c1?feedback_type=hallucination&utm_source=github&utm_medium=bot-comment&utm_campaign=github-assistant&utm_content=feedback-hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/dbac2206-b319-489c-a242-f895d7ce45c1?feedback_type=bug_report&utm_source=github&utm_medium=bot-comment&utm_campaign=github-assistant&utm_content=feedback-bug_report) | [Other](https://app.dosu.dev/response-feedback/dbac2206-b319-489c-a242-f895d7ce45c1?feedback_type=other&utm_source=github&utm_medium=bot-comment&utm_campaign=github-assistant&utm_content=feedback-other)</sup> [![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github&utm_medium=bot-comment&utm_campaign=github-assistant&utm_content=chat-badge)&nbsp;[![Open in Cursor](https://dosu.dev/dosu-in-cursor.svg)](https://cursor.com/link/prompt?text=The%20error%20%60%22request%20not%20found%22%60%20occurs%20because%20%5Bthe%20%60oauth2UserInfo%60%20endpoint%20checks%20if%20%60ctx.request%60%20exists%5D%28https%3A//github.com/better-auth/better-auth/blob/00246a473cbd8d37d31b5a5fd740339ade394997/packages/oauth-provider/src/userinfo.ts%23L40-L45%29%20and%20throws%20this%20error%20when%20it%27s%20missing.%20When%20calling%20%60auth.api.oauth2UserInfo%60%20programmatically%20%28vs%20via%20HTTP%20fetch%29%2C%20the%20HTTP%20request%20context%20may%20not%20be%20properly%20available.%0A%0ATo%20fix%20this%2C%20you%20need%20to%20pass%20the%20headers%20explicitly%3A%0A%0A%60%60%60typescript%0Aawait%20auth.api.oauth2UserInfo%28%7B%0A%20%20%20%20headers%3A%20new%20Headers%28%7B%0A%20%20%20%20%20%20%20%20%22Authorization%22%3A%20%60Bearer%20%24%7ByourAccessToken%7D%60%0A%20%20%20%20%7D%29%0A%7D%29%3B%0A%60%60%60%0A%0AUnlike%20%60fetch%28%22/oauth2/userinfo%22%29%60%20which%20automatically%20has%20access%20to%20the%20HTTP%20context%2C%20%5Bwhen%20using%20%60auth.api%60%2C%20you%20must%20explicitly%20pass%20headers%5D%28https%3A//github.com/better-auth/better-auth/issues/7558%29%20since%20there%27s%20no%20automatic%20propagation%20from%20the%20calling%20environment.%0A%0AIf%20you%27re%20calling%20this%20from%20a%20server-side%20context%20%28like%20Next.js%20API%20routes%29%2C%20make%20sure%20the%20HTTP%20request%20object%20is%20properly%20passed%20through%20to%20provide%20the%20necessary%20%60ctx.request%60%20context.)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot?utm_source=github&utm_medium=bot-comment&utm_campaign=github-assistant&utm_content=join-discord)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/8806)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#11196