handle errors

This commit is contained in:
Daniel Bulant 2026-04-27 19:49:11 +02:00
parent dfef859ede
commit a41ad5fb6e
No known key found for this signature in database

View file

@ -1,46 +1,49 @@
import { createServerFn } from "@tanstack/react-start"; import { createServerFn } from "@tanstack/react-start";
export interface AuthSession { export interface AuthSession {
user: { user: {
id: string; id: string;
createdAt: Date; createdAt: Date;
updatedAt: Date; updatedAt: Date;
email: string; email: string;
emailVerified: boolean; emailVerified: boolean;
name: string; name: string;
image?: string | null | undefined; image?: string | null | undefined;
}; };
session: { session: {
id: string; id: string;
createdAt: Date; createdAt: Date;
updatedAt: Date; updatedAt: Date;
userId: string; userId: string;
expiresAt: Date; expiresAt: Date;
token: string; token: string;
ipAddress?: string | null | undefined; ipAddress?: string | null | undefined;
userAgent?: string | null | undefined; userAgent?: string | null | undefined;
}; };
} }
export const getSession = createServerFn({ method: "GET" }).handler( export const getSession = createServerFn({ method: "GET" }).handler(
async (): Promise<AuthSession | null> => { async (): Promise<AuthSession | null> => {
const { getRequestHeaders } = await import("@tanstack/react-start/server"); const { getRequestHeaders } = await import("@tanstack/react-start/server");
const headers = getRequestHeaders(); const headers = getRequestHeaders();
const resolvedBaseUrl = const resolvedBaseUrl =
process.env.VITE_BETTER_AUTH_URL ?? process.env.VITE_BETTER_AUTH_URL ??
import.meta.env.VITE_BETTER_AUTH_URL ?? import.meta.env.VITE_BETTER_AUTH_URL ??
headers.get("origin") ?? headers.get("origin") ??
"http://127.0.0.1:3000"; "http://localhost:3000";
const response = await fetch( const response = await fetch(
new URL("/api/auth/get-session", resolvedBaseUrl), new URL("/api/auth/get-session", resolvedBaseUrl),
{ headers }, { headers },
); ).catch((e) => {
console.error(e);
return { ok: false } as const;
});
if (!response.ok) { if (!response.ok) {
return null; return null;
} }
const data = (await response.json()) as AuthSession | null; const data = (await response.json()) as AuthSession | null;
return data ?? null; return data ?? null;
}, },
); );