npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

tanstack-auth-session

v0.1.3

Published

Typed, request-scoped auth session for SSR React apps

Readme

tanstack-auth-session

Typed, request-scoped auth session for modern React SSR applications

tanstack-auth-session provides a real session layer for React applications that use SSR, loaders, middleware, or server components.

It is framework-agnostic and works consistently across TanStack Router, Remix, Next.js, Express/Fastify, and plain Fetch-based SSR.


Why this library exists

In modern React apps:

  • Cookies are not sessions
  • JWT is not authorization
  • SSR has no shared user state
  • Global variables break in Node.js
  • Auth logic is duplicated everywhere

Most projects re-implement:

  • cookie parsing
  • token verification
  • user validation
  • role & permission checks
  • SSR → client hydration

This leads to bugs, security issues, and untyped user objects.


What tanstack-auth-session gives you

  • Secure cookie-based authentication
  • Request-scoped session (no globals, no memory leaks)
  • Runtime validation with Zod
  • Fully typed session.user
  • Permissions with session.can()
  • Request-level cache
  • SSR → Client hydration
  • Framework-agnostic core

Installation

npm install tanstack-auth-session

Peer dependencies

You must provide these in your application:

  • react >= 18
  • @tanstack/react-router >= 1 (only if you use the adapter)

Core Concept

A Session:

  • is created per request
  • lives only during that request
  • is cached per request
  • is safe in concurrent SSR
  • works on server and client

Basic Setup (Framework-agnostic)

import { createAuthSession } from 'tanstack-auth-session'
import { z } from 'zod'

export const auth = createAuthSession({
  cookieName: 'auth',

  schema: z.object({
    id: z.string(),
    role: z.enum(['user', 'admin']),
  }),

  verify: async (token) => {
    if (token === 'admin-token') {
      return { id: '1', role: 'admin' }
    }
    return null
  },

  permissions: {
    admin: ['users.read', 'users.write'],
    user: ['profile.read'],
  },
})

Session API

session.isAuthenticated()
session.isGuest()

session.user           // typed user or null
session.can('users.write')

Request-level cache

Calling fromRequest multiple times during the same request will not re-verify the token or re-fetch the user.

const s1 = await auth.fromRequest(request)
const s2 = await auth.fromRequest(request)

// same instance

Usage with TanStack Router (SSR)

import { withAuthGuard } from 'tanstack-auth-session/adapters/tanstack-router'

export const beforeLoad = withAuthGuard(auth)
beforeLoad: async ({ context }) => {
  const session = await auth.fromRequest(context.request)

  if (session.isGuest()) {
    throw redirect({ to: '/login' })
  }

  return { session }
}

Usage with Remix

export const loader = async ({ request }) => {
  const session = await auth.fromRequest(request)

  if (session.isGuest()) {
    throw redirect('/login')
  }

  return json({ user: session.user })
}

Usage with Next.js (App Router)

import { cookies } from 'next/headers'

export async function getSession() {
  const cookieStore = cookies()

  const request = new Request('http://localhost', {
    headers: {
      cookie: cookieStore.toString(),
    },
  })

  return auth.fromRequest(request)
}

Usage with Express / Fastify

app.use(async (req, res, next) => {
  const request = new Request('http://localhost', {
    headers: {
      cookie: req.headers.cookie ?? '',
    },
  })

  req.session = await auth.fromRequest(request)
  next()
})

Client-side usage (React)

Provider

import { AuthSessionProvider } from 'tanstack-auth-session'

<AuthSessionProvider session={sessionFromSSR}>
  <App />
</AuthSessionProvider>

Hook

import { useAuthSession } from 'tanstack-auth-session'

const session = useAuthSession()

if (session.can('users.write')) {
  return <AdminPanel />
}

Security notes

  • Use httpOnly cookies
  • Always validate user with Zod
  • Never store session globally
  • Rotate tokens on logout (recommended)

Who is this for

  • Senior / Staff Frontend Engineers
  • Fullstack React developers
  • SSR applications
  • SaaS & B2B products
  • Teams that care about architecture

License

MIT

tanstack-auth-session

tanstack-auth-session

tanstack-auth-session