Compare commits
2 Commits
51f0028883
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
997dd90c92 | ||
|
|
597e0d9390 |
@@ -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");
|
||||||
|
|||||||
@@ -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: () => SecureStore.getItem(TOKEN_KEY) ?? "",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
plugins: [
|
plugins: [
|
||||||
expoClient({
|
expoClient({
|
||||||
scheme: "haushaltsApp",
|
scheme: "haushaltsApp",
|
||||||
|
|||||||
4
packages/env/src/native.ts
vendored
4
packages/env/src/native.ts
vendored
@@ -6,6 +6,8 @@ export const env = createEnv({
|
|||||||
client: {
|
client: {
|
||||||
EXPO_PUBLIC_SERVER_URL: z.url(),
|
EXPO_PUBLIC_SERVER_URL: z.url(),
|
||||||
},
|
},
|
||||||
runtimeEnv: process.env,
|
runtimeEnv: {
|
||||||
|
EXPO_PUBLIC_SERVER_URL: process.env.EXPO_PUBLIC_SERVER_URL ?? "https://api.app-forge.dev",
|
||||||
|
},
|
||||||
emptyStringAsUndefined: true,
|
emptyStringAsUndefined: true,
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user