Problem with react native expo reloading the authentication while using better auth client #1900

Closed
opened 2026-03-13 09:11:37 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @h4kbas on GitHub (Sep 10, 2025).

When I close expo, the auth is ended when I reopen the app I have to login again each time, I used emulator, physical device or expo go app, nothing changed, I even built and sent to testflight then tested on real device to be sure. I used alternatives to SecureStorage, asked chatgpt, tried various ways, used all methods on reddit, it just forgets. I think it's a bug to read or write authentication in betterAuth.

I use better auth client with react native expo app and server is nextjs here are my configs

const APP_URL_SCHEME = "myapp";
const WEB_URL = process.env.NEXT_PUBLIC_APP_URL || "http://127.0.0.1:3333";

export const auth = betterAuth({
  baseURL: WEB_URL,
  trustedOrigins: [
    `${APP_URL_SCHEME}://`, // Expo app URL scheme
    "http://127.0.0.1:3333", // Development server
    "https://my.app", // Production URL
  ],
  database: drizzleAdapter(db, {
    provider: "pg",
    schema: authSchema,
  }),
  emailAndPassword: {
    enabled: true,
    sendResetPassword: async ({ user, url }) => {
      ...
    },
  },
  emailVerification: {
    enabled: true,
    sendOnSignUp: true,
    sendVerificationEmail: async ({ user, url }) => {
      ...
    },
  },
  autoSignInAfterVerification: true,
  plugins: [jwt(), openAPI(), bearer(), expo()],
});

Auth Client

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

export const authClient = createAuthClient({
  baseURL: process.env.EXPO_PUBLIC_BETTER_AUTH_BASE_URL,
  plugins: [
    expoClient({
      scheme: "myapp",
      storagePrefix: "myapp",
      storage: SecureStore,
    }),
  ],
});
Originally created by @h4kbas on GitHub (Sep 10, 2025). When I close expo, the auth is ended when I reopen the app I have to login again each time, I used emulator, physical device or expo go app, nothing changed, I even built and sent to testflight then tested on real device to be sure. I used alternatives to SecureStorage, asked chatgpt, tried various ways, used all methods on reddit, it just forgets. I think it's a bug to read or write authentication in betterAuth. I use better auth client with react native expo app and server is nextjs here are my configs ``` const APP_URL_SCHEME = "myapp"; const WEB_URL = process.env.NEXT_PUBLIC_APP_URL || "http://127.0.0.1:3333"; export const auth = betterAuth({ baseURL: WEB_URL, trustedOrigins: [ `${APP_URL_SCHEME}://`, // Expo app URL scheme "http://127.0.0.1:3333", // Development server "https://my.app", // Production URL ], database: drizzleAdapter(db, { provider: "pg", schema: authSchema, }), emailAndPassword: { enabled: true, sendResetPassword: async ({ user, url }) => { ... }, }, emailVerification: { enabled: true, sendOnSignUp: true, sendVerificationEmail: async ({ user, url }) => { ... }, }, autoSignInAfterVerification: true, plugins: [jwt(), openAPI(), bearer(), expo()], }); ``` ### Auth Client ``` import { createAuthClient } from "better-auth/react"; import { expoClient } from "@better-auth/expo/client"; import * as SecureStore from "expo-secure-store"; export const authClient = createAuthClient({ baseURL: process.env.EXPO_PUBLIC_BETTER_AUTH_BASE_URL, plugins: [ expoClient({ scheme: "myapp", storagePrefix: "myapp", storage: SecureStore, }), ], }); ```
GiteaMirror added the expo label 2026-03-13 09:11:37 -05:00
Author
Owner

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

Hi @h4kbas! I'm Dosu and I’m helping the better-auth team.

This is a common issue with Expo and Better Auth. The most likely cause is that session data is not being restored from SecureStore after app reloads. The recommended workaround is to explicitly call authClient.getSession() after any redirect or sign-in flow—this forces Better Auth to refresh and persist the session state, so you don’t have to log in again each time you reopen the app. This is especially important after deep links or social sign-ins, as the session may not be immediately available in SecureStore until you do this call (source, source).

Also, double-check that your app’s scheme is set in app.json and matches the scheme in trustedOrigins on the server (source). If you’re using Expo web, SecureStore isn’t available—use a platform check to select localStorage for web and SecureStore for native (source).

If you’re still seeing issues, make sure the deep link after sign-in contains the cookie query param, and that SecureStore is actually storing the cookie. If not, it’s usually a backend config or callback URL problem (source).

Let me know if you want a code snippet for the session refresh workaround or platform-specific storage!

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 10, 2025): <!-- Greeting --> Hi @h4kbas! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> This is a common issue with Expo and Better Auth. The most likely cause is that session data is not being restored from SecureStore after app reloads. The recommended workaround is to explicitly call `authClient.getSession()` after any redirect or sign-in flow—this forces Better Auth to refresh and persist the session state, so you don’t have to log in again each time you reopen the app. This is especially important after deep links or social sign-ins, as the session may not be immediately available in SecureStore until you do this call ([source](https://github.com/better-auth/better-auth/issues/1443), [source](https://github.com/better-auth/better-auth/discussions/3982)). Also, double-check that your app’s scheme is set in `app.json` and matches the scheme in `trustedOrigins` on the server ([source](https://github.com/better-auth/better-auth/discussions/3982)). If you’re using Expo web, SecureStore isn’t available—use a platform check to select localStorage for web and SecureStore for native ([source](https://github.com/better-auth/better-auth/issues/4392)). If you’re still seeing issues, make sure the deep link after sign-in contains the `cookie` query param, and that SecureStore is actually storing the cookie. If not, it’s usually a backend config or callback URL problem ([source](https://github.com/better-auth/better-auth/issues/3711)). Let me know if you want a code snippet for the session refresh workaround or platform-specific storage! <!-- 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/da9fd278-8745-4215-bffc-35b654819c8e?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/da9fd278-8745-4215-bffc-35b654819c8e?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/da9fd278-8745-4215-bffc-35b654819c8e?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/da9fd278-8745-4215-bffc-35b654819c8e?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/da9fd278-8745-4215-bffc-35b654819c8e?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/da9fd278-8745-4215-bffc-35b654819c8e?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/da9fd278-8745-4215-bffc-35b654819c8e?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/4570)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1900