[GH-ISSUE #2048] Session not populated immediately after sign in #9025

Closed
opened 2026-04-13 04:17:46 -05:00 by GiteaMirror · 5 comments
Owner

Originally created by @jirizavadil on GitHub (Mar 29, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/2048

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

auth-client.js

import {createAuthClient} from "better-auth/vue"
import {emailOTPClient} from "better-auth/client/plugins"

export const {signIn, useSession} = createAuthClient({
  baseURL: `https://example.com/auth`,
  },
  plugins: [emailOTPClient()],
})

AuthForm.vue

...
const {useSession, emailOtp, signIn} = authClient
const session = useSession()

const {data, error, isPending} = await signIn.emailOtp({
    email: 'test@example.com',
    otp: '123456',
})

console.log(session.value?.data) // <- ❌ null

// workaround: wait until session is populated
await new Promise((resolve) =>
   watch(session, (newSession) => {
      if (newSession?.data) resolve()
   })
)

console.log(session.value?.data) // <- ✅ populated
...

Current vs. Expected behavior

current

Session is empty immediately upon successful sign in Promise is resolved

expected

Session is populated immediately upon successful sign in Promise is resolved

What version of Better Auth are you using?

1.2.5

Provide environment information

- OS: MacOS 15.3.2 (24D81)
- Browser: Chrome 134.0.6998.166 (Official Build) (arm64)

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

Client

Auth config (if applicable)


Additional context

No response

Originally created by @jirizavadil on GitHub (Mar 29, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/2048 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce auth-client.js ```javascript import {createAuthClient} from "better-auth/vue" import {emailOTPClient} from "better-auth/client/plugins" export const {signIn, useSession} = createAuthClient({ baseURL: `https://example.com/auth`, }, plugins: [emailOTPClient()], }) ``` AuthForm.vue ```javascript ... const {useSession, emailOtp, signIn} = authClient const session = useSession() const {data, error, isPending} = await signIn.emailOtp({ email: 'test@example.com', otp: '123456', }) console.log(session.value?.data) // <- ❌ null // workaround: wait until session is populated await new Promise((resolve) => watch(session, (newSession) => { if (newSession?.data) resolve() }) ) console.log(session.value?.data) // <- ✅ populated ... ``` ### Current vs. Expected behavior ### current Session is empty immediately upon successful sign in Promise is resolved ### expected Session is populated immediately upon successful sign in Promise is resolved ### What version of Better Auth are you using? 1.2.5 ### Provide environment information ```bash - OS: MacOS 15.3.2 (24D81) - Browser: Chrome 134.0.6998.166 (Official Build) (arm64) ``` ### Which area(s) are affected? (Select all that apply) Client ### Auth config (if applicable) ```typescript ``` ### Additional context _No response_
GiteaMirror added the lockedbug labels 2026-04-13 04:17:46 -05:00
Author
Owner

@asimaranov commented on GitHub (Apr 1, 2025):

Same one

<!-- gh-comment-id:2768704576 --> @asimaranov commented on GitHub (Apr 1, 2025): Same one
Author
Owner

@ping-maxwell commented on GitHub (Jun 19, 2025):

useSession updates the session by watching the authClient and seeing if it hits any of the relevant endpoints which could update a session. If one of those endpoints are called, then useSession will trigger it's own fetch to get the possibly new session data, and it's at this point it makes all useSession hooks retriggered.

This is likely why it's delayed for you guys.

<!-- gh-comment-id:2987522069 --> @ping-maxwell commented on GitHub (Jun 19, 2025): useSession updates the session by watching the authClient and seeing if it hits any of the relevant endpoints which could update a session. If one of those endpoints are called, then useSession will trigger it's own fetch to get the possibly new session data, and it's at this point it makes all useSession hooks retriggered. This is likely why it's delayed for you guys.
Author
Owner

@scottahaynie commented on GitHub (Aug 9, 2025):

Bashed my head against the wall on this one. Seems like a bug to me, but from the response, it sounds like this is working as designed?

<!-- gh-comment-id:3170522424 --> @scottahaynie commented on GitHub (Aug 9, 2025): Bashed my head against the wall on this one. Seems like a bug to me, but from the response, it sounds like this is working as designed?
Author
Owner

@Cvikli commented on GitHub (Nov 12, 2025):

For me I still see this issue. 1.3.34 version. Not sure if it got resolved already.

<!-- gh-comment-id:3521130395 --> @Cvikli commented on GitHub (Nov 12, 2025): For me I still see this issue. 1.3.34 version. Not sure if it got resolved already.
Author
Owner

@ping-maxwell commented on GitHub (Nov 12, 2025):

Yes it's by design. Unless you're using something like stateless auth where the session is immediately avaible on the client, we must fetch that data from the server. While it's fetching the useSession hook sets isPending to true, indicating that it's fetching data. When the fetching is complete, either session is defined or error is defined. If nether is defined it means there is no active session.

<!-- gh-comment-id:3524162328 --> @ping-maxwell commented on GitHub (Nov 12, 2025): Yes it's by design. Unless you're using something like stateless auth where the session is immediately avaible on the client, we must fetch that data from the server. While it's fetching the `useSession` hook sets `isPending` to true, indicating that it's fetching data. When the fetching is complete, either `session` is defined or `error` is defined. If nether is defined it means there is no active session.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#9025