TanStackT
TanStack8mo ago
6 replies
worthy-azure

tRPC + Custom Auth

Hi again! I was hoping someone could help with a cookie problem I have. I'm pretty new to the world of SSR (I think SSR is where my issue lies, please correct if I'm wrong). I was looking through https://github.com/makyinmars/tan-stack-start-full-stack/?tab=readme-ov-file#tan-stack-start-full-stack for custom auth, which is pretty similar to what I've used with NextJS previously, it follows the Lucia guide for rolling your own auth service.

Anyways, I have a function which creates a token and a session but when I try to set a cookie, I get the following error: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client. I'm using setCookie from vinxi/http, also tried with setCookie from @tanstack/react-start/server. Here's the function that's throwing the error:

export async function setSessionTokenCookie(
  token: string,
  expiresAt: Date
): Promise<void> {
  setCookie(SESSION_COOKIE_NAME, token, {
    httpOnly: true,
    sameSite: 'lax',
    secure: env.NODE_ENV === 'production',
    expires: expiresAt,
    path: '/',
  });
}


This is being call inside my trpc mutation. Let me know if I need to share anything else and thanks in advance.
GitHub
Contribute to makyinmars/tan-stack-start-full-stack development by creating an account on GitHub.
GitHub - makyinmars/tan-stack-start-full-stack
Was this page helpful?