marca getSessionUserId/fetchSessionProfile como server-only

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.
This commit is contained in:
joao.herculano
2026-08-06 14:04:30 -03:00
parent 8c26c93732
commit f2e7c1f741
+5 -5
View File
@@ -1,4 +1,4 @@
import { createServerFn } from "@tanstack/react-start"; import { createServerFn, createServerOnlyFn } from "@tanstack/react-start";
import { z } from "zod"; import { z } from "zod";
import { toLocalDateTimeString } from "@/lib/datetime"; import { toLocalDateTimeString } from "@/lib/datetime";
@@ -41,12 +41,12 @@ function toPerfil(row: PerfilRow): Perfil {
}; };
} }
async function getSessionUserId(): Promise<string | null> { const getSessionUserId = createServerOnlyFn(async (): Promise<string | null> => {
const { getSessionUserId: getServerSessionUserId } = await import("@/lib/auth.server"); const { getSessionUserId: getServerSessionUserId } = await import("@/lib/auth.server");
return await getServerSessionUserId(); return await getServerSessionUserId();
} });
async function fetchSessionProfile(userId: string): Promise<SessaoAtual | null> { const fetchSessionProfile = createServerOnlyFn(async (userId: string): Promise<SessaoAtual | null> => {
const [{ getPool, sql }] = await Promise.all([import("@/lib/db.server")]); const [{ getPool, sql }] = await Promise.all([import("@/lib/db.server")]);
const pool = await getPool(); const pool = await getPool();
@@ -82,7 +82,7 @@ async function fetchSessionProfile(userId: string): Promise<SessaoAtual | null>
admin: Boolean(row.admin), admin: Boolean(row.admin),
perfil, perfil,
}; };
} });
async function requireSession(): Promise<SessaoAtual> { async function requireSession(): Promise<SessaoAtual> {
const userId = await getSessionUserId(); const userId = await getSessionUserId();