Issue with magic-link verify #1811

Closed
opened 2026-03-13 09:05:07 -05:00 by GiteaMirror · 3 comments
Owner

Originally created by @maelp on GitHub (Aug 30, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

When I sign with a magic link, the URL in the email gives me something like

http://localhost:3000/auth/magic-link/verify?token=oVAiZlzoqdOdDkVZOprUThxmOflCJJtO&callbackURL=http%3A%2F%2Flocalhost%3A3001%2F

as expected (I guess?)

but then when I click on the link:

  • it doesn't redirect me to my website
  • and it dump the Hono path and context, including all sensitive backend stuff???

eg

{
  "path": "/magic-link/verify",
  "method": "GET",
  "headers": {

  },
  "params": {

  },
  "request": {

  },
  "query": {
    "token": "oVAiZlzoqdOdDkVZOprUThxmOflCJJtO",
    "callbackURL": "http://localhost:3001/"
  },
  "_flag": "router",
  "asResponse": false,
  "context": {
    "appName": "Gouach API",
    "socialProviders": [
      {
        "id": "google",
        "name": "Google",
        "options": {
          "clientId": "fake-client-id",
          "clientSecret": "some-better-auth-social-provider-google-dev-secret"
        }

any idea why?

Current vs. Expected behavior

I would assume first that /magic-link/verify would redirect me to my website, but then that it wouldn't just return the whole backend server context, including all secrets, to the user?

What version of Better Auth are you using?

latest

System info

latest

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

Backend

Auth config (if applicable)

import { betterAuth } from "better-auth"
export const auth = betterAuth({
  emailAndPassword: {  
    enabled: true
  },
});

Additional context

No response

Originally created by @maelp on GitHub (Aug 30, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce When I sign with a magic link, the URL in the email gives me something like http://localhost:3000/auth/magic-link/verify?token=oVAiZlzoqdOdDkVZOprUThxmOflCJJtO&callbackURL=http%3A%2F%2Flocalhost%3A3001%2F as expected (I guess?) but then when I click on the link: - it doesn't redirect me to my website - and it dump the Hono path and context, including all sensitive backend stuff??? eg ```json { "path": "/magic-link/verify", "method": "GET", "headers": { }, "params": { }, "request": { }, "query": { "token": "oVAiZlzoqdOdDkVZOprUThxmOflCJJtO", "callbackURL": "http://localhost:3001/" }, "_flag": "router", "asResponse": false, "context": { "appName": "Gouach API", "socialProviders": [ { "id": "google", "name": "Google", "options": { "clientId": "fake-client-id", "clientSecret": "some-better-auth-social-provider-google-dev-secret" } ``` any idea why? ### Current vs. Expected behavior I would assume first that /magic-link/verify would redirect me to my website, but then that it wouldn't just return the whole backend server context, including all secrets, to the user? ### What version of Better Auth are you using? latest ### System info ```bash latest ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ emailAndPassword: { enabled: true }, }); ``` ### Additional context _No response_
GiteaMirror added the honobug labels 2026-03-13 09:05:07 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Aug 30, 2025):

This behavior isn't expected from Better Auth—the /magic-link/verify endpoint should either redirect to your callback URL or return a sanitized JSON response with session and user info, never backend context or secrets. The code and recent fixes confirm that all error cases are handled by redirection, and successful cases never expose internal context or secrets in the response [source].

If you're seeing a dump of backend context, it's likely due to a misconfiguration or missing error handler in your server setup—possibly at the Hono framework level. This can happen if an error is thrown and not properly caught, causing Hono to return a debug/error response with the full context [source].

To fix this:

  • Make sure you have the magic-link plugin enabled in your Better Auth config.
  • Add a trustedOrigins array to your config that explicitly lists your frontend's origin (including port), for example:
    export const auth = betterAuth({
      emailAndPassword: { enabled: true },
      plugins: [magicLink({ /* ... */ })],
      trustedOrigins: ["http://localhost:3001"],
    });
    
  • Double-check that your callbackURL matches one of the trusted origins exactly (including port).
  • If you have custom error handling or middleware in your Hono setup, ensure it doesn't leak context on errors.

If you still see backend context in responses, review your global error handling and middleware to ensure errors are properly caught and sanitized before being sent to the client.

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 (Aug 30, 2025): <!-- Answer --> This behavior isn't expected from Better Auth—the /magic-link/verify endpoint should either redirect to your callback URL or return a sanitized JSON response with session and user info, never backend context or secrets. The code and recent fixes confirm that all error cases are handled by redirection, and successful cases never expose internal context or secrets in the response [[source](https://github.com/better-auth/better-auth/blob/74b8e50bee8f2894a6fca2867f0391b96ce6d717/packages/better-auth/src/plugins/magic-link/index.ts)]. If you're seeing a dump of backend context, it's likely due to a misconfiguration or missing error handler in your server setup—possibly at the Hono framework level. This can happen if an error is thrown and not properly caught, causing Hono to return a debug/error response with the full context [[source](https://github.com/better-auth/better-auth/blob/74b8e50bee8f2894a6fca2867f0391b96ce6d717/packages/better-auth/src/plugins/magic-link/index.ts)]. To fix this: - Make sure you have the magic-link plugin enabled in your Better Auth config. - Add a trustedOrigins array to your config that explicitly lists your frontend's origin (including port), for example: ```js export const auth = betterAuth({ emailAndPassword: { enabled: true }, plugins: [magicLink({ /* ... */ })], trustedOrigins: ["http://localhost:3001"], }); ``` - Double-check that your callbackURL matches one of the trusted origins exactly (including port). - If you have custom error handling or middleware in your Hono setup, ensure it doesn't leak context on errors. If you still see backend context in responses, review your global error handling and middleware to ensure errors are properly caught and sanitized before being sent to the client. <!-- 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/8a77b061-9c8d-4714-9539-04070a7100c3?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/8a77b061-9c8d-4714-9539-04070a7100c3?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/8a77b061-9c8d-4714-9539-04070a7100c3?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/8a77b061-9c8d-4714-9539-04070a7100c3?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/8a77b061-9c8d-4714-9539-04070a7100c3?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/8a77b061-9c8d-4714-9539-04070a7100c3?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/8a77b061-9c8d-4714-9539-04070a7100c3?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/4322)
Author
Owner

@maelp commented on GitHub (Aug 30, 2025):

It might indeed be a bug with my "after" hook
@dosu should we return some things from the after hooks? or not?

@maelp commented on GitHub (Aug 30, 2025): It might indeed be a bug with my "after" hook @dosu should we return some things from the after hooks? or not?
Author
Owner

@dosubot[bot] commented on GitHub (Aug 30, 2025):

You generally should not return anything from Better Auth "after" hooks—these hooks are meant for side effects like logging, cleanup, or syncing data, not for modifying the HTTP response or leaking context. Returning a value from an "after" hook does not affect the response sent to the client, and examples in the codebase show hooks simply performing their logic and returning implicitly [example]. If you need to handle errors or cleanup, do it explicitly inside the hook.

If you're seeing backend context or secrets exposed, it's likely due to an error or misconfiguration in your hook or global error handling—not because of what you return from the hook [discussion]. Make sure your hooks don't throw uncaught errors or leak sensitive data, and review your error handling to ensure responses are properly sanitized.

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 (Aug 30, 2025): <!-- Answer --> You generally should not return anything from Better Auth "after" hooks—these hooks are meant for side effects like logging, cleanup, or syncing data, not for modifying the HTTP response or leaking context. Returning a value from an "after" hook does not affect the response sent to the client, and examples in the codebase show hooks simply performing their logic and returning implicitly [[example](https://github.com/better-auth/better-auth/issues/3819)]. If you need to handle errors or cleanup, do it explicitly inside the hook. If you're seeing backend context or secrets exposed, it's likely due to an error or misconfiguration in your hook or global error handling—not because of what you return from the hook [[discussion](https://github.com/better-auth/better-auth/issues/3576)]. Make sure your hooks don't throw uncaught errors or leak sensitive data, and review your error handling to ensure responses are properly sanitized. <!-- 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/d5044db7-33cb-4ec7-b988-6c6875472b93?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/d5044db7-33cb-4ec7-b988-6c6875472b93?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/d5044db7-33cb-4ec7-b988-6c6875472b93?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/d5044db7-33cb-4ec7-b988-6c6875472b93?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/d5044db7-33cb-4ec7-b988-6c6875472b93?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/d5044db7-33cb-4ec7-b988-6c6875472b93?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/d5044db7-33cb-4ec7-b988-6c6875472b93?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/4322)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1811