#page reload on SignIn betterauth

8 messages · Page 1 of 1 (latest)

hexed basalt
#

Hi there,

so I finally managed to signIn as a user with convex-better auth. The problem I have is that it is immediatly reloading the page, and then it will not remember my sign in.

My signIn:

 const signIn = async () => {
        await authClient.signIn.email({
            email: "[email protected]",
            password: "coolpassword",
            rememberMe: true
        }, {
            onError: (ctx) => {
                alert(ctx.error.message)
            },
            onSuccess: () => {
                console.log("loggedIn")
            }
        })
    }

console logs:
logged in
page reloads and
convex dev throws the error:
[CONVEX H(GET /api/auth/get-session)] Client disconnected

note i have no callbackRoute set, I dont understand why its refreshing the page...
does anybody know what could cause this behaviour?

spice isleBOT
#

Hi @hexed basalt. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Based on the information in the knowledge sources, there are a few things to consider regarding your issue with Convex Better Auth:

  1. Unexpected Page Reloads:
    The page reload after sign-in is not the default or expected behavior for Convex Better Auth. Typically, after a successful sign-in, you should remain on the same page unless a redirect or reload is explicitly triggered by your code or by the auth library due to a configuration or error.

  2. Session Not Remembered:
    If the session is not being remembered after the reload, it could be related to how cookies or tokens are being set and persisted. In some cases, issues with cookies (such as not being set correctly, or being set as session cookies that are cleared on reload) can cause the authentication state to be lost. There is a known issue where, if cookie expiration is not set, cookies may be treated as session cookies and cleared when the browser session ends or reloads. Setting a maxAge in your cookie/session config can resolve this (see this discussion).

  3. No Callback Route:
    Not setting a callbackRoute should not, by itself, cause a reload. However, some authentication flows (especially those involving redirects or OAuth) may expect a callback route to complete the process. If the flow is incomplete, it could result in unexpected behavior.

  4. Console Error:
    The error [CONVEX H(GET /api/auth/get-session)] Client disconnected suggests that the client lost connection to the Convex backend, possibly due to the reload or a network issue.

#
  1. Known Issues with Token Availability:
    There have been reports where, after sign-in, the JWT or session token is not immediately available until a manual refresh. This was addressed in newer versions of Better Auth, so ensure you are using the latest version (see this thread).

What you can try:

  • Set Cookie Expiration:
    Make sure your session or cookie configuration includes a maxAge so that cookies persist across reloads:

    { cookieConfig: { maxAge: 60 * 60 * 24 * 30 } }
    

    This ensures the session is remembered after a reload (source).

  • Check for Unintentional Reloads:
    Review your code and any auth configuration for places where a reload or redirect might be triggered after sign-in.

  • Update Better Auth:
    Ensure you are using the latest version of Better Auth, as some issues with token availability and session persistence have been fixed in recent releases (source).

  • Debugging:
    Enable verbose logging in your client and backend to get more insight into what happens during sign-in and after the reload (debugging guide).

If after these steps the issue persists, it may be a bug or a configuration issue specific to your setup. In that case, providing a minimal reproducible example (as suggested in the support threads) can help the Convex team assist you further.

Let me know if you need more specific guidance on any of these steps!

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@hexed basalt Kapa.ai is still learning and improving, please let me know how I did by reacting below.

hexed basalt
#

@spice isle I dont use next.js , where would I put coockieCOnfig?

spice isleBOT
#

Let me check the knowledge base for you again. One moment! :books:

#

I'm sorry, but the knowledge sources do not provide explicit information about where to set cookieConfig when using Convex Better Auth outside of Next.js. All references to cookieConfig in the sources are specific to Next.js middleware configuration (e.g., in middleware.ts with convexAuthNextjsMiddleware) and do not mention usage in other frameworks or plain React setups Authz Next.js.

If you are not using Next.js, the documentation does not clarify where or how to set cookieConfig for session persistence. You may need to consult the Better Auth documentation directly or ask in the Convex Discord for framework-specific guidance.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: