Enviar cookies entre orígenes diferentes
Esta página fue traducida por PageTurner AI (beta). No está respaldada oficialmente por el proyecto. ¿Encontraste un error? Reportar problema →
Si tu API reside en un origen diferente al de tu front-end y deseas enviar cookies a ella, necesitarás habilitar CORS en tu servidor y enviar cookies con tus solicitudes proporcionando la opción {credentials: "include"} a fetch.
Los argumentos proporcionados a la función fetch que utiliza tRPC pueden modificarse de la siguiente manera.
app.tstsimport { createTRPCProxyClient, httpBatchLink } from '@trpc/client';const client = createTRPCProxyClient<AppRouter>({links: [httpBatchLink({url: 'YOUR_SERVER_URL',fetch(url, options) {return fetch(url, {...options,credentials: 'include',});},}),],});
app.tstsimport { createTRPCProxyClient, httpBatchLink } from '@trpc/client';const client = createTRPCProxyClient<AppRouter>({links: [httpBatchLink({url: 'YOUR_SERVER_URL',fetch(url, options) {return fetch(url, {...options,credentials: 'include',});},}),],});
También necesitas habilitar CORS en tu servidor modificando tu adaptador o el servidor HTTP que está delante de tu API. La mejor manera de hacer esto varía entre adaptadores y según tu infraestructura de hosting, y los adaptadores individuales generalmente documentan este proceso cuando es aplicable.