MagicLink unnecessary Pending state of authClient.useSession() #1825

Closed
opened 2026-03-13 09:05:53 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @ArtGurianov on GitHub (Sep 2, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. Implement basic SignIn form with some state for showing results using Magic Link (or any multi-step provider)
  2. Wrap all different options of your SignIn client pages with SignedOutGuard at layout level making sure that only unauthorized users can access those pages
  3. Try to Sign In with Magic Link

Current vs. Expected behavior

Current Behavior:

  • Sending magic link email unnecessarily turns session state into Pending, leading to Guard triggers in my case. It might cause other unexpected triggers depending on business logic - the most important is that at this step it doesn't have anything to do with session YET.
  • Current implementation of BetterAuth plugins treats any endpoint's hit as a potential session mutating, hence switching the state into pending. This is exactly the case for 1-step auth flows, but not for multi-step flows.

Expecting Behavior:

  • Expecting to switch session state only at steps where session is involved.

What version of Better Auth are you using?

latest

System info

React + Next.js App Router

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

Client

Auth config (if applicable)


Additional context

One possible solution would be to pass an extra options at authClient's plugin config, letting developer chose the exact auth route or number of routes, where session mutation is expected.

Originally created by @ArtGurianov on GitHub (Sep 2, 2025). ### Is this suited for github? - Yes, this is suited for github ### To Reproduce 1. Implement basic SignIn form with some state for showing results using Magic Link (or any multi-step provider) 2. Wrap all different options of your SignIn client pages with SignedOutGuard at layout level making sure that only unauthorized users can access those pages 3. Try to Sign In with Magic Link ### Current vs. Expected behavior Current Behavior: - Sending magic link email unnecessarily turns session state into Pending, leading to Guard triggers in my case. It might cause other unexpected triggers depending on business logic - the most important is that at this step it doesn't have anything to do with session YET. - Current implementation of BetterAuth plugins treats any endpoint's hit as a potential session mutating, hence switching the state into pending. This is exactly the case for 1-step auth flows, but not for multi-step flows. Expecting Behavior: - Expecting to switch session state only at steps where session is involved. ### What version of Better Auth are you using? latest ### System info ```bash React + Next.js App Router ``` ### Which area(s) are affected? (Select all that apply) Client ### Auth config (if applicable) ```typescript ``` ### Additional context One possible solution would be to pass an extra options at authClient's plugin config, letting developer chose the exact auth route or number of routes, where session mutation is expected.
GiteaMirror added the nextjs label 2026-03-13 09:05:53 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Sep 2, 2025):

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

You’re right—Better Auth currently treats any auth endpoint hit as potentially mutating the session, so sending a magic link email sets the session state to Pending even though no session is actually created yet. This behavior is hardcoded and works for 1-step flows, but it’s not ideal for multi-step flows like Magic Link, where session state should only change after verification. There’s no config option in the plugin or authClient to control which endpoints trigger session mutation or Pending state, so your suggestion for a more granular option is valid and not currently supported. This can cause issues with guards like <SignedOutGuard> that rely on session state for access control, as you’ve described.

A possible workaround for now is to handle Pending state in your guard or UI logic, distinguishing between “auth in progress” and “authenticated” states until this is configurable in Better Auth. If you want to propose this as a feature, your reasoning and use case are well-supported by the current code and plugin structure.
See supporting code details

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Chat with Dosu Join Discord Share on X

@dosubot[bot] commented on GitHub (Sep 2, 2025): <!-- Greeting --> Hi @ArtGurianov! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> You’re right—Better Auth currently treats any auth endpoint hit as potentially mutating the session, so sending a magic link email sets the session state to Pending even though no session is actually created yet. This behavior is hardcoded and works for 1-step flows, but it’s not ideal for multi-step flows like Magic Link, where session state should only change after verification. There’s no config option in the plugin or authClient to control which endpoints trigger session mutation or Pending state, so your suggestion for a more granular option is valid and not currently supported. This can cause issues with guards like `<SignedOutGuard>` that rely on session state for access control, as you’ve described. A possible workaround for now is to handle Pending state in your guard or UI logic, distinguishing between “auth in progress” and “authenticated” states until this is configurable in Better Auth. If you want to propose this as a feature, your reasoning and use case are well-supported by the current code and plugin structure. [See supporting code details](https://github.com/better-auth/better-auth/blob/cf7f2f118147b6a79a99fcf48898408f038390f5/packages/better-auth/src/plugins/magic-link/index.ts) <!-- 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/765e192d-626c-4fa4-92c3-419d1448c1b0?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/765e192d-626c-4fa4-92c3-419d1448c1b0?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/765e192d-626c-4fa4-92c3-419d1448c1b0?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/765e192d-626c-4fa4-92c3-419d1448c1b0?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/765e192d-626c-4fa4-92c3-419d1448c1b0?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/765e192d-626c-4fa4-92c3-419d1448c1b0?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/765e192d-626c-4fa4-92c3-419d1448c1b0?feedback_type=other)</sup>&nbsp;&nbsp;[![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github)&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/4357)
Author
Owner

@mawallace commented on GitHub (Feb 28, 2026):

This is still happening for me at 1.4.19 — should this be reopened?

@mawallace commented on GitHub (Feb 28, 2026): This is still happening for me at 1.4.19 — should this be reopened?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1825