[GH-ISSUE #3238] ElysiaJS endpoints not working with BetterAuth #26859

Closed
opened 2026-04-17 17:34:44 -05:00 by GiteaMirror · 3 comments
Owner

Originally created by @cholasimmons on GitHub (Jul 1, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/3238

I have shared my demo project here.
Running the latest Elysia, Prisma, and BetterAuth on Windows 11. I've followed the instructions on Elysia's website for integrating betterAuth including generating the schema and migrating it.

/auth endpoints (my custom betterAuth endpoints) are getting captured without errors but also not producing any results.
REST client is showing 404 while Elysia is logging code 200.

Coming from Lucia v3 I was expecting some errors but not this early in the process.

What am I missing? I've been at this for 3 days now

Originally created by @cholasimmons on GitHub (Jul 1, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/3238 I have shared my demo project [here](https://GitHub.com/cholasimmons/furr-buddies_api_elysia). Running the latest Elysia, Prisma, and BetterAuth on Windows 11. I've followed the instructions on Elysia's website for integrating betterAuth including generating the schema and migrating it. `/auth` endpoints (my custom betterAuth endpoints) are getting captured without errors but also not producing any results. [REST client](https://yaak.app) is showing 404 while Elysia is logging code 200. Coming from Lucia v3 I was expecting **some** errors but not this early in the process. What am I missing? I've been at this for 3 days now
Author
Owner

@dosubot[bot] commented on GitHub (Jul 1, 2025):

The most common cause for your symptoms (404 in REST client, 200 in Elysia logs, no errors) is that the BetterAuth handler isn’t mounted at the correct path or isn’t receiving requests in the format it expects. For ElysiaJS, BetterAuth expects to be mounted using .mount(auth.handler) at the exact path prefix you want to serve auth endpoints from—typically /api/auth/* (or whatever your basePath is) [docs].

Here’s the minimal working pattern:

import { Elysia } from "elysia";
import { auth } from "./auth";

const app = new Elysia().mount(auth.handler).listen(3000);

If you want to use a custom handler (for example, to restrict methods), use the official pattern:

import { Elysia, Context } from "elysia";
import { auth } from "./auth";

const betterAuthView = (context: Context) => {
  const ALLOWED = ["POST", "GET"];
  if (ALLOWED.includes(context.request.method)) {
    return auth.handler(context.request);
  }
  context.error(405);
};

const app = new Elysia().all("/api/auth/*", betterAuthView).listen(3000);

[source]

Key things to check:

  • The handler must be mounted at the same path you’re hitting from your REST client (e.g., /api/auth/*). If you mount at /auth/* but your client requests /api/auth/*, you’ll get 404s [docs].
  • Elysia’s .mount() and .all() must pass a standard web Request object to BetterAuth. If you use a custom handler, make sure you’re not changing the request shape.
  • If you use a different base path, make sure to pass the full URL (including the path) to the handler [docs].
  • Middleware or route ordering can interfere. Make sure no other middleware is intercepting or modifying requests before they reach BetterAuth [source].

If you’re still seeing 404s, double-check that your endpoints are actually defined and that you’re not hitting a path that BetterAuth doesn’t recognize. If you want to expose custom endpoints, make sure they’re registered in the BetterAuth config or as Elysia routes outside the auth handler [source].

If you share your actual Elysia server setup code, I can help spot any subtle issues.

To reply, just mention @dosu.


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

<!-- gh-comment-id:3024600970 --> @dosubot[bot] commented on GitHub (Jul 1, 2025): <!-- Answer --> The most common cause for your symptoms (404 in REST client, 200 in Elysia logs, no errors) is that the BetterAuth handler isn’t mounted at the correct path or isn’t receiving requests in the format it expects. For ElysiaJS, BetterAuth expects to be mounted using `.mount(auth.handler)` at the exact path prefix you want to serve auth endpoints from—typically `/api/auth/*` (or whatever your basePath is) [[docs](https://github.com/better-auth/better-auth/blob/12e15cce41fceb36ddefcfc39fc66e082b8af699/docs/content/docs/integrations/elysia.mdx)]. Here’s the minimal working pattern: ```ts import { Elysia } from "elysia"; import { auth } from "./auth"; const app = new Elysia().mount(auth.handler).listen(3000); ``` If you want to use a custom handler (for example, to restrict methods), use the official pattern: ```ts import { Elysia, Context } from "elysia"; import { auth } from "./auth"; const betterAuthView = (context: Context) => { const ALLOWED = ["POST", "GET"]; if (ALLOWED.includes(context.request.method)) { return auth.handler(context.request); } context.error(405); }; const app = new Elysia().all("/api/auth/*", betterAuthView).listen(3000); ``` [[source](https://github.com/better-auth/better-auth/pull/213)] Key things to check: - The handler must be mounted at the same path you’re hitting from your REST client (e.g., `/api/auth/*`). If you mount at `/auth/*` but your client requests `/api/auth/*`, you’ll get 404s [[docs](https://better-auth.com/docs/installation)]. - Elysia’s `.mount()` and `.all()` must pass a standard web `Request` object to BetterAuth. If you use a custom handler, make sure you’re not changing the request shape. - If you use a different base path, make sure to pass the full URL (including the path) to the handler [[docs](https://better-auth.com/docs/installation)]. - Middleware or route ordering can interfere. Make sure no other middleware is intercepting or modifying requests before they reach BetterAuth [[source](https://github.com/better-auth/better-auth/issues/2975)]. If you’re still seeing 404s, double-check that your endpoints are actually defined and that you’re not hitting a path that BetterAuth doesn’t recognize. If you want to expose custom endpoints, make sure they’re registered in the BetterAuth config or as Elysia routes outside the auth handler [[source](https://github.com/better-auth/better-auth/issues/2959)]. If you share your actual Elysia server setup code, I can help spot any subtle issues. <!-- 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/4d00058c-1a88-4cb6-a42e-f567e8094abf?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/4d00058c-1a88-4cb6-a42e-f567e8094abf?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/4d00058c-1a88-4cb6-a42e-f567e8094abf?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/4d00058c-1a88-4cb6-a42e-f567e8094abf?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/4d00058c-1a88-4cb6-a42e-f567e8094abf?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/4d00058c-1a88-4cb6-a42e-f567e8094abf?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/4d00058c-1a88-4cb6-a42e-f567e8094abf?feedback_type=other)</sup>&nbsp;&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/3238)
Author
Owner

@cholasimmons commented on GitHub (Jul 1, 2025):

Thank you @dosu, unless I've missed a step, I believe I've done everything as you've said, though my custom endpoint is "/auth" .
I attached a link to my repo in the very first sentence where it says "here"

Let me try using default instructions and see how that goes.

<!-- gh-comment-id:3025089684 --> @cholasimmons commented on GitHub (Jul 1, 2025): Thank you @dosu, unless I've missed a step, I believe I've done everything as you've said, though my custom endpoint is "/auth" . I attached a link to my repo in the very first sentence where it says ["here"](https://GitHub.com/cholasimmons/furr-buddies_api_elysia) Let me try using default instructions and see how that goes.
Author
Owner

@dosubot[bot] commented on GitHub (Jul 1, 2025):

If your Elysia app mounts BetterAuth at /auth, all your REST client requests must use /auth/* (not /api/auth/* or any other prefix). A 404 in your client with a 200 in Elysia logs almost always means the request path doesn't match the handler's mount path, or the handler isn't set up as a catch-all for subroutes. Double-check that your .mount(auth.handler) or .all("/auth/*", handler) matches exactly what your client is calling, and that you aren't missing the wildcard for subroutes if you expect /auth/signin, /auth/callback, etc. Docs

If you try the default instructions, make sure to update both your server and your REST client to use the same path. If you still see issues, try adding a simple .get("/ping", () => "pong") route to confirm your server is routing as expected. Middleware order and CORS settings can also affect things, but path mismatches are by far the most common cause for this symptom set.

To reply, just mention @dosu.


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

<!-- gh-comment-id:3025113876 --> @dosubot[bot] commented on GitHub (Jul 1, 2025): <!-- Answer --> If your Elysia app mounts BetterAuth at `/auth`, all your REST client requests must use `/auth/*` (not `/api/auth/*` or any other prefix). A 404 in your client with a 200 in Elysia logs almost always means the request path doesn't match the handler's mount path, or the handler isn't set up as a catch-all for subroutes. Double-check that your `.mount(auth.handler)` or `.all("/auth/*", handler)` matches exactly what your client is calling, and that you aren't missing the wildcard for subroutes if you expect `/auth/signin`, `/auth/callback`, etc. [Docs](https://github.com/better-auth/better-auth/blob/5ead4ab2f277a121a1da188c0e172ebb633fb7a0/docs/content/docs/integrations/elysia.mdx) If you try the default instructions, make sure to update both your server and your REST client to use the same path. If you still see issues, try adding a simple `.get("/ping", () => "pong")` route to confirm your server is routing as expected. Middleware order and CORS settings can also affect things, but path mismatches are by far the most common cause for this symptom set. <!-- 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/dd81ce3c-b4ca-4262-b3b0-e862b4df1cae?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/dd81ce3c-b4ca-4262-b3b0-e862b4df1cae?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/dd81ce3c-b4ca-4262-b3b0-e862b4df1cae?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/dd81ce3c-b4ca-4262-b3b0-e862b4df1cae?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/dd81ce3c-b4ca-4262-b3b0-e862b4df1cae?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/dd81ce3c-b4ca-4262-b3b0-e862b4df1cae?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/dd81ce3c-b4ca-4262-b3b0-e862b4df1cae?feedback_type=other)</sup>&nbsp;&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/3238)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#26859