From e1d22c1f64edb9688c952e1aee06d781e9a9d508 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adel=20Rodr=C3=ADguez?= Date: Sun, 23 Aug 2026 11:13:56 -0400 Subject: [PATCH] fix: persist mobile sessions by matching the API auth cookie prefix Co-Authored-By: Claude Fable 5 --- apps/api/src/shared/auth.ts | 7 ++----- packages/auth/src/constants.ts | 3 +++ packages/auth/src/expo/client.ts | 6 +++++- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/apps/api/src/shared/auth.ts b/apps/api/src/shared/auth.ts index fa2e9ff3..c157f3e0 100644 --- a/apps/api/src/shared/auth.ts +++ b/apps/api/src/shared/auth.ts @@ -1,7 +1,7 @@ import { AUTH_ADVANCED_OPTIONS, + AUTH_API_COOKIE_PREFIX, AUTH_APP_NAME, - AUTH_COOKIE_PREFIX, AUTH_EMAIL_AND_PASSWORD_OPTIONS, AUTH_SESSION_OPTIONS, } from "@init/auth/constants" @@ -14,10 +14,7 @@ import { ENV } from "#shared/env.generated.ts" import { allowedOrigins, baseUrl } from "#shared/utils.ts" export const auth = createAuth({ - // The app's auth instance shares the localhost cookie jar during - // development, so this instance namespaces its cookies to keep the two - // sessions from clobbering each other. - advanced: { ...AUTH_ADVANCED_OPTIONS, cookiePrefix: `${AUTH_COOKIE_PREFIX}-api` }, + advanced: { ...AUTH_ADVANCED_OPTIONS, cookiePrefix: AUTH_API_COOKIE_PREFIX }, appName: AUTH_APP_NAME, basePath: "/auth", baseURL: baseUrl, diff --git a/packages/auth/src/constants.ts b/packages/auth/src/constants.ts index 73a2b4ac..0148d4be 100644 --- a/packages/auth/src/constants.ts +++ b/packages/auth/src/constants.ts @@ -1,6 +1,9 @@ import { seconds } from "humanspan" export const AUTH_COOKIE_PREFIX = "init" +// The API's auth instance namespaces its cookies so the two localhost jars +// stay separate during development. +export const AUTH_API_COOKIE_PREFIX = `${AUTH_COOKIE_PREFIX}-api` export const AUTH_APP_NAME = "init" export const AUTH_ADVANCED_OPTIONS = { diff --git a/packages/auth/src/expo/client.ts b/packages/auth/src/expo/client.ts index a6572d74..dbff2127 100644 --- a/packages/auth/src/expo/client.ts +++ b/packages/auth/src/expo/client.ts @@ -1,8 +1,12 @@ import { expoClient as baseExpoClient } from "@better-auth/expo/client" -import { AUTH_COOKIE_PREFIX } from "#constants.ts" +import { AUTH_API_COOKIE_PREFIX, AUTH_COOKIE_PREFIX } from "#constants.ts" export function expoClient(opts: Parameters[0]) { return baseExpoClient({ + // Must match the API's cookie prefix: the Expo plugin only persists + // cookies whose names start with this value, and it defaults to + // "better-auth" when unset. + cookiePrefix: AUTH_API_COOKIE_PREFIX, scheme: AUTH_COOKIE_PREFIX, storagePrefix: AUTH_COOKIE_PREFIX, ...opts,