Skip to content

Issues with useSession: Invariant failed: expected content-type header to be set #6102

@mhsattarian

Description

@mhsattarian

Which project does this relate to?

Start

Describe the bug

When implementing a manual authentication procedure from the provided guide in the documentation, several issues arise. Notably, the useSession method is not functioning correctly. Below is the sample code I utilize and the corresponding error:

How session is configured:

// utils/tanstack-session.ts
"use server";

import { useSession } from "@tanstack/react-start/server";

export function useAppSession() {
  return useSession({
    name: "session",
    password:
      "LKMDSM<DCLMSCDKNScjdnskjdfn3434rmn3kj4n3kjrnk3j4nrkj34nrkj3nrkj3n4rkjn", // At least 32 characters
    cookie: {
      secure: false,
      sameSite: "lax",
      httpOnly: true,
      maxAge: 7 * 24 * 60 * 60, // 7 days
    },
  });
}

How is used:

// actions.ts
import { useAppSession } from "./utils/tanstack-session";

export async function getSessionAction() {
  const session = await useAppSession();

  if (!session.data) {
    return null;
  }

  return session.data;
}

here is the error I get:

Error: Invariant failed: expected content-type header to be set

There are some other issues with the docs regarding the manual authentication guide:
  1. In the "2. Session Management" section, the provided code sample's wont work unless after adding a "use server" directive.

  2. In the "3. Authentication Context" section, the usage of useServerFn is incorrect:

Image

Your Example Website or App

https://codesandbox.io/p/devbox/cranky-mayer-pl9r9f

Steps to Reproduce the Bug or Issue

  1. Go to "https://codesandbox.io/p/devbox/cranky-mayer-pl9r9f"
  2. In the CodeSandBox there's another error that I coud'nt figure out: "globalThis.process.getBuiltinModule is not a function"
  3. Download the project and run it locally
  4. Open the project, on the main page there's a butto which by clicking on it the action that uses the cookies would run hence causing the error

Expected behavior

This should work as expected and return the cookie, as it did previously.

Screenshots or Videos

Image

Platform

  • Router / Start Version: Router: 1.141.2 | Start: 1.141.3
  • OS: macOS
  • Browser: Chrome
  • Browser Version: 143.0.7499.109
  • Bundler: vite
  • Bundler Version: 7.1.7

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions