[GH-ISSUE #1019] Type mismatch with session data from useSession #8553

Closed
opened 2026-04-13 03:40:11 -05:00 by GiteaMirror · 0 comments
Owner

Originally created by @andrewcoelho on GitHub (Dec 26, 2024).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/1019

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

In an Expo app using the following auth client config:

import * as SecureStore from 'expo-secure-store';
import { expoClient } from '@better-auth/expo/client';
import { createAuthClient } from 'better-auth/react';

export const authClient = createAuthClient({
  baseURL: 'localhost:3000',
  plugins: [
    expoClient({
      scheme: 'myapp',
      storagePrefix: 'myapp',
      storage: SecureStore,
    }),
  ],

Checking for session data after isPending is set to false returns something like:

{"__Secure-better-auth.session_token": {"expires": "<timestamp>", "value": "<randomvalue>"}}

I am checking for data === null to redirect to the sign in page, and as you can see I get no typescript error for accessing data.user.id after that check, but get a runtime type error because the initial data response doesn't actually have a populated user object.

function Page() {
  const { data, isPending } = authClient.useSession();

  if (isPending) {
    return <Text>Loading</Text>;
  }

  if (data === null) {
    return <Redirect href="/sign-in" />;
  }

  console.log(data.user.id) // TypeError: Cannot read property 'id' of undefined
}

I think this probably has something to do with how the session is being cached in SecureStore and the __Secure-better-auth.session_token is returned first before the full user and session objects.

I tried to figure out what's going on, and what I see is that the storage is doing a setItem with the full user / session objects, but appears to only do a getItem for myapp_cookie which is the {"__Secure-better-auth.session_token": {"expires": "<timestamp>", "value": "<randomvalue>"}} I mentioned above. I would assume this token is then used to fetch the full user / session object from my server. It seems like useSession should also return the cached user / session objects before getting the response from the server.

Current vs. Expected behavior

Maybe an expected behavior would be one of the following:

  1. Make the SecureStorage cache return the actual user / session objects
  2. Don't set isPending to false until the full user / session objects are returned

What version of Better Auth are you using?

1.1.3

Provide environment information

- IOS via Expo Go

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

Client

Originally created by @andrewcoelho on GitHub (Dec 26, 2024). Original GitHub issue: https://github.com/better-auth/better-auth/issues/1019 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce In an Expo app using the following auth client config: ``` import * as SecureStore from 'expo-secure-store'; import { expoClient } from '@better-auth/expo/client'; import { createAuthClient } from 'better-auth/react'; export const authClient = createAuthClient({ baseURL: 'localhost:3000', plugins: [ expoClient({ scheme: 'myapp', storagePrefix: 'myapp', storage: SecureStore, }), ], ``` Checking for session data after `isPending` is set to false returns something like: ``` {"__Secure-better-auth.session_token": {"expires": "<timestamp>", "value": "<randomvalue>"}} ``` I am checking for `data === null` to redirect to the sign in page, and as you can see I get no typescript error for accessing `data.user.id` after that check, but get a runtime type error because the initial data response doesn't actually have a populated user object. ``` function Page() { const { data, isPending } = authClient.useSession(); if (isPending) { return <Text>Loading</Text>; } if (data === null) { return <Redirect href="/sign-in" />; } console.log(data.user.id) // TypeError: Cannot read property 'id' of undefined } ``` I think this probably has something to do with how the session is being cached in SecureStore and the `__Secure-better-auth.session_token` is returned first before the full user and session objects. I tried to figure out what's going on, and what I see is that the storage is doing a `setItem` with the full user / session objects, but appears to only do a `getItem` for `myapp_cookie` which is the `{"__Secure-better-auth.session_token": {"expires": "<timestamp>", "value": "<randomvalue>"}}` I mentioned above. I would assume this token is then used to fetch the full user / session object from my server. It seems like `useSession` should also return the cached user / session objects before getting the response from the server. ### Current vs. Expected behavior Maybe an expected behavior would be one of the following: 1. Make the SecureStorage cache return the actual user / session objects 2. Don't set `isPending` to false until the full user / session objects are returned ### What version of Better Auth are you using? 1.1.3 ### Provide environment information ```bash - IOS via Expo Go ``` ### Which area(s) are affected? (Select all that apply) Client
GiteaMirror added the lockedbug labels 2026-04-13 03:40:11 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#8553