auth-proxy.ts990 BView on GitHub
import { perfLogger } from '@/lib/performance-logger';
import { getRuntimeBackendUrl } from '@/lib/runtime-urls';
import { createAuthClient } from 'better-auth/client';

const authClient = createAuthClient({
  baseURL: getRuntimeBackendUrl(),
  fetchOptions: {
    credentials: 'include',
  },
  plugins: [],
});

export const authProxy = {
  api: {
    getSession: async ({ headers }: { headers: Headers }) => {
      perfLogger.start('authProxy:getSession');

      perfLogger.start('authProxy:networkRequest');
      const session = await authClient.getSession({
        fetchOptions: { headers, credentials: 'include' },
      });
      perfLogger.end('authProxy:networkRequest');

      if (session.error) {
        console.error(`Failed to get session: ${session.error}`, session);
        perfLogger.end('authProxy:getSession', { error: true });
        return null;
      }

      perfLogger.end('authProxy:getSession', { success: true });
      return session.data;
    },
  },
};