[oauth-provider-2.1] Expose OAuth provider request state for external use #2772

Closed
opened 2026-03-13 10:19:17 -05:00 by GiteaMirror · 3 comments
Owner

Originally created by @DibyodyutiMondal on GitHub (Jan 27, 2026).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

create a backend with

  • email enabled
  • oidc 2.1 provider plugin
  • sign-in page
  • an 'after' hook for the '/sign-in/email' endpoint

Now, initiate a oauth authorization flow,
it should send you to the sign-in page, but there will be several query params+exp+sig added to the url
sign-in with credentials
you will see that your query params are being sent as oauth_query, along with email and password
the hook will be triggered, but there is no way to access the oauth state that the plugin created and used while processing the request

Current vs. Expected behavior

in the hook, there is no 'standard' way to access the oauth state. we will have to check for 'oauth_query' which is an internal implementation detail.

the expectation is that we can use something like 'getOauthState' like the main library does it, but maybe that is confusing or is for a different kind of state?
At any rate, I need to access the state in my hook because I am doing some aditional process for the silent session login flows using OIDC

What version of Better Auth are you using?

1.5.0-beta.9

System info

{
  "system": {
    "platform": "linux",
    "arch": "x64",
    "version": "#1 SMP PREEMPT_DYNAMIC Thu Jun  5 18:30:46 UTC 2025",
    "release": "6.6.87.2-microsoft-standard-WSL2",
    "cpuCount": 16,
    "cpuModel": "Intel(R) Core(TM) i9-9900K CPU @ 3.60GHz",
    "totalMemory": "31.34 GB",
    "freeMemory": "17.65 GB"
  },
  "node": {
    "version": "v24.5.0",
    "env": "development"
  },
  "packageManager": {
    "name": "pnpm",
    "version": "10.28.1"
  },
  "frameworks": [
    {
      "name": "next",
      "version": "~16.0.6"
    },
    {
      "name": "react",
      "version": "19.2.0"
    },
    {
      "name": "fastify",
      "version": "5.6.2"
    }
  ],
  "databases": [
    {
      "name": "mongodb",
      "version": "7.0.0"
    }
  ],
  "betterAuth": {
    "version": "1.5.0-beta.9",
    "config": null
  }
}

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

Package

Auth config (if applicable)


Additional context

No response

Originally created by @DibyodyutiMondal on GitHub (Jan 27, 2026). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce create a backend with - email enabled - oidc 2.1 provider plugin - sign-in page - an 'after' hook for the '/sign-in/email' endpoint Now, initiate a oauth authorization flow, it should send you to the sign-in page, but there will be several query params+exp+sig added to the url sign-in with credentials you will see that your query params are being sent as oauth_query, along with email and password the hook will be triggered, but there is no way to access the oauth state that the plugin created and used while processing the request ### Current vs. Expected behavior in the hook, there is no 'standard' way to access the oauth state. we will have to check for 'oauth_query' which is an internal implementation detail. the expectation is that we can use something like 'getOauthState' like the main library does it, but maybe that is confusing or is for a different kind of state? At any rate, I need to access the state in my hook because I am doing some aditional process for the silent session login flows using OIDC ### What version of Better Auth are you using? 1.5.0-beta.9 ### System info ```bash { "system": { "platform": "linux", "arch": "x64", "version": "#1 SMP PREEMPT_DYNAMIC Thu Jun 5 18:30:46 UTC 2025", "release": "6.6.87.2-microsoft-standard-WSL2", "cpuCount": 16, "cpuModel": "Intel(R) Core(TM) i9-9900K CPU @ 3.60GHz", "totalMemory": "31.34 GB", "freeMemory": "17.65 GB" }, "node": { "version": "v24.5.0", "env": "development" }, "packageManager": { "name": "pnpm", "version": "10.28.1" }, "frameworks": [ { "name": "next", "version": "~16.0.6" }, { "name": "react", "version": "19.2.0" }, { "name": "fastify", "version": "5.6.2" } ], "databases": [ { "name": "mongodb", "version": "7.0.0" } ], "betterAuth": { "version": "1.5.0-beta.9", "config": null } } ``` ### Which area(s) are affected? (Select all that apply) Package ### Auth config (if applicable) ```typescript ``` ### Additional context _No response_
GiteaMirror added the plugin label 2026-03-13 10:19:17 -05:00
Author
Owner

@better-auth-agent[bot] commented on GitHub (Jan 27, 2026):

To access the OAuth state in your hook when using the OIDC provider plugin, it's important to note that currently, there isn't a standard method to retrieve that state. You mentioned you want something akin to a getOauthState function. However, accessing the OAuth state typically involves working with the oauth_query, which is considered an internal detail of the implementation.

A potential next step would be to raise this issue on GitHub to discuss the inclusion of a method for obtaining the OAuth state directly, as it currently seems to create some limitations in your workflow [1].

If you have further questions or need specific advice on implementing your hook, feel free to ask! Have you explored any workarounds or alternatives for managing state in your OIDC flow?

If you need more help, tag @better-auth-agent in a comment so I can respond.

DiagramDiscordGitHub

Diagram Join Star

@better-auth-agent[bot] commented on GitHub (Jan 27, 2026): To access the OAuth state in your hook when using the OIDC provider plugin, it's important to note that currently, there isn't a standard method to retrieve that state. You mentioned you want something akin to a `getOauthState` function. However, accessing the OAuth state typically involves working with the `oauth_query`, which is considered an internal detail of the implementation. A potential next step would be to raise this issue on GitHub to discuss the inclusion of a method for obtaining the OAuth state directly, as it currently seems to create some limitations in your workflow [[1]](https://github.com/better-auth/better-auth/issues/4275). If you have further questions or need specific advice on implementing your hook, feel free to ask! Have you explored any workarounds or alternatives for managing state in your OIDC flow? _If you need more help, tag @better-auth-agent in a comment so I can respond._ <!-- 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

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

Hi @DibyodyutiMondal! I'm Dosu and I’m helping the better-auth team.

There isn't a documented or stable way to access the OAuth state in hooks with the OIDC provider plugin. The plugin stores the OAuth request state (including query parameters) in a signed cookie (oidc_login_prompt) when redirecting to the login page, and after login, it rehydrates this into ctx.query for internal use. There is no exported getOauthState helper for this context—so if you need to access the OAuth state in your custom hooks, you currently have to rely on inspecting ctx.query after the plugin's internal logic runs, or manually parse the oidc_login_prompt cookie if it's still present. This approach is not considered stable and may break with future changes, as it's not part of the public API. There is community demand for a more robust accessor or hook parameter, but as of now, this is the only available workaround in Better Auth 1.5.0-beta.9 citation.

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

@dosubot[bot] commented on GitHub (Jan 27, 2026): <!-- Greeting --> Hi @DibyodyutiMondal! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> There isn't a documented or stable way to access the OAuth state in hooks with the OIDC provider plugin. The plugin stores the OAuth request state (including query parameters) in a signed cookie (`oidc_login_prompt`) when redirecting to the login page, and after login, it rehydrates this into `ctx.query` for internal use. There is no exported `getOauthState` helper for this context—so if you need to access the OAuth state in your custom hooks, you currently have to rely on inspecting `ctx.query` after the plugin's internal logic runs, or manually parse the `oidc_login_prompt` cookie if it's still present. This approach is not considered stable and may break with future changes, as it's not part of the public API. There is community demand for a more robust accessor or hook parameter, but as of now, this is the only available workaround in Better Auth 1.5.0-beta.9 [citation](https://github.com/better-auth/better-auth/blob/d1602ef0ae9822e462a5a42497072a0a1756e804/packages/better-auth/src/plugins/oidc-provider/index.ts#L281-L1756). <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/f0793aef-7214-48e3-88ef-427e701c18eb?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/f0793aef-7214-48e3-88ef-427e701c18eb?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/f0793aef-7214-48e3-88ef-427e701c18eb?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/f0793aef-7214-48e3-88ef-427e701c18eb?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/f0793aef-7214-48e3-88ef-427e701c18eb?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/f0793aef-7214-48e3-88ef-427e701c18eb?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/f0793aef-7214-48e3-88ef-427e701c18eb?feedback_type=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)&nbsp;[![Open in Cursor](https://dosu.dev/dosu-in-cursor.svg)](https://cursor.com/link/prompt?text=There%20isn%27t%20a%20documented%20or%20stable%20way%20to%20access%20the%20OAuth%20state%20in%20hooks%20with%20the%20OIDC%20provider%20plugin.%20The%20plugin%20stores%20the%20OAuth%20request%20state%20%28including%20query%20parameters%29%20in%20a%20signed%20cookie%20%28%60oidc_login_prompt%60%29%20when%20redirecting%20to%20the%20login%20page%2C%20and%20after%20login%2C%20it%20rehydrates%20this%20into%20%60ctx.query%60%20for%20internal%20use.%20There%20is%20no%20exported%20%60getOauthState%60%20helper%20for%20this%20context%E2%80%94so%20if%20you%20need%20to%20access%20the%20OAuth%20state%20in%20your%20custom%20hooks%2C%20you%20currently%20have%20to%20rely%20on%20inspecting%20%60ctx.query%60%20after%20the%20plugin%27s%20internal%20logic%20runs%2C%20or%20manually%20parse%20the%20%60oidc_login_prompt%60%20cookie%20if%20it%27s%20still%20present.%20This%20approach%20is%20not%20considered%20stable%20and%20may%20break%20with%20future%20changes%2C%20as%20it%27s%20not%20part%20of%20the%20public%20API.%20There%20is%20community%20demand%20for%20a%20more%20robust%20accessor%20or%20hook%20parameter%2C%20but%20as%20of%20now%2C%20this%20is%20the%20only%20available%20workaround%20in%20Better%20Auth%201.5.0-beta.9%C2%A0%5Bcitation%5D%28https%3A//github.com/better-auth/better-auth/blob/d1602ef0ae9822e462a5a42497072a0a1756e804/packages/better-auth/src/plugins/oidc-provider/index.ts%23L281-L1756%29.)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&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/7630)
Author
Owner

@DibyodyutiMondal commented on GitHub (Jan 27, 2026):

@dosu

there isn't a documented way, yes. But the oidc provider needs to deal with oauth state in different kinds of scenarios.
one example is when it's dealing with direct oidc-spec request, such as to the authorize endpoint
but it may also need to deal with sign-in redirects happening on the authorize endpoint

to deal with this, the oidc provider stores the oauth request state inside a 'before' hook, making it available for later hooks in the plugin.
Here is the location of the constant that handles the per-request oauthState:
426f0b5c22/packages/oauth-provider/src/oauth.ts (L42-L44)

we can see how that constant is being used in the same file.

However, this const is not exposed to the outside, so if someone wanted to add more functionality, they will not have direct access to the initialized and processed oauth state that this plugin has to offer.

@DibyodyutiMondal commented on GitHub (Jan 27, 2026): @dosu there isn't a documented way, yes. But the oidc provider needs to deal with oauth state in different kinds of scenarios. one example is when it's dealing with direct oidc-spec request, such as to the authorize endpoint but it may also need to deal with sign-in redirects happening on the authorize endpoint to deal with this, the oidc provider stores the oauth request state inside a 'before' hook, making it available for later hooks in the plugin. Here is the location of the constant that handles the per-request oauthState: https://github.com/better-auth/better-auth/blob/426f0b5c227d315bea486afdcc42675fb67ab89d/packages/oauth-provider/src/oauth.ts#L42-L44 we can see how that constant is being used in the same file. However, this const is not exposed to the outside, so if someone wanted to add more functionality, they will not have direct access to the initialized and processed oauth state that this plugin has to offer.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#2772