fix(expo): store normalized cookie name in storage (#5432)

This commit is contained in:
Maxwell
2025-10-21 08:14:02 +10:00
committed by GitHub
parent 08eb2bed70
commit 2cf7d0229d
2 changed files with 44 additions and 2 deletions

View File

@@ -238,12 +238,39 @@ export function hasBetterAuthCookies(
return false;
}
/**
* Expo secure store does not support colons in the keys.
* This function replaces colons with underscores.
*
* @see https://github.com/better-auth/better-auth/issues/5426
*
* @param name cookie name to be saved in the storage
* @returns normalized cookie name
*/
export function normalizeCookieName(name: string) {
return name.replace(/:/g, "_");
}
export function storageAdapter(storage: {
getItem: (name: string) => string | null;
setItem: (name: string, value: string) => void;
}) {
return {
getItem: (name: string) => {
return storage.getItem(normalizeCookieName(name));
},
setItem: (name: string, value: string) => {
return storage.setItem(normalizeCookieName(name), value);
},
};
}
export const expoClient = (opts: ExpoClientOptions) => {
let store: Store | null = null;
const storagePrefix = opts?.storagePrefix || "better-auth";
const cookieName = `${storagePrefix}_cookie`;
const localCacheName = `${storagePrefix}_session_data`;
const storage = opts?.storage;
const storage = storageAdapter(opts?.storage);
const isWeb = Platform.OS === "web";
const cookiePrefix = opts?.cookiePrefix || "better-auth";

View File

@@ -2,7 +2,7 @@ import { createAuthClient } from "better-auth/react";
import Database from "better-sqlite3";
import { beforeAll, afterAll, describe, expect, it, vi } from "vitest";
import { expo } from ".";
import { expoClient } from "./client";
import { expoClient, storageAdapter } from "./client";
import { betterAuth } from "better-auth";
import { getMigrations } from "better-auth/db";
import { oAuthProxy } from "better-auth/plugins";
@@ -325,4 +325,19 @@ describe("expo with cookieCache", async () => {
expect(hasBetterAuthCookies(customCookieHeader, "better-auth")).toBe(false);
});
it("should normalize colons in secure storage name via storage adapter", async () => {
const map = new Map<string, string>();
const storage = storageAdapter({
getItem(name) {
return map.get(name) || null;
},
setItem(name, value) {
map.set(name, value);
},
});
storage.setItem("better-auth:session_token", "123");
expect(map.has("better-auth_session_token")).toBe(true);
expect(map.has("better-auth:session_token")).toBe(false);
});
});