fix: use expoClient getCookie() for API requests per Better Auth docs

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
René Schober
2026-03-23 08:20:05 +01:00
parent 597e0d9390
commit 997dd90c92
2 changed files with 4 additions and 22 deletions

View File

@@ -1,32 +1,29 @@
import * as SecureStore from "expo-secure-store";
import { router } from "expo-router"; import { router } from "expo-router";
import { env } from "@haushaltsApp/env/native"; import { env } from "@haushaltsApp/env/native";
import { useAuthStore } from "../stores/auth.store"; import { useAuthStore } from "../stores/auth.store";
import { authClient } from "./auth-client";
const BASE_URL = env.EXPO_PUBLIC_SERVER_URL; const BASE_URL = env.EXPO_PUBLIC_SERVER_URL;
// expoClient plugin stores session token under "<storagePrefix>.session_token"
const TOKEN_KEY = "haushaltsapp.session_token";
export async function apiRequest<T>( export async function apiRequest<T>(
path: string, path: string,
options: RequestInit = {}, options: RequestInit = {},
): Promise<T> { ): Promise<T> {
const householdId = useAuthStore.getState().activeHouseholdId; const householdId = useAuthStore.getState().activeHouseholdId;
const token = await SecureStore.getItemAsync(TOKEN_KEY); const cookies = authClient.getCookie();
const response = await fetch(`${BASE_URL}${path}`, { const response = await fetch(`${BASE_URL}${path}`, {
...options, ...options,
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
...(token ? { Authorization: `Bearer ${token}` } : {}), ...(cookies ? { Cookie: cookies } : {}),
...(householdId ? { "x-household-id": householdId } : {}), ...(householdId ? { "x-household-id": householdId } : {}),
...options.headers, ...options.headers,
}, },
credentials: "include", credentials: "omit",
}); });
if (response.status === 401) { if (response.status === 401) {
await SecureStore.deleteItemAsync(TOKEN_KEY);
useAuthStore.getState().clearSession(); useAuthStore.getState().clearSession();
router.replace("/(auth)/login"); router.replace("/(auth)/login");
throw new Error("Unauthorized"); throw new Error("Unauthorized");

View File

@@ -4,23 +4,8 @@ import { expoClient } from "@better-auth/expo/client";
import * as SecureStore from "expo-secure-store"; import * as SecureStore from "expo-secure-store";
import { env } from "@haushaltsApp/env/native"; import { env } from "@haushaltsApp/env/native";
// expoClient plugin stores session token under "<storagePrefix>.session_token"
const TOKEN_KEY = "haushaltsapp.session_token";
export const authClient = createAuthClient({ export const authClient = createAuthClient({
baseURL: env.EXPO_PUBLIC_SERVER_URL, baseURL: env.EXPO_PUBLIC_SERVER_URL,
fetchOptions: {
onSuccess: (ctx) => {
const token = ctx.response.headers.get("set-auth-token");
if (token) {
SecureStore.setItemAsync(TOKEN_KEY, token);
}
},
auth: {
type: "Bearer",
token: async () => (await SecureStore.getItemAsync(TOKEN_KEY)) ?? "",
},
},
plugins: [ plugins: [
expoClient({ expoClient({
scheme: "haushaltsApp", scheme: "haushaltsApp",