useSession with React JS - unable to get the updated values #1233

Closed
opened 2026-03-13 08:29:26 -05:00 by GiteaMirror · 3 comments
Owner

Originally created by @Tholkappiar on GitHub (May 19, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

React client side code :

React version : ^19.1.0

import { authClient } from "../lib/auth-client";

export function Temp() {
    const { data: session, isPending, error } = authClient.useSession();

    return (
        <div>
            {session ? (
                <>
                    {/* after the session expiry which in cookies, i am still getting 
                    these values - meaning not re-rendered with updated null state when the session expired */}

                    <div>Logged in as: {session.user.name}</div>
                    <div>is Pending : {isPending}</div>
                    <div>is error : {error?.error}</div>
                </>
            ) : (
                <>
                    <div>Not logged in</div>
                    <div>is Pending : {isPending}</div>
                    <div>is error : {error?.error}</div>
                </>
            )}
        </div>
    );
}

Express server side code :

Express version : ^5.1.0
Better auth version : ^1.2.7

import { betterAuth } from "better-auth";
import { prismaAdapter } from "better-auth/adapters/prisma";
import { PrismaClient } from "../../generated/prisma/client";
import { openAPI } from "better-auth/plugins";
import { CONFIG } from "./config";

const prisma = new PrismaClient();
export const auth = betterAuth({
    database: prismaAdapter(prisma, {
        provider: "postgresql",
    }),
    emailAndPassword: { enabled: true },
    trustedOrigins: [CONFIG.FRONTEND_URL],
    session: {
        expiresIn: 30, // 30 sec for test
    },
});

Steps :

Important and Easier to reproduce the issue with this below repo :
Reproduced the exact same issue with readme file : https://github.com/Tholkappiar/better-auth-issue/

  1. use the above repo which contains the React and express code
  2. use the authClient.useSession(); like usual to reproduce this issue !

Current vs. Expected behavior

Current behaviour :

  • The component doesn't re-render or returning any new updated values

Expected behaviour :

  • The component needs to re-render after the session expired with the updated null values

What version of Better Auth are you using?

^1.2.7

Provide environment information

- OS : macOS
- Browser : Brave (Brave is up to date Version 1.78.102 Chromium: 136.0.7103.113 (Official Build) (arm64))

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

Client

Auth config (if applicable)

import { betterAuth } from "better-auth";
import { prismaAdapter } from "better-auth/adapters/prisma";
import { PrismaClient } from "../../generated/prisma/client";
import { openAPI } from "better-auth/plugins";
import { CONFIG } from "./config";

const prisma = new PrismaClient();
export const auth = betterAuth({
    database: prismaAdapter(prisma, {
        provider: "postgresql",
    }),
    emailAndPassword: { enabled: true },
    trustedOrigins: [CONFIG.FRONTEND_URL],
    plugins: [openAPI()],
    session: {
        expiresIn: 30, // 30 sec for test
    },
});

Additional context

Currently this issue is happening in local environment !

Originally created by @Tholkappiar on GitHub (May 19, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce **React client side code :** **React version** : ^19.1.0 ```js import { authClient } from "../lib/auth-client"; export function Temp() { const { data: session, isPending, error } = authClient.useSession(); return ( <div> {session ? ( <> {/* after the session expiry which in cookies, i am still getting these values - meaning not re-rendered with updated null state when the session expired */} <div>Logged in as: {session.user.name}</div> <div>is Pending : {isPending}</div> <div>is error : {error?.error}</div> </> ) : ( <> <div>Not logged in</div> <div>is Pending : {isPending}</div> <div>is error : {error?.error}</div> </> )} </div> ); } ``` **Express server side code :** **Express version** : ^5.1.0 **Better auth version** : ^1.2.7 ```js import { betterAuth } from "better-auth"; import { prismaAdapter } from "better-auth/adapters/prisma"; import { PrismaClient } from "../../generated/prisma/client"; import { openAPI } from "better-auth/plugins"; import { CONFIG } from "./config"; const prisma = new PrismaClient(); export const auth = betterAuth({ database: prismaAdapter(prisma, { provider: "postgresql", }), emailAndPassword: { enabled: true }, trustedOrigins: [CONFIG.FRONTEND_URL], session: { expiresIn: 30, // 30 sec for test }, }); ``` **Steps :** **Important and Easier to reproduce the issue with this below repo :** Reproduced the exact same issue with readme file : https://github.com/Tholkappiar/better-auth-issue/ 1. use the above repo which contains the React and express code 2. use the ```authClient.useSession();``` like usual to reproduce this issue ! ### Current vs. Expected behavior **Current behaviour :** - The component doesn't re-render or returning any new updated values **Expected behaviour :** - The component needs to re-render after the session expired with the updated null values ### What version of Better Auth are you using? ^1.2.7 ### Provide environment information ```bash - OS : macOS - Browser : Brave (Brave is up to date Version 1.78.102 Chromium: 136.0.7103.113 (Official Build) (arm64)) ``` ### Which area(s) are affected? (Select all that apply) Client ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth"; import { prismaAdapter } from "better-auth/adapters/prisma"; import { PrismaClient } from "../../generated/prisma/client"; import { openAPI } from "better-auth/plugins"; import { CONFIG } from "./config"; const prisma = new PrismaClient(); export const auth = betterAuth({ database: prismaAdapter(prisma, { provider: "postgresql", }), emailAndPassword: { enabled: true }, trustedOrigins: [CONFIG.FRONTEND_URL], plugins: [openAPI()], session: { expiresIn: 30, // 30 sec for test }, }); ``` ### Additional context Currently this issue is happening in local environment !
Author
Owner

@devallibus commented on GitHub (May 20, 2025):

Yes, somewhat similar I also found using the update user profile hooks does not refresh that data on user session. This can be worked around but of course, it'd be nice.

@devallibus commented on GitHub (May 20, 2025): Yes, somewhat similar I also found using the update user profile hooks does not refresh that data on user session. This can be worked around but of course, it'd be nice.
Author
Owner

@himself65 commented on GitHub (Aug 5, 2025):

Related: https://github.com/better-auth/better-auth/issues/1006

@himself65 commented on GitHub (Aug 5, 2025): Related: https://github.com/better-auth/better-auth/issues/1006
Author
Owner

@dosubot[bot] commented on GitHub (Nov 4, 2025):

Hi, @Tholkappiar. I'm Dosu, and I'm helping the better-auth team manage their backlog and am marking this issue as stale.

Issue Summary:

  • You reported that authClient.useSession() in a React app does not update or trigger a re-render when the server-side session expires, leading to stale session data in the UI.
  • You provided a minimal reproducible example and repository to demonstrate the problem.
  • Another user, debatz, mentioned a similar issue with user profile hooks and shared a possible workaround but prefers a proper fix.
  • A related issue (#1006) was linked by Himself65, which you acknowledged.

Next Steps:

  • Please let me know if this issue is still relevant with the latest version of better-auth by commenting here to keep the discussion open.
  • If I do not hear back within 7 days, this issue will be automatically closed.

Thank you for your understanding and contribution!

@dosubot[bot] commented on GitHub (Nov 4, 2025): Hi, @Tholkappiar. I'm [Dosu](https://dosu.dev), and I'm helping the better-auth team manage their backlog and am marking this issue as stale. **Issue Summary:** - You reported that `authClient.useSession()` in a React app does not update or trigger a re-render when the server-side session expires, leading to stale session data in the UI. - You provided a minimal reproducible example and repository to demonstrate the problem. - Another user, debatz, mentioned a similar issue with user profile hooks and shared a possible workaround but prefers a proper fix. - A related issue (#1006) was linked by Himself65, which you acknowledged. **Next Steps:** - Please let me know if this issue is still relevant with the latest version of better-auth by commenting here to keep the discussion open. - If I do not hear back within 7 days, this issue will be automatically closed. Thank you for your understanding and contribution!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1233