Betterauth is slow on nextjs #2002

Closed
opened 2026-03-13 09:20:13 -05:00 by GiteaMirror · 10 comments
Owner

Originally created by @Stealthwriter on GitHub (Sep 23, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

when I click login it takes 3-4 seconds to finish the auth requests

Current vs. Expected behavior

should be faster

What version of Better Auth are you using?

1.3

System info

I'm using nextjs and supabase

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 @Stealthwriter on GitHub (Sep 23, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce when I click login it takes 3-4 seconds to finish the auth requests ### Current vs. Expected behavior should be faster ### What version of Better Auth are you using? 1.3 ### System info ```bash I'm using nextjs and supabase ``` ### 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 nextjsenhancement labels 2026-03-13 09:20:13 -05:00
Author
Owner

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

There are a few known causes for slow authentication with Better Auth on Next.js and Supabase. One common issue is duplicate or excessive API requests during React hydration, especially if you use the useSession hook in a client component at the top layout, combined with async page components and slow networks. This can lead to hundreds of duplicate /api/auth/get-session requests, significantly slowing down the login process. A workaround is to wrap components using useSession in a hydration guard (like useIsMounted) or delay session initialization until after the component mounts. This can help reduce unnecessary requests and improve performance [source].

Other factors that can slow down authentication include database query speed, password hashing cost, and session creation. Make sure your Supabase instance is in the same region as your backend, your user table is indexed on email, and your password hashing parameters are balanced for both security and speed. Profiling the request lifecycle (for example, with Next.js middleware timing) can help you pinpoint whether the delay is in database access, password hashing, or session creation [source].

No official performance tuning docs exist for this stack, but these steps have helped others resolve similar issues.

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 23, 2025): <!-- Answer --> There are a few known causes for slow authentication with Better Auth on Next.js and Supabase. One common issue is duplicate or excessive API requests during React hydration, especially if you use the `useSession` hook in a client component at the top layout, combined with async page components and slow networks. This can lead to hundreds of duplicate `/api/auth/get-session` requests, significantly slowing down the login process. A workaround is to wrap components using `useSession` in a hydration guard (like `useIsMounted`) or delay session initialization until after the component mounts. This can help reduce unnecessary requests and improve performance [[source]](https://github.com/better-auth/better-auth/issues/4609). Other factors that can slow down authentication include database query speed, password hashing cost, and session creation. Make sure your Supabase instance is in the same region as your backend, your user table is indexed on email, and your password hashing parameters are balanced for both security and speed. Profiling the request lifecycle (for example, with Next.js middleware timing) can help you pinpoint whether the delay is in database access, password hashing, or session creation [[source]](https://github.com/better-auth/better-auth/issues/4762). No official performance tuning docs exist for this stack, but these steps have helped others resolve similar 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/e1d4e1cd-0f49-49e6-9e8e-5dc2d44f7fef?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/e1d4e1cd-0f49-49e6-9e8e-5dc2d44f7fef?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/e1d4e1cd-0f49-49e6-9e8e-5dc2d44f7fef?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/e1d4e1cd-0f49-49e6-9e8e-5dc2d44f7fef?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/e1d4e1cd-0f49-49e6-9e8e-5dc2d44f7fef?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/e1d4e1cd-0f49-49e6-9e8e-5dc2d44f7fef?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/e1d4e1cd-0f49-49e6-9e8e-5dc2d44f7fef?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/4841)
Author
Owner

@ping-maxwell commented on GitHub (Sep 23, 2025):

Is this on production or localhost?

@ping-maxwell commented on GitHub (Sep 23, 2025): Is this on production or localhost?
Author
Owner

@Stealthwriter commented on GitHub (Sep 23, 2025):

Production around 5k active users,

Is it supabase?

On Tue, 23 Sep 2025, 4:49 PM Maxwell @.***> wrote:

ping-maxwell left a comment (better-auth/better-auth#4841)
https://github.com/better-auth/better-auth/issues/4841#issuecomment-3323878485

Is this on production or localhost?


Reply to this email directly, view it on GitHub
https://github.com/better-auth/better-auth/issues/4841#issuecomment-3323878485,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/A6HMVXCMM7WAGGP55H6NJPT3UE6WFAVCNFSM6AAAAACHINL72WVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTGMRTHA3TQNBYGU
.
You are receiving this because you authored the thread.Message ID:
@.***>

@Stealthwriter commented on GitHub (Sep 23, 2025): Production around 5k active users, Is it supabase? On Tue, 23 Sep 2025, 4:49 PM Maxwell ***@***.***> wrote: > *ping-maxwell* left a comment (better-auth/better-auth#4841) > <https://github.com/better-auth/better-auth/issues/4841#issuecomment-3323878485> > > Is this on production or localhost? > > — > Reply to this email directly, view it on GitHub > <https://github.com/better-auth/better-auth/issues/4841#issuecomment-3323878485>, > or unsubscribe > <https://github.com/notifications/unsubscribe-auth/A6HMVXCMM7WAGGP55H6NJPT3UE6WFAVCNFSM6AAAAACHINL72WVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTGMRTHA3TQNBYGU> > . > You are receiving this because you authored the thread.Message ID: > ***@***.***> >
Author
Owner

@Stealthwriter commented on GitHub (Sep 23, 2025):

Server running on google cloud

On Tue, 23 Sep 2025, 5:53 PM Maher Mansour @.***> wrote:

Production around 5k active users,

Is it supabase?

On Tue, 23 Sep 2025, 4:49 PM Maxwell @.***> wrote:

ping-maxwell left a comment (better-auth/better-auth#4841)
https://github.com/better-auth/better-auth/issues/4841#issuecomment-3323878485

Is this on production or localhost?


Reply to this email directly, view it on GitHub
https://github.com/better-auth/better-auth/issues/4841#issuecomment-3323878485,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/A6HMVXCMM7WAGGP55H6NJPT3UE6WFAVCNFSM6AAAAACHINL72WVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTGMRTHA3TQNBYGU
.
You are receiving this because you authored the thread.Message ID:
@.***>

@Stealthwriter commented on GitHub (Sep 23, 2025): Server running on google cloud On Tue, 23 Sep 2025, 5:53 PM Maher Mansour ***@***.***> wrote: > Production around 5k active users, > > Is it supabase? > > On Tue, 23 Sep 2025, 4:49 PM Maxwell ***@***.***> wrote: > >> *ping-maxwell* left a comment (better-auth/better-auth#4841) >> <https://github.com/better-auth/better-auth/issues/4841#issuecomment-3323878485> >> >> Is this on production or localhost? >> >> — >> Reply to this email directly, view it on GitHub >> <https://github.com/better-auth/better-auth/issues/4841#issuecomment-3323878485>, >> or unsubscribe >> <https://github.com/notifications/unsubscribe-auth/A6HMVXCMM7WAGGP55H6NJPT3UE6WFAVCNFSM6AAAAACHINL72WVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTGMRTHA3TQNBYGU> >> . >> You are receiving this because you authored the thread.Message ID: >> ***@***.***> >> >
Author
Owner

@Stealthwriter commented on GitHub (Sep 23, 2025):

anyone knows how I can debug nextjs to see what is consuming the most time?
in server and client

On Tue, Sep 23, 2025 at 4:14 PM dosubot[bot] @.***>
wrote:

dosubot[bot] left a comment (better-auth/better-auth#4841)
https://github.com/better-auth/better-auth/issues/4841#issuecomment-3323754661

There are a few known causes for slow authentication with Better Auth on
Next.js and Supabase. One common issue is duplicate or excessive API
requests during React hydration, especially if you use the useSession
hook in a client component at the top layout, combined with async page
components and slow networks. This can lead to hundreds of duplicate
/api/auth/get-session requests, significantly slowing down the login
process. A workaround is to wrap components using useSession in a
hydration guard (like useIsMounted) or delay session initialization until
after the component mounts. This can help reduce unnecessary requests and
improve performance [source]
https://github.com/better-auth/better-auth/issues/4609.

Other factors that can slow down authentication include database query
speed, password hashing cost, and session creation. Make sure your Supabase
instance is in the same region as your backend, your user table is indexed
on email, and your password hashing parameters are balanced for both
security and speed. Profiling the request lifecycle (for example, with
Next.js middleware timing) can help you pinpoint whether the delay is in
database access, password hashing, or session creation [source]
https://github.com/better-auth/better-auth/issues/4762.

No official performance tuning docs exist for this stack, but these steps
have helped others resolve similar issues.

To reply, just mention @dosu https://go.dosu.dev/dosubot.

How did I do? Good
https://app.dosu.dev/response-feedback/e1d4e1cd-0f49-49e6-9e8e-5dc2d44f7fef?feedback_type=great_response
| Irrelevant
https://app.dosu.dev/response-feedback/e1d4e1cd-0f49-49e6-9e8e-5dc2d44f7fef?feedback_type=irrelevant_answer
| Incorrect
https://app.dosu.dev/response-feedback/e1d4e1cd-0f49-49e6-9e8e-5dc2d44f7fef?feedback_type=incorrect_sources
| Verbose
https://app.dosu.dev/response-feedback/e1d4e1cd-0f49-49e6-9e8e-5dc2d44f7fef?feedback_type=too_verbose
| Hallucination
https://app.dosu.dev/response-feedback/e1d4e1cd-0f49-49e6-9e8e-5dc2d44f7fef?feedback_type=hallucination
| Report 🐛
https://app.dosu.dev/response-feedback/e1d4e1cd-0f49-49e6-9e8e-5dc2d44f7fef?feedback_type=bug_report
| Other
https://app.dosu.dev/response-feedback/e1d4e1cd-0f49-49e6-9e8e-5dc2d44f7fef?feedback_type=other
[image: Chat with Dosu]
https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github
[image: Join Discord] https://go.dosu.dev/discord-bot [image: Share on
X]
https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/4841


Reply to this email directly, view it on GitHub
https://github.com/better-auth/better-auth/issues/4841#issuecomment-3323754661,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/A6HMVXC4LH3JTD7JAZQEQX33UE2SXAVCNFSM6AAAAACHINL72WVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTGMRTG42TINRWGE
.
You are receiving this because you authored the thread.Message ID:
@.***>

@Stealthwriter commented on GitHub (Sep 23, 2025): anyone knows how I can debug nextjs to see what is consuming the most time? in server and client On Tue, Sep 23, 2025 at 4:14 PM dosubot[bot] ***@***.***> wrote: > *dosubot[bot]* left a comment (better-auth/better-auth#4841) > <https://github.com/better-auth/better-auth/issues/4841#issuecomment-3323754661> > > There are a few known causes for slow authentication with Better Auth on > Next.js and Supabase. One common issue is duplicate or excessive API > requests during React hydration, especially if you use the useSession > hook in a client component at the top layout, combined with async page > components and slow networks. This can lead to hundreds of duplicate > /api/auth/get-session requests, significantly slowing down the login > process. A workaround is to wrap components using useSession in a > hydration guard (like useIsMounted) or delay session initialization until > after the component mounts. This can help reduce unnecessary requests and > improve performance [source] > <https://github.com/better-auth/better-auth/issues/4609>. > > Other factors that can slow down authentication include database query > speed, password hashing cost, and session creation. Make sure your Supabase > instance is in the same region as your backend, your user table is indexed > on email, and your password hashing parameters are balanced for both > security and speed. Profiling the request lifecycle (for example, with > Next.js middleware timing) can help you pinpoint whether the delay is in > database access, password hashing, or session creation [source] > <https://github.com/better-auth/better-auth/issues/4762>. > > No official performance tuning docs exist for this stack, but these steps > have helped others resolve similar issues. > > *To reply, just mention @dosu <https://go.dosu.dev/dosubot>.* > ------------------------------ > > How did I do? Good > <https://app.dosu.dev/response-feedback/e1d4e1cd-0f49-49e6-9e8e-5dc2d44f7fef?feedback_type=great_response> > | Irrelevant > <https://app.dosu.dev/response-feedback/e1d4e1cd-0f49-49e6-9e8e-5dc2d44f7fef?feedback_type=irrelevant_answer> > | Incorrect > <https://app.dosu.dev/response-feedback/e1d4e1cd-0f49-49e6-9e8e-5dc2d44f7fef?feedback_type=incorrect_sources> > | Verbose > <https://app.dosu.dev/response-feedback/e1d4e1cd-0f49-49e6-9e8e-5dc2d44f7fef?feedback_type=too_verbose> > | Hallucination > <https://app.dosu.dev/response-feedback/e1d4e1cd-0f49-49e6-9e8e-5dc2d44f7fef?feedback_type=hallucination> > | Report 🐛 > <https://app.dosu.dev/response-feedback/e1d4e1cd-0f49-49e6-9e8e-5dc2d44f7fef?feedback_type=bug_report> > | Other > <https://app.dosu.dev/response-feedback/e1d4e1cd-0f49-49e6-9e8e-5dc2d44f7fef?feedback_type=other> > [image: Chat with Dosu] > <https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github> > [image: Join Discord] <https://go.dosu.dev/discord-bot> [image: Share on > X] > <https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/4841> > > — > Reply to this email directly, view it on GitHub > <https://github.com/better-auth/better-auth/issues/4841#issuecomment-3323754661>, > or unsubscribe > <https://github.com/notifications/unsubscribe-auth/A6HMVXC4LH3JTD7JAZQEQX33UE2SXAVCNFSM6AAAAACHINL72WVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTGMRTG42TINRWGE> > . > You are receiving this because you authored the thread.Message ID: > ***@***.***> >
Author
Owner

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

To debug what's consuming time in your Next.js app (server and client), here are practical steps:

Server-side (API/auth routes):

  • Add timing logs around your API handlers. For example, wrap your Better Auth handler with console.time/console.timeEnd or log timestamps before and after calling the handler to measure request duration.
  • Use Node.js profilers like clinic.js or node --inspect to profile CPU and async operations.
  • Log database query durations (if using Supabase, enable query logging or use the dashboard to check slow queries).
  • If you suspect password hashing, try lowering the cost factor (for bcrypt/scrypt) temporarily to see if it speeds things up.

Client-side:

  • Use Chrome DevTools' Performance tab to record and analyze what happens during login—look for long network requests or slow rendering.
  • Use the Network tab to check for duplicate or slow /api/auth/get-session requests. If you see many, you may be hitting the duplicate requests during hydration issue.
  • Use React DevTools to profile component rendering and see if any component is blocking the UI.

Better Auth specifics:

  • Better Auth does not have a comprehensive debug mode or detailed built-in logging for authentication flows. Even with debug logging enabled, you may see little or no output for some endpoints, which is normal and confirmed by maintainers.
  • You can wrap your API handlers to log request URLs and timing, but much of Better Auth's internal logic is a "black box" for now.
  • On the client, Better Auth supports lifecycle hooks (onRequest, onResponse, etc.) for fetch requests—use these to log or time API calls and spot bottlenecks.

Workflow:
Start by timing your API/auth route handlers and database queries. Check for duplicate client requests. Use browser and Node.js profilers to pinpoint slow steps (network, database, hashing, session creation). If you see hundreds of /api/auth/get-session requests, implement a hydration guard for useSession.

Let me know if you want code snippets for any of these steps!

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 23, 2025): <!-- Answer --> To debug what's consuming time in your Next.js app (server and client), here are practical steps: **Server-side (API/auth routes):** - Add timing logs around your API handlers. For example, wrap your Better Auth handler with `console.time`/`console.timeEnd` or log timestamps before and after calling the handler to measure request duration. - Use Node.js profilers like `clinic.js` or `node --inspect` to profile CPU and async operations. - Log database query durations (if using Supabase, enable query logging or use the dashboard to check slow queries). - If you suspect password hashing, try lowering the cost factor (for bcrypt/scrypt) temporarily to see if it speeds things up. **Client-side:** - Use Chrome DevTools' Performance tab to record and analyze what happens during login—look for long network requests or slow rendering. - Use the Network tab to check for duplicate or slow `/api/auth/get-session` requests. If you see many, you may be hitting the [duplicate requests during hydration issue](https://github.com/better-auth/better-auth/issues/4609). - Use React DevTools to profile component rendering and see if any component is blocking the UI. **Better Auth specifics:** - Better Auth does not have a comprehensive debug mode or detailed built-in logging for authentication flows. Even with debug logging enabled, you may see little or no output for some endpoints, which is [normal and confirmed by maintainers](https://github.com/better-auth/better-auth/issues/3169). - You can wrap your API handlers to log request URLs and timing, but much of Better Auth's internal logic is a "black box" for now. - On the client, Better Auth supports lifecycle hooks (`onRequest`, `onResponse`, etc.) for fetch requests—use these to log or time API calls and spot bottlenecks. **Workflow:** Start by timing your API/auth route handlers and database queries. Check for duplicate client requests. Use browser and Node.js profilers to pinpoint slow steps (network, database, hashing, session creation). If you see hundreds of `/api/auth/get-session` requests, implement a hydration guard for `useSession`. Let me know if you want code snippets for any of these steps! <!-- 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/9b8610fb-191e-4bd0-85f0-24e3c34f8bb3?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/9b8610fb-191e-4bd0-85f0-24e3c34f8bb3?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/9b8610fb-191e-4bd0-85f0-24e3c34f8bb3?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/9b8610fb-191e-4bd0-85f0-24e3c34f8bb3?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/9b8610fb-191e-4bd0-85f0-24e3c34f8bb3?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/9b8610fb-191e-4bd0-85f0-24e3c34f8bb3?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/9b8610fb-191e-4bd0-85f0-24e3c34f8bb3?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/4841)
Author
Owner

@ping-maxwell commented on GitHub (Sep 23, 2025):

You could try to enable adapter debug logs to see and compare timestamps to see if it's coming from the DB taking some time

Image
@ping-maxwell commented on GitHub (Sep 23, 2025): You could try to enable adapter debug logs to see and compare timestamps to see if it's coming from the DB taking some time <img width="657" height="53" alt="Image" src="https://github.com/user-attachments/assets/0cdd6de8-1985-4f34-92ef-b68521864aed" />
Author
Owner

@Bekacru commented on GitHub (Sep 23, 2025):

Things I'd recommend to improve performance

  • Indexing DB fields
  • Enabling Cookie Cache
  • Using Secondary Storage
@Bekacru commented on GitHub (Sep 23, 2025): Things I'd recommend to improve performance - Indexing DB fields - Enabling Cookie Cache - Using Secondary Storage
Author
Owner

@Stealthwriter commented on GitHub (Sep 24, 2025):

I thought better auth do indexing by default when migrating

do you know what columns i need to index?

@Stealthwriter commented on GitHub (Sep 24, 2025): I thought better auth do indexing by default when migrating do you know what columns i need to index?
Author
Owner

@ping-maxwell commented on GitHub (Sep 24, 2025):

I thought better auth do indexing by default when migrating

It's definitely planned that we will do this in the future

do you know what columns i need to index?

Have a look at this table:
https://www.better-auth.com/docs/guides/optimizing-for-performance#recommended-fields-to-index

@ping-maxwell commented on GitHub (Sep 24, 2025): > I thought better auth do indexing by default when migrating It's definitely planned that we will do this in the future > do you know what columns i need to index? Have a look at this table: https://www.better-auth.com/docs/guides/optimizing-for-performance#recommended-fields-to-index
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#2002