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:
+5
-5
@@ -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<string | null> {
|
||||
const getSessionUserId = createServerOnlyFn(async (): Promise<string | null> => {
|
||||
const { getSessionUserId: getServerSessionUserId } = await import("@/lib/auth.server");
|
||||
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 pool = await getPool();
|
||||
|
||||
@@ -82,7 +82,7 @@ async function fetchSessionProfile(userId: string): Promise<SessaoAtual | null>
|
||||
admin: Boolean(row.admin),
|
||||
perfil,
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
async function requireSession(): Promise<SessaoAtual> {
|
||||
const userId = await getSessionUserId();
|
||||
|
||||
Reference in New Issue
Block a user