From f2e7c1f7414a9fc42c4e169dc6ca880e9e4720b5 Mon Sep 17 00:00:00 2001 From: "joao.herculano" Date: Thu, 6 Aug 2026 14:04:30 -0300 Subject: [PATCH] marca getSessionUserId/fetchSessionProfile como server-only MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Essas funções tocam módulos *.server.* (sessão em cookie, conexão SQL Server) mas eram funções soltas, então o TanStack Start acusava "import denied in client environment" ao analisar o grafo de import que chega em auth.ts a partir de rotas renderizadas no cliente. Na prática já eram seguras (só chamadas de dentro de handlers createServerFn, que rodam só no servidor), mas createServerOnlyFn torna isso explícito e reconhecido pela análise estática do framework, além de lançar erro em vez de rodar se algum dia forem chamadas do lado do cliente por engano. --- src/lib/auth.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/lib/auth.ts b/src/lib/auth.ts index 68c749f..8db19c0 100644 --- a/src/lib/auth.ts +++ b/src/lib/auth.ts @@ -1,4 +1,4 @@ -import { createServerFn } from "@tanstack/react-start"; +import { createServerFn, createServerOnlyFn } from "@tanstack/react-start"; import { z } from "zod"; import { toLocalDateTimeString } from "@/lib/datetime"; @@ -41,12 +41,12 @@ function toPerfil(row: PerfilRow): Perfil { }; } -async function getSessionUserId(): Promise { +const getSessionUserId = createServerOnlyFn(async (): Promise => { const { getSessionUserId: getServerSessionUserId } = await import("@/lib/auth.server"); return await getServerSessionUserId(); -} +}); -async function fetchSessionProfile(userId: string): Promise { +const fetchSessionProfile = createServerOnlyFn(async (userId: string): Promise => { const [{ getPool, sql }] = await Promise.all([import("@/lib/db.server")]); const pool = await getPool(); @@ -82,7 +82,7 @@ async function fetchSessionProfile(userId: string): Promise admin: Boolean(row.admin), perfil, }; -} +}); async function requireSession(): Promise { const userId = await getSessionUserId();