{
    "componentChunkName": "component---src-pages-blog-markdown-remark-fields-slug-js",
    "path": "/blog/how-to-add-auth-to-tanstack-start-with-supertokens",
    "result": {"data":{"markdownRemark":{"html":"<p>To add authentication to TanStack Start, mount SuperTokens’ auth API on a server route, read the session in server functions and <code class=\"language-text\">beforeLoad</code>, and render SuperTokens’ prebuilt login UI on an <code class=\"language-text\">/auth</code> route. This guide covers TanStack Start authentication end to end with an open-source, self-hostable provider.</p>\n<p>By the end, the app has email/password and Google login, protected routes, protected server functions, and SSR-safe sessions with automatic token refresh. The TanStack Start auth setup uses current APIs only, and the post flags the older ones you will still meet in 2025 tutorials.</p>\n<h2 id=\"how-authentication-works-in-tanstack-start\" style=\"position:relative;\"><a href=\"#how-authentication-works-in-tanstack-start\" aria-label=\"how authentication works in tanstack start permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>How Authentication Works in TanStack Start</h2>\n<p>TanStack Start authentication with SuperTokens runs across three layers. A server route serves the auth API. Server functions act as the security boundary. <code class=\"language-text\">beforeLoad</code> on layout routes handles navigation.</p>\n<table>\n<thead>\n<tr>\n<th>Layer</th>\n<th>TanStack Start primitive</th>\n<th>Job</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>Auth API</td>\n<td>Server route at <code class=\"language-text\">/api/auth/*</code></td>\n<td>Sign-in, sign-up, sign-out, token refresh</td>\n</tr>\n<tr>\n<td>Security boundary</td>\n<td><code class=\"language-text\">createServerFn</code> + <code class=\"language-text\">createMiddleware</code></td>\n<td>Verify the session before reading or writing private data</td>\n</tr>\n<tr>\n<td>User experience</td>\n<td><code class=\"language-text\">beforeLoad</code> on a layout route</td>\n<td>Redirect signed-out users before a page renders</td>\n</tr>\n</tbody>\n</table>\n<p>One line matters more than the rest. <strong>In TanStack Start, <code class=\"language-text\">beforeLoad</code> controls navigation, but every <code class=\"language-text\">createServerFn</code> is a public HTTP endpoint, so each one must verify the session itself.</strong> Route guards are UX. The server function is the boundary. You can see why in the browser’s Network tab: each server function is a request to a URL under <code class=\"language-text\">/_serverFn/</code>. TanStack’s own <a href=\"https://tanstack.com/start/latest/docs/framework/react/guide/authentication-overview\" target=\"_blank\" rel=\"nofollow\">authentication overview</a> says the same thing: enforce auth in every server function that reads or writes private data.</p>\n<h2 id=\"why-use-supertokens-for-tanstack-start-auth\" style=\"position:relative;\"><a href=\"#why-use-supertokens-for-tanstack-start-auth\" aria-label=\"why use supertokens for tanstack start auth permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Why Use SuperTokens for TanStack Start Auth</h2>\n<p>SuperTokens gives TanStack Start auth an open-source backend that you can run yourself, plus a prebuilt React login UI. It fits teams that want to own their user data and avoid per-user pricing.</p>\n<ul>\n<li><strong>Open source</strong> under Apache 2.0, for both <code class=\"language-text\">supertokens-node</code> and <code class=\"language-text\">supertokens-auth-react</code>.</li>\n<li><strong>Self-host or managed.</strong> Run the core with Docker, or use the managed service.</li>\n<li><a href=\"https://supertokens.com/pricing\" target=\"_blank\" rel=\"nofollow\">No per-user pricing when self-hosted</a> for core auth. Add-ons such as MFA are billed per user either way.</li>\n<li><strong>Prebuilt React UI</strong> for sign-in, sign-up and password reset.</li>\n<li><strong>Rotating refresh tokens</strong> with theft detection, plus recipes for MFA, multi-tenancy and roles.</li>\n</ul>\n<h2 id=\"prerequisites\" style=\"position:relative;\"><a href=\"#prerequisites\" aria-label=\"prerequisites permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Prerequisites</h2>\n<p>TanStack Start and SuperTokens need three things before you start: a recent Node version, a TanStack Start app, and a SuperTokens core to talk to.</p>\n<table>\n<thead>\n<tr>\n<th>Requirement</th>\n<th>Detail</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>Node</td>\n<td>22 or later (tested on <code class=\"language-text\">22.22.0</code>)</td>\n</tr>\n<tr>\n<td>TanStack Start app</td>\n<td><code class=\"language-text\">npx @tanstack/cli create my-app --framework React</code></td>\n</tr>\n<tr>\n<td>SuperTokens core</td>\n<td><code class=\"language-text\">https://try.supertokens.com</code> for local testing; the managed service or Docker for real use</td>\n</tr>\n</tbody>\n</table>\n<h2 id=\"step-1-install-supertokens-packages\" style=\"position:relative;\"><a href=\"#step-1-install-supertokens-packages\" aria-label=\"step 1 install supertokens packages permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Step 1: Install SuperTokens Packages</h2>\n<p>TanStack Start needs two SuperTokens packages: <code class=\"language-text\">supertokens-node</code> for the server and <code class=\"language-text\">supertokens-auth-react</code> for the login UI. The React SDK pulls in <code class=\"language-text\">supertokens-web-js</code> on its own, so you do not install it separately.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"95390730147096120000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied!\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`npm install supertokens-node@24.0.3 supertokens-auth-react@0.51.3`, `95390730147096120000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                <svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token function\">npm</span> <span class=\"token function\">install</span> supertokens-node@24.0.3 supertokens-auth-react@0.51.3</code></pre></div>\n<h2 id=\"step-2-configure-the-supertokens-backend\" style=\"position:relative;\"><a href=\"#step-2-configure-the-supertokens-backend\" aria-label=\"step 2 configure the supertokens backend permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Step 2: Configure the SuperTokens Backend</h2>\n<p>With your app info and recipes. Start with the shared app info, which both server and browser read.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"72105621179842180000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied!\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`// src/config/appInfo.ts\nexport const appInfo = {\n appName: 'TanStack Start + SuperTokens',\n apiDomain: 'http://localhost:3000',\n websiteDomain: 'http://localhost:3000',\n apiBasePath: '/api/auth',\n websiteBasePath: '/auth',\n}`, `72105621179842180000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                <svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"ts\"><pre class=\"language-ts\"><code class=\"language-ts\"><span class=\"token comment\">// src/config/appInfo.ts</span>\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">const</span> appInfo <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span>\n appName<span class=\"token operator\">:</span> <span class=\"token string\">'TanStack Start + SuperTokens'</span><span class=\"token punctuation\">,</span>\n apiDomain<span class=\"token operator\">:</span> <span class=\"token string\">'http://localhost:3000'</span><span class=\"token punctuation\">,</span>\n websiteDomain<span class=\"token operator\">:</span> <span class=\"token string\">'http://localhost:3000'</span><span class=\"token punctuation\">,</span>\n apiBasePath<span class=\"token operator\">:</span> <span class=\"token string\">'/api/auth'</span><span class=\"token punctuation\">,</span>\n websiteBasePath<span class=\"token operator\">:</span> <span class=\"token string\">'/auth'</span><span class=\"token punctuation\">,</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p><code class=\"language-text\">apiBasePath: \"/api/auth\"</code> tells SuperTokens where its API lives, so sign-in becomes <code class=\"language-text\">POST /api/auth/signin</code>. <code class=\"language-text\">websiteBasePath: \"/auth\"</code> tells it where the login UI lives. Both must match the routes you create in Steps 3 and 5.</p>\n<p>Now the backend config. It enables email/password, Google and GitHub, and sessions.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"91921003533406190000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied!\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`// src/server/supertokens.ts\nimport SuperTokens from 'supertokens-node'\nimport EmailPassword from 'supertokens-node/recipe/emailpassword'\nimport ThirdParty from 'supertokens-node/recipe/thirdparty'\nimport Session from 'supertokens-node/recipe/session'\nimport { appInfo } from '#/config/appInfo'\n\nconst provider = (thirdPartyId: string, prefix: string) => ({\n config: {\n   thirdPartyId,\n   clients: [{\n     clientId: process.env[\\`\\${prefix}_CLIENT_ID\\`] ?? '',\n     clientSecret: process.env[\\`\\${prefix}_CLIENT_SECRET\\`] ?? '',\n   }],\n },\n})\n\nlet initialized = false\n\nexport function ensureSuperTokensInit() {\n if (initialized) return\n SuperTokens.init({\n   framework: 'custom',\n   supertokens: {\n     connectionURI: process.env.SUPERTOKENS_CONNECTION_URI ?? 'https://try.supertokens.com',\n     apiKey: process.env.SUPERTOKENS_API_KEY,\n   },\n   appInfo,\n   recipeList: [\n     EmailPassword.init(),\n     ThirdParty.init({\n       signInAndUpFeature: { providers: [provider('google', 'GOOGLE'), provider('github', 'GITHUB')] },\n     }),\n     Session.init(),\n   ],\n })\n initialized = true\n}`, `91921003533406190000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                <svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"ts\"><pre class=\"language-ts\"><code class=\"language-ts\"><span class=\"token comment\">// src/server/supertokens.ts</span>\n<span class=\"token keyword\">import</span> SuperTokens <span class=\"token keyword\">from</span> <span class=\"token string\">'supertokens-node'</span>\n<span class=\"token keyword\">import</span> EmailPassword <span class=\"token keyword\">from</span> <span class=\"token string\">'supertokens-node/recipe/emailpassword'</span>\n<span class=\"token keyword\">import</span> ThirdParty <span class=\"token keyword\">from</span> <span class=\"token string\">'supertokens-node/recipe/thirdparty'</span>\n<span class=\"token keyword\">import</span> Session <span class=\"token keyword\">from</span> <span class=\"token string\">'supertokens-node/recipe/session'</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> appInfo <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'#/config/appInfo'</span>\n\n<span class=\"token keyword\">const</span> <span class=\"token function-variable function\">provider</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span>thirdPartyId<span class=\"token operator\">:</span> <span class=\"token builtin\">string</span><span class=\"token punctuation\">,</span> prefix<span class=\"token operator\">:</span> <span class=\"token builtin\">string</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n config<span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n   thirdPartyId<span class=\"token punctuation\">,</span>\n   clients<span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span><span class=\"token punctuation\">{</span>\n     clientId<span class=\"token operator\">:</span> process<span class=\"token punctuation\">.</span>env<span class=\"token punctuation\">[</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>prefix<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token string\">_CLIENT_ID</span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">]</span> <span class=\"token operator\">??</span> <span class=\"token string\">''</span><span class=\"token punctuation\">,</span>\n     clientSecret<span class=\"token operator\">:</span> process<span class=\"token punctuation\">.</span>env<span class=\"token punctuation\">[</span><span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>prefix<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token string\">_CLIENT_SECRET</span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">]</span> <span class=\"token operator\">??</span> <span class=\"token string\">''</span><span class=\"token punctuation\">,</span>\n   <span class=\"token punctuation\">}</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span>\n <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n\n<span class=\"token keyword\">let</span> initialized <span class=\"token operator\">=</span> <span class=\"token boolean\">false</span>\n\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">function</span> <span class=\"token function\">ensureSuperTokensInit</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>initialized<span class=\"token punctuation\">)</span> <span class=\"token keyword\">return</span>\n SuperTokens<span class=\"token punctuation\">.</span><span class=\"token function\">init</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n   framework<span class=\"token operator\">:</span> <span class=\"token string\">'custom'</span><span class=\"token punctuation\">,</span>\n   supertokens<span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n     connectionURI<span class=\"token operator\">:</span> process<span class=\"token punctuation\">.</span>env<span class=\"token punctuation\">.</span><span class=\"token constant\">SUPERTOKENS_CONNECTION_URI</span> <span class=\"token operator\">??</span> <span class=\"token string\">'https://try.supertokens.com'</span><span class=\"token punctuation\">,</span>\n     apiKey<span class=\"token operator\">:</span> process<span class=\"token punctuation\">.</span>env<span class=\"token punctuation\">.</span><span class=\"token constant\">SUPERTOKENS_API_KEY</span><span class=\"token punctuation\">,</span>\n   <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n   appInfo<span class=\"token punctuation\">,</span>\n   recipeList<span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>\n     EmailPassword<span class=\"token punctuation\">.</span><span class=\"token function\">init</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n     ThirdParty<span class=\"token punctuation\">.</span><span class=\"token function\">init</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n       signInAndUpFeature<span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> providers<span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span><span class=\"token function\">provider</span><span class=\"token punctuation\">(</span><span class=\"token string\">'google'</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'GOOGLE'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span> <span class=\"token function\">provider</span><span class=\"token punctuation\">(</span><span class=\"token string\">'github'</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'GITHUB'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">]</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n     <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n     Session<span class=\"token punctuation\">.</span><span class=\"token function\">init</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n   <span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span>\n <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n initialized <span class=\"token operator\">=</span> <span class=\"token boolean\">true</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>Two details matter here. <code class=\"language-text\">framework: 'custom'</code> tells SuperTokens it will receive standard Web <code class=\"language-text\">Request</code> objects, which is what TanStack Start hands you. <code class=\"language-text\">ensureSuperTokensInit()</code> is idempotent, so every server route and server function can call it safely without initialising twice.</p>\n<h2 id=\"step-3-mount-the-auth-api-on-a-tanstack-start-server-route\" style=\"position:relative;\"><a href=\"#step-3-mount-the-auth-api-on-a-tanstack-start-server-route\" aria-label=\"step 3 mount the auth api on a tanstack start server route permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Step 3: Mount the Auth API on a TanStack Start Server Route</h2>\n<p>SuperTokens’ auth API mounts on a single TanStack Start server route that forwards every HTTP method to <code class=\"language-text\">handleAuthAPIRequest()</code>. A splat route, <code class=\"language-text\">$.ts</code>, catches every path under <code class=\"language-text\">/api/auth</code>.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"12335661627389415000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied!\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`// src/routes/api/auth/\\$.ts\nimport { createFileRoute } from '@tanstack/react-router'\nimport { handleAuthAPIRequest } from 'supertokens-node/custom'\nimport { ensureSuperTokensInit } from '#/server/supertokens'\n\nasync function handler({ request }: { request: Request }) {\n ensureSuperTokensInit()\n return handleAuthAPIRequest()(request)\n}\n\nexport const Route = createFileRoute('/api/auth/\\$')({\n server: {\n   handlers: {\n     GET: handler,\n     POST: handler,\n     PUT: handler,\n     DELETE: handler,\n     PATCH: handler,\n     OPTIONS: handler,\n   },\n },\n})`, `12335661627389415000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                <svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"ts\"><pre class=\"language-ts\"><code class=\"language-ts\"><span class=\"token comment\">// src/routes/api/auth/$.ts</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> createFileRoute <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@tanstack/react-router'</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> handleAuthAPIRequest <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'supertokens-node/custom'</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> ensureSuperTokensInit <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'#/server/supertokens'</span>\n\n<span class=\"token keyword\">async</span> <span class=\"token keyword\">function</span> <span class=\"token function\">handler</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> request <span class=\"token punctuation\">}</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> request<span class=\"token operator\">:</span> Request <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n <span class=\"token function\">ensureSuperTokensInit</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n <span class=\"token keyword\">return</span> <span class=\"token function\">handleAuthAPIRequest</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">(</span>request<span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">const</span> Route <span class=\"token operator\">=</span> <span class=\"token function\">createFileRoute</span><span class=\"token punctuation\">(</span><span class=\"token string\">'/api/auth/$'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n server<span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n   handlers<span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n     <span class=\"token constant\">GET</span><span class=\"token operator\">:</span> handler<span class=\"token punctuation\">,</span>\n     <span class=\"token constant\">POST</span><span class=\"token operator\">:</span> handler<span class=\"token punctuation\">,</span>\n     <span class=\"token constant\">PUT</span><span class=\"token operator\">:</span> handler<span class=\"token punctuation\">,</span>\n     <span class=\"token constant\">DELETE</span><span class=\"token operator\">:</span> handler<span class=\"token punctuation\">,</span>\n     <span class=\"token constant\">PATCH</span><span class=\"token operator\">:</span> handler<span class=\"token punctuation\">,</span>\n     <span class=\"token constant\">OPTIONS</span><span class=\"token operator\">:</span> handler<span class=\"token punctuation\">,</span>\n   <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span></code></pre></div>\n<p>This is the TanStack Start equivalent of the Next.js App Router catch-all. The route has no component, so it only ever answers HTTP requests.</p>\n<p>The splat route forwards whatever path arrives, and SuperTokens decides what it means. Email/password sign-in lands on <code class=\"language-text\">/api/auth/signin</code>, sign-up on <code class=\"language-text\">/api/auth/signup</code>, and sign-out on <code class=\"language-text\">/api/auth/signout</code>. The frontend SDK calls <code class=\"language-text\">/api/auth/session/refresh</code> to rotate tokens. Google and GitHub start their OAuth flow at <code class=\"language-text\">/api/auth/authorisationurl</code> and finish at <code class=\"language-text\">/api/auth/signinup</code>. You never write these handlers yourself. Adding a recipe in Step 2 adds its endpoints.</p>\n<p><strong>Import path:</strong> use <code class=\"language-text\">supertokens-node/custom</code>. Older SuperTokens examples import from <code class=\"language-text\">supertokens-node/customFramework</code>, but that subpath is not in the package’s <code class=\"language-text\">exports</code> map in <code class=\"language-text\">24.0.3</code>. Node throws <code class=\"language-text\">ERR_PACKAGE_PATH_NOT_EXPORTED</code> at startup, and TypeScript reports <code class=\"language-text\">Cannot find module</code>. Both paths point at the same helpers.</p>\n<h2 id=\"step-4-initialise-supertokens-on-the-frontend\" style=\"position:relative;\"><a href=\"#step-4-initialise-supertokens-on-the-frontend\" aria-label=\"step 4 initialise supertokens on the frontend permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Step 4: Initialise SuperTokens on the Frontend</h2>\n<p><code class=\"language-text\">supertokens-auth-react</code> reads <code class=\"language-text\">window</code>, so in TanStack Start it must initialise in the browser only. A <code class=\"language-text\">typeof window</code> guard keeps it out of server rendering.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"88515913174243500000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied!\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`// src/config/frontend.ts\nimport SuperTokens from 'supertokens-auth-react'\nimport EmailPassword from 'supertokens-auth-react/recipe/emailpassword'\nimport ThirdParty, { Github, Google } from 'supertokens-auth-react/recipe/thirdparty'\nimport Session from 'supertokens-auth-react/recipe/session'\nimport { EmailPasswordPreBuiltUI } from 'supertokens-auth-react/recipe/emailpassword/prebuiltui'\nimport { ThirdPartyPreBuiltUI } from 'supertokens-auth-react/recipe/thirdparty/prebuiltui'\nimport { appInfo } from '#/config/appInfo'\n\nexport const PreBuiltUIList = [EmailPasswordPreBuiltUI, ThirdPartyPreBuiltUI]\n\nlet initialized = false\n\nexport function ensureFrontendInit() {\n // supertokens-auth-react reads window, so it must never run during SSR.\n if (initialized || typeof window === 'undefined') return\n SuperTokens.init({\n   appInfo,\n   recipeList: [\n     EmailPassword.init(),\n     ThirdParty.init({\n       signInAndUpFeature: { providers: [Google.init(), Github.init()] },\n     }),\n     Session.init(),\n   ],\n })\n initialized = true\n}`, `88515913174243500000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                <svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"ts\"><pre class=\"language-ts\"><code class=\"language-ts\"><span class=\"token comment\">// src/config/frontend.ts</span>\n<span class=\"token keyword\">import</span> SuperTokens <span class=\"token keyword\">from</span> <span class=\"token string\">'supertokens-auth-react'</span>\n<span class=\"token keyword\">import</span> EmailPassword <span class=\"token keyword\">from</span> <span class=\"token string\">'supertokens-auth-react/recipe/emailpassword'</span>\n<span class=\"token keyword\">import</span> ThirdParty<span class=\"token punctuation\">,</span> <span class=\"token punctuation\">{</span> Github<span class=\"token punctuation\">,</span> Google <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'supertokens-auth-react/recipe/thirdparty'</span>\n<span class=\"token keyword\">import</span> Session <span class=\"token keyword\">from</span> <span class=\"token string\">'supertokens-auth-react/recipe/session'</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> EmailPasswordPreBuiltUI <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'supertokens-auth-react/recipe/emailpassword/prebuiltui'</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> ThirdPartyPreBuiltUI <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'supertokens-auth-react/recipe/thirdparty/prebuiltui'</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> appInfo <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'#/config/appInfo'</span>\n\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">const</span> PreBuiltUIList <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span>EmailPasswordPreBuiltUI<span class=\"token punctuation\">,</span> ThirdPartyPreBuiltUI<span class=\"token punctuation\">]</span>\n\n<span class=\"token keyword\">let</span> initialized <span class=\"token operator\">=</span> <span class=\"token boolean\">false</span>\n\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">function</span> <span class=\"token function\">ensureFrontendInit</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n <span class=\"token comment\">// supertokens-auth-react reads window, so it must never run during SSR.</span>\n <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>initialized <span class=\"token operator\">||</span> <span class=\"token keyword\">typeof</span> window <span class=\"token operator\">===</span> <span class=\"token string\">'undefined'</span><span class=\"token punctuation\">)</span> <span class=\"token keyword\">return</span>\n SuperTokens<span class=\"token punctuation\">.</span><span class=\"token function\">init</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n   appInfo<span class=\"token punctuation\">,</span>\n   recipeList<span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>\n     EmailPassword<span class=\"token punctuation\">.</span><span class=\"token function\">init</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n     ThirdParty<span class=\"token punctuation\">.</span><span class=\"token function\">init</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n       signInAndUpFeature<span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> providers<span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>Google<span class=\"token punctuation\">.</span><span class=\"token function\">init</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span> Github<span class=\"token punctuation\">.</span><span class=\"token function\">init</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">]</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n     <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n     Session<span class=\"token punctuation\">.</span><span class=\"token function\">init</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n   <span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span>\n <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n initialized <span class=\"token operator\">=</span> <span class=\"token boolean\">true</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>Next, wrap the app in <code class=\"language-text\">SuperTokensWrapper</code> inside <code class=\"language-text\">__root.tsx</code>. The wrapper sits inside TanStack Router’s <code class=\"language-text\">ClientOnly</code>, so the server renders the page content and the browser adds the session context after hydration.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"1037671894621916500\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied!\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`// src/routes/__root.tsx\nimport { ClientOnly, HeadContent, Scripts, createRootRoute } from '@tanstack/react-router'\nimport { SuperTokensWrapper } from 'supertokens-auth-react'\nimport { ensureFrontendInit } from '#/config/frontend'\nimport { SessionStatus } from '#/components/SessionStatus'\nimport appCss from '../styles.css?url'\n\nensureFrontendInit()\n\nexport const Route = createRootRoute({\n head: () => ({\n   meta: [\n     { charSet: 'utf-8' },\n     { name: 'viewport', content: 'width=device-width, initial-scale=1' },\n     { title: 'TanStack Start + SuperTokens' },\n   ],\n   links: [{ rel: 'stylesheet', href: appCss }],\n }),\n shellComponent: RootDocument,\n})\n\nfunction RootDocument({ children }: { children: React.ReactNode }) {\n return (\n   <html lang=&quot;en&quot;>\n     <head>\n       <HeadContent />\n     </head>\n     <body>\n       <ClientOnly fallback={children}>\n         <SuperTokensWrapper>\n           <header><SessionStatus /></header>\n           {children}\n         </SuperTokensWrapper>\n       </ClientOnly>\n       <Scripts />\n     </body>\n   </html>\n )\n}`, `1037671894621916500`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                <svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"tsx\"><pre class=\"language-tsx\"><code class=\"language-tsx\"><span class=\"token comment\">// src/routes/__root.tsx</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> ClientOnly<span class=\"token punctuation\">,</span> HeadContent<span class=\"token punctuation\">,</span> Scripts<span class=\"token punctuation\">,</span> createRootRoute <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@tanstack/react-router'</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> SuperTokensWrapper <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'supertokens-auth-react'</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> ensureFrontendInit <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'#/config/frontend'</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> SessionStatus <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'#/components/SessionStatus'</span>\n<span class=\"token keyword\">import</span> appCss <span class=\"token keyword\">from</span> <span class=\"token string\">'../styles.css?url'</span>\n\n<span class=\"token function\">ensureFrontendInit</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">const</span> Route <span class=\"token operator\">=</span> <span class=\"token function\">createRootRoute</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n <span class=\"token function-variable function\">head</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n   meta<span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>\n     <span class=\"token punctuation\">{</span> charSet<span class=\"token operator\">:</span> <span class=\"token string\">'utf-8'</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n     <span class=\"token punctuation\">{</span> name<span class=\"token operator\">:</span> <span class=\"token string\">'viewport'</span><span class=\"token punctuation\">,</span> content<span class=\"token operator\">:</span> <span class=\"token string\">'width=device-width, initial-scale=1'</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n     <span class=\"token punctuation\">{</span> title<span class=\"token operator\">:</span> <span class=\"token string\">'TanStack Start + SuperTokens'</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n   <span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span>\n   links<span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span><span class=\"token punctuation\">{</span> rel<span class=\"token operator\">:</span> <span class=\"token string\">'stylesheet'</span><span class=\"token punctuation\">,</span> href<span class=\"token operator\">:</span> appCss <span class=\"token punctuation\">}</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span>\n <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n shellComponent<span class=\"token operator\">:</span> RootDocument<span class=\"token punctuation\">,</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n\n<span class=\"token keyword\">function</span> <span class=\"token function\">RootDocument</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> children <span class=\"token punctuation\">}</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> children<span class=\"token operator\">:</span> React<span class=\"token punctuation\">.</span>ReactNode <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n <span class=\"token keyword\">return</span> <span class=\"token punctuation\">(</span>\n   <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>html</span> <span class=\"token attr-name\">lang</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>en<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span><span class=\"token plain-text\">\n     </span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>head</span><span class=\"token punctuation\">></span></span><span class=\"token plain-text\">\n       </span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span><span class=\"token class-name\">HeadContent</span></span> <span class=\"token punctuation\">/></span></span><span class=\"token plain-text\">\n     </span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>head</span><span class=\"token punctuation\">></span></span><span class=\"token plain-text\">\n     </span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>body</span><span class=\"token punctuation\">></span></span><span class=\"token plain-text\">\n       </span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span><span class=\"token class-name\">ClientOnly</span></span> <span class=\"token attr-name\">fallback</span><span class=\"token script language-javascript\"><span class=\"token script-punctuation punctuation\">=</span><span class=\"token punctuation\">{</span>children<span class=\"token punctuation\">}</span></span><span class=\"token punctuation\">></span></span><span class=\"token plain-text\">\n         </span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span><span class=\"token class-name\">SuperTokensWrapper</span></span><span class=\"token punctuation\">></span></span><span class=\"token plain-text\">\n           </span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>header</span><span class=\"token punctuation\">></span></span><span class=\"token plain-text\">&lt;SessionStatus /></span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>header</span><span class=\"token punctuation\">></span></span><span class=\"token plain-text\">\n           </span><span class=\"token punctuation\">{</span>children<span class=\"token punctuation\">}</span><span class=\"token plain-text\">\n         </span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span><span class=\"token class-name\">SuperTokensWrapper</span></span><span class=\"token punctuation\">></span></span><span class=\"token plain-text\">\n       </span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span><span class=\"token class-name\">ClientOnly</span></span><span class=\"token punctuation\">></span></span><span class=\"token plain-text\">\n       </span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span><span class=\"token class-name\">Scripts</span></span> <span class=\"token punctuation\">/></span></span><span class=\"token plain-text\">\n     </span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>body</span><span class=\"token punctuation\">></span></span><span class=\"token plain-text\">\n   </span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>html</span><span class=\"token punctuation\">></span></span>\n <span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>The <code class=\"language-text\">fallback={children}</code> prop matters. Without it, the server would render an empty body. With it, the server still renders the full page for SEO and fast first paint.</p>\n<p><code class=\"language-text\">SuperTokensWrapper</code> provides the React context that the prebuilt UI and <code class=\"language-text\">useSessionContext()</code> read. Any component that needs the session in the browser must render inside it.</p>\n<h2 id=\"step-5-add-the-login-and-sign-up-ui\" style=\"position:relative;\"><a href=\"#step-5-add-the-login-and-sign-up-ui\" aria-label=\"step 5 add the login and sign up ui permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Step 5: Add the Login and Sign-Up UI</h2>\n<p>SuperTokens’ prebuilt login UI renders on a TanStack Start splat route at <code class=\"language-text\">/auth</code>, using <code class=\"language-text\">canHandleRoute</code> and <code class=\"language-text\">getRoutingComponent</code>. The splat catches <code class=\"language-text\">/auth</code>, <code class=\"language-text\">/auth/reset-password</code> and every other path the UI needs.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"72155205557299350000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied!\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`// src/routes/auth/\\$.tsx\nimport { createFileRoute } from '@tanstack/react-router'\nimport { canHandleRoute, getRoutingComponent } from 'supertokens-auth-react/ui'\nimport { PreBuiltUIList } from '#/config/frontend'\n\nexport const Route = createFileRoute('/auth/\\$')({\n // The prebuilt UI reads window.location, so render it in the browser only.\n ssr: false,\n component: AuthPage,\n})\n\nfunction AuthPage() {\n if (canHandleRoute(PreBuiltUIList)) return getRoutingComponent(PreBuiltUIList)\n return <p>Page not found</p>\n}`, `72155205557299350000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                <svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"tsx\"><pre class=\"language-tsx\"><code class=\"language-tsx\"><span class=\"token comment\">// src/routes/auth/$.tsx</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> createFileRoute <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@tanstack/react-router'</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> canHandleRoute<span class=\"token punctuation\">,</span> getRoutingComponent <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'supertokens-auth-react/ui'</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> PreBuiltUIList <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'#/config/frontend'</span>\n\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">const</span> Route <span class=\"token operator\">=</span> <span class=\"token function\">createFileRoute</span><span class=\"token punctuation\">(</span><span class=\"token string\">'/auth/$'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n <span class=\"token comment\">// The prebuilt UI reads window.location, so render it in the browser only.</span>\n ssr<span class=\"token operator\">:</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">,</span>\n component<span class=\"token operator\">:</span> AuthPage<span class=\"token punctuation\">,</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n\n<span class=\"token keyword\">function</span> <span class=\"token function\">AuthPage</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token function\">canHandleRoute</span><span class=\"token punctuation\">(</span>PreBuiltUIList<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span> <span class=\"token keyword\">return</span> <span class=\"token function\">getRoutingComponent</span><span class=\"token punctuation\">(</span>PreBuiltUIList<span class=\"token punctuation\">)</span>\n <span class=\"token keyword\">return</span> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>p</span><span class=\"token punctuation\">></span></span><span class=\"token plain-text\">Page not found</span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>p</span><span class=\"token punctuation\">></span></span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p><code class=\"language-text\">ssr: false</code> is TanStack Router’s selective SSR option. It skips server rendering for this route only, which is exactly what a <code class=\"language-text\">window</code>-dependent login form needs. The rest of the app still renders on the server.</p>\n<h3 id=\"configuring-google-and-github-login\" style=\"position:relative;\"><a href=\"#configuring-google-and-github-login\" aria-label=\"configuring google and github login permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><strong>Configuring Google and GitHub Login</strong></h3>\n<p>TanStack Start social login with Google and GitHub needs OAuth credentials on the server and a provider button on the client. The client side is already done in Step 4 with <code class=\"language-text\">Google.init()</code> and <code class=\"language-text\">Github.init()</code>.</p>\n<p>For the server side, create an OAuth app in each provider’s console and set the callback URL to <code class=\"language-text\">http://localhost:3000/auth/callback/google</code> or <code class=\"language-text\">/auth/callback/github</code>. Then add the credentials to <code class=\"language-text\">.env</code>:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"47525295787976330000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied!\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`# .env\nGOOGLE_CLIENT_ID=...\nGOOGLE_CLIENT_SECRET=...\nGITHUB_CLIENT_ID=...\nGITHUB_CLIENT_SECRET=...`, `47525295787976330000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                <svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token comment\"># .env</span>\n<span class=\"token assign-left variable\">GOOGLE_CLIENT_ID</span><span class=\"token operator\">=</span><span class=\"token punctuation\">..</span>.\n<span class=\"token assign-left variable\">GOOGLE_CLIENT_SECRET</span><span class=\"token operator\">=</span><span class=\"token punctuation\">..</span>.\n<span class=\"token assign-left variable\">GITHUB_CLIENT_ID</span><span class=\"token operator\">=</span><span class=\"token punctuation\">..</span>.\n<span class=\"token assign-left variable\">GITHUB_CLIENT_SECRET</span><span class=\"token operator\">=</span><span class=\"token punctuation\">..</span>.</code></pre></div>\n<p>The <code class=\"language-text\">provider()</code> helper in Step 2 reads these values. The client secrets never reach the browser, because only server code reads them. SuperTokens’ <a href=\"https://supertokens.com/docs/authentication/social/initial-setup\" target=\"_blank\" rel=\"nofollow\">third-party login docs</a> cover Apple, Facebook and custom OIDC providers the same way.</p>\n<h2 id=\"step-6-read-the-session-in-server-functions\" style=\"position:relative;\"><a href=\"#step-6-read-the-session-in-server-functions\" aria-label=\"step 6 read the session in server functions permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Step 6: Read the Session in Server Functions</h2>\n<p>TanStack Start session management with SuperTokens starts with one helper. It reads the request with <code class=\"language-text\">getRequest()</code>, then verifies it with <code class=\"language-text\">getSessionForSSR()</code>. TanStack Start server functions auth builds on the same helper, through both a public session lookup and the protection middleware.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"70073764887346540000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied!\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`// src/server/auth.ts\nimport { createMiddleware, createServerFn } from '@tanstack/react-start'\nimport { getRequest, setResponseStatus } from '@tanstack/react-start/server'\nimport { getSessionForSSR } from 'supertokens-node/custom'\nimport { ensureSuperTokensInit } from '#/server/supertokens'\n\nexport type UserSession =\n | { status: 'authenticated'; userId: string }\n | { status: 'needs-refresh' }\n | { status: 'unauthenticated' }\n\nasync function readSession(): Promise<UserSession> {\n ensureSuperTokensInit()\n const { accessTokenPayload, hasToken, error } = await getSessionForSSR(getRequest())\n if (error) throw error\n if (accessTokenPayload?.sub) return { status: 'authenticated', userId: accessTokenPayload.sub }\n // A token exists but did not verify: it has expired, so the client must refresh it.\n if (hasToken) return { status: 'needs-refresh' }\n return { status: 'unauthenticated' }\n}\n\nexport const getUserSession = createServerFn({ method: 'GET' }).handler(readSession)\n\nexport const authMiddleware = createMiddleware({ type: 'function' }).server(async ({ next }) => {\n const session = await readSession()\n if (session.status !== 'authenticated') {\n   setResponseStatus(401)\n   throw new Error('Unauthorized')\n }\n return next({ context: { userId: session.userId } })\n})`, `70073764887346540000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                <svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"ts\"><pre class=\"language-ts\"><code class=\"language-ts\"><span class=\"token comment\">// src/server/auth.ts</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> createMiddleware<span class=\"token punctuation\">,</span> createServerFn <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@tanstack/react-start'</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> getRequest<span class=\"token punctuation\">,</span> setResponseStatus <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@tanstack/react-start/server'</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> getSessionForSSR <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'supertokens-node/custom'</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> ensureSuperTokensInit <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'#/server/supertokens'</span>\n\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">type</span> <span class=\"token class-name\">UserSession</span> <span class=\"token operator\">=</span>\n <span class=\"token operator\">|</span> <span class=\"token punctuation\">{</span> status<span class=\"token operator\">:</span> <span class=\"token string\">'authenticated'</span><span class=\"token punctuation\">;</span> userId<span class=\"token operator\">:</span> <span class=\"token builtin\">string</span> <span class=\"token punctuation\">}</span>\n <span class=\"token operator\">|</span> <span class=\"token punctuation\">{</span> status<span class=\"token operator\">:</span> <span class=\"token string\">'needs-refresh'</span> <span class=\"token punctuation\">}</span>\n <span class=\"token operator\">|</span> <span class=\"token punctuation\">{</span> status<span class=\"token operator\">:</span> <span class=\"token string\">'unauthenticated'</span> <span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">async</span> <span class=\"token keyword\">function</span> <span class=\"token function\">readSession</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token operator\">:</span> <span class=\"token builtin\">Promise</span><span class=\"token operator\">&lt;</span>UserSession<span class=\"token operator\">></span> <span class=\"token punctuation\">{</span>\n <span class=\"token function\">ensureSuperTokensInit</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n <span class=\"token keyword\">const</span> <span class=\"token punctuation\">{</span> accessTokenPayload<span class=\"token punctuation\">,</span> hasToken<span class=\"token punctuation\">,</span> error <span class=\"token punctuation\">}</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">await</span> <span class=\"token function\">getSessionForSSR</span><span class=\"token punctuation\">(</span><span class=\"token function\">getRequest</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>error<span class=\"token punctuation\">)</span> <span class=\"token keyword\">throw</span> error\n <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>accessTokenPayload<span class=\"token operator\">?.</span>sub<span class=\"token punctuation\">)</span> <span class=\"token keyword\">return</span> <span class=\"token punctuation\">{</span> status<span class=\"token operator\">:</span> <span class=\"token string\">'authenticated'</span><span class=\"token punctuation\">,</span> userId<span class=\"token operator\">:</span> accessTokenPayload<span class=\"token punctuation\">.</span>sub <span class=\"token punctuation\">}</span>\n <span class=\"token comment\">// A token exists but did not verify: it has expired, so the client must refresh it.</span>\n <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>hasToken<span class=\"token punctuation\">)</span> <span class=\"token keyword\">return</span> <span class=\"token punctuation\">{</span> status<span class=\"token operator\">:</span> <span class=\"token string\">'needs-refresh'</span> <span class=\"token punctuation\">}</span>\n <span class=\"token keyword\">return</span> <span class=\"token punctuation\">{</span> status<span class=\"token operator\">:</span> <span class=\"token string\">'unauthenticated'</span> <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">const</span> getUserSession <span class=\"token operator\">=</span> <span class=\"token function\">createServerFn</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> method<span class=\"token operator\">:</span> <span class=\"token string\">'GET'</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">handler</span><span class=\"token punctuation\">(</span>readSession<span class=\"token punctuation\">)</span>\n\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">const</span> authMiddleware <span class=\"token operator\">=</span> <span class=\"token function\">createMiddleware</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> type<span class=\"token operator\">:</span> <span class=\"token string\">'function'</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">server</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">async</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> next <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n <span class=\"token keyword\">const</span> session <span class=\"token operator\">=</span> <span class=\"token keyword\">await</span> <span class=\"token function\">readSession</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>session<span class=\"token punctuation\">.</span>status <span class=\"token operator\">!==</span> <span class=\"token string\">'authenticated'</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n   <span class=\"token function\">setResponseStatus</span><span class=\"token punctuation\">(</span><span class=\"token number\">401</span><span class=\"token punctuation\">)</span>\n   <span class=\"token keyword\">throw</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">Error</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Unauthorized'</span><span class=\"token punctuation\">)</span>\n <span class=\"token punctuation\">}</span>\n <span class=\"token keyword\">return</span> <span class=\"token function\">next</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> context<span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> userId<span class=\"token operator\">:</span> session<span class=\"token punctuation\">.</span>userId <span class=\"token punctuation\">}</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span></code></pre></div>\n<p><code class=\"language-text\">getSessionForSSR</code> returns three fields, and the three session states map directly onto them:</p>\n<table>\n<thead>\n<tr>\n<th><code class=\"language-text\">accessTokenPayload</code></th>\n<th><code class=\"language-text\">hasToken</code></th>\n<th>Meaning</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>present</td>\n<td><code class=\"language-text\">true</code></td>\n<td>Valid session. Read the user ID from <code class=\"language-text\">sub</code>.</td>\n</tr>\n<tr>\n<td><code class=\"language-text\">undefined</code></td>\n<td><code class=\"language-text\">true</code></td>\n<td>Token exists but failed verification, usually because it expired. Refresh it.</td>\n</tr>\n<tr>\n<td><code class=\"language-text\">undefined</code></td>\n<td><code class=\"language-text\">false</code></td>\n<td>No session. Send the user to sign in.</td>\n</tr>\n</tbody>\n</table>\n<p>The third state never contacts the SuperTokens core, because there is no token to verify. That keeps signed-out requests cheap.</p>\n<p><code class=\"language-text\">getRequest()</code> is what makes this work inside a server function. It returns the incoming Web <code class=\"language-text\">Request</code> for the current call, cookies included. SuperTokens reads the <code class=\"language-text\">sAccessToken</code> cookie from it and verifies the JWT signature against the core’s public keys. The <code class=\"language-text\">sub</code> claim in the payload is the SuperTokens user ID.</p>\n<h3 id=\"protect-server-functions-with-middleware\" style=\"position:relative;\"><a href=\"#protect-server-functions-with-middleware\" aria-label=\"protect server functions with middleware permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><strong>Protect Server Functions with Middleware</strong></h3>\n<p>TanStack Start middleware authentication puts the session check in one place and attaches a typed <code class=\"language-text\">userId</code> to every protected server function. The <code class=\"language-text\">authMiddleware</code> above rejects requests without a session and passes <code class=\"language-text\">userId</code> through <code class=\"language-text\">next({ context })</code>.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"57852570657906150000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied!\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`// src/server/notes.ts\nimport { createServerFn } from '@tanstack/react-start'\nimport { authMiddleware } from '#/server/auth'\n\nexport const getMyNotes = createServerFn({ method: 'GET' })\n .middleware([authMiddleware])\n .handler(async ({ context }) => {\n   // context.userId is typed and guaranteed: the middleware rejected anyone without a session.\n   return { userId: context.userId, notes: ['Ship the TanStack Start auth guide'] }\n })\n\nexport const addNote = createServerFn({ method: 'POST' })\n .middleware([authMiddleware])\n .validator((input: { text: string }) => {\n   if (typeof input?.text !== 'string' || input.text.length === 0) throw new Error('text is required')\n   return input\n })\n .handler(async ({ context, data }) => {\n   return { userId: context.userId, saved: data.text }\n })`, `57852570657906150000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                <svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"ts\"><pre class=\"language-ts\"><code class=\"language-ts\"><span class=\"token comment\">// src/server/notes.ts</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> createServerFn <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@tanstack/react-start'</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> authMiddleware <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'#/server/auth'</span>\n\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">const</span> getMyNotes <span class=\"token operator\">=</span> <span class=\"token function\">createServerFn</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> method<span class=\"token operator\">:</span> <span class=\"token string\">'GET'</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n <span class=\"token punctuation\">.</span><span class=\"token function\">middleware</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span>authMiddleware<span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span>\n <span class=\"token punctuation\">.</span><span class=\"token function\">handler</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">async</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> context <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n   <span class=\"token comment\">// context.userId is typed and guaranteed: the middleware rejected anyone without a session.</span>\n   <span class=\"token keyword\">return</span> <span class=\"token punctuation\">{</span> userId<span class=\"token operator\">:</span> context<span class=\"token punctuation\">.</span>userId<span class=\"token punctuation\">,</span> notes<span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span><span class=\"token string\">'Ship the TanStack Start auth guide'</span><span class=\"token punctuation\">]</span> <span class=\"token punctuation\">}</span>\n <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">const</span> addNote <span class=\"token operator\">=</span> <span class=\"token function\">createServerFn</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> method<span class=\"token operator\">:</span> <span class=\"token string\">'POST'</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n <span class=\"token punctuation\">.</span><span class=\"token function\">middleware</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span>authMiddleware<span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span>\n <span class=\"token punctuation\">.</span><span class=\"token function\">validator</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span>input<span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> text<span class=\"token operator\">:</span> <span class=\"token builtin\">string</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n   <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">typeof</span> input<span class=\"token operator\">?.</span>text <span class=\"token operator\">!==</span> <span class=\"token string\">'string'</span> <span class=\"token operator\">||</span> input<span class=\"token punctuation\">.</span>text<span class=\"token punctuation\">.</span>length <span class=\"token operator\">===</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span> <span class=\"token keyword\">throw</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">Error</span><span class=\"token punctuation\">(</span><span class=\"token string\">'text is required'</span><span class=\"token punctuation\">)</span>\n   <span class=\"token keyword\">return</span> input\n <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n <span class=\"token punctuation\">.</span><span class=\"token function\">handler</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">async</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> context<span class=\"token punctuation\">,</span> data <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n   <span class=\"token keyword\">return</span> <span class=\"token punctuation\">{</span> userId<span class=\"token operator\">:</span> context<span class=\"token punctuation\">.</span>userId<span class=\"token punctuation\">,</span> saved<span class=\"token operator\">:</span> data<span class=\"token punctuation\">.</span>text <span class=\"token punctuation\">}</span>\n <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span></code></pre></div>\n<p>Inside the handler, <code class=\"language-text\">context.userId</code> is a <code class=\"language-text\">string</code>, not <code class=\"language-text\">string | undefined</code>. TypeScript knows the middleware ran. Note that <code class=\"language-text\">.validator()</code> is the current name; <code class=\"language-text\">.inputValidator()</code> still works but is marked deprecated.</p>\n<p>Middleware runs before validation. An unauthenticated request with invalid input gets a 401, not a validation error. The endpoint never reveals its input rules to anyone without a session.</p>\n<h3 id=\"handling-expired-access-tokens\" style=\"position:relative;\"><a href=\"#handling-expired-access-tokens\" aria-label=\"handling expired access tokens permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><strong>Handling Expired Access Tokens</strong></h3>\n<p>Access tokens in SuperTokens are short-lived, so a TanStack Start app will regularly see a server request carrying an expired one. Handling that state correctly is what keeps users signed in, and it is the step most TanStack Start auth guides skip.</p>\n<p>The server cannot refresh the token itself. Refresh tokens are scoped to the refresh endpoint path, so the browser does not send them on page requests. The browser has to do it. So when <code class=\"language-text\">readSession()</code> returns <code class=\"language-text\">needs-refresh</code>, the protected layout redirects to a client-only route that runs the refresh and sends the user back.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"6609001363288525000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied!\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`// src/routes/refresh-session.tsx\nimport { useEffect } from 'react'\nimport { createFileRoute } from '@tanstack/react-router'\nimport Session from 'supertokens-auth-react/recipe/session'\nimport { redirectToAuth } from 'supertokens-auth-react'\n\nexport const Route = createFileRoute('/refresh-session')({\n ssr: false,\n validateSearch: (search: Record<string, unknown>) => ({\n   redirectToPath: typeof search.redirectToPath === 'string' ? search.redirectToPath : '/',\n }),\n component: RefreshSession,\n})\n\nfunction RefreshSession() {\n const { redirectToPath } = Route.useSearch()\n useEffect(() => {\n   Session.attemptRefreshingSession().then((refreshed) => {\n     // Rotated tokens are now in cookies; a full navigation re-runs the SSR session check.\n     if (refreshed) window.location.href = redirectToPath\n     else redirectToAuth({ redirectBack: false })\n   })\n }, [redirectToPath])\n return <p>Refreshing your session...</p>\n}`, `6609001363288525000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                <svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"tsx\"><pre class=\"language-tsx\"><code class=\"language-tsx\"><span class=\"token comment\">// src/routes/refresh-session.tsx</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> useEffect <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'react'</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> createFileRoute <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@tanstack/react-router'</span>\n<span class=\"token keyword\">import</span> Session <span class=\"token keyword\">from</span> <span class=\"token string\">'supertokens-auth-react/recipe/session'</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> redirectToAuth <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'supertokens-auth-react'</span>\n\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">const</span> Route <span class=\"token operator\">=</span> <span class=\"token function\">createFileRoute</span><span class=\"token punctuation\">(</span><span class=\"token string\">'/refresh-session'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n ssr<span class=\"token operator\">:</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">,</span>\n <span class=\"token function-variable function\">validateSearch</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">(</span>search<span class=\"token operator\">:</span> Record<span class=\"token operator\">&lt;</span><span class=\"token builtin\">string</span><span class=\"token punctuation\">,</span> <span class=\"token builtin\">unknown</span><span class=\"token operator\">></span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n   redirectToPath<span class=\"token operator\">:</span> <span class=\"token keyword\">typeof</span> search<span class=\"token punctuation\">.</span>redirectToPath <span class=\"token operator\">===</span> <span class=\"token string\">'string'</span> <span class=\"token operator\">?</span> search<span class=\"token punctuation\">.</span>redirectToPath <span class=\"token operator\">:</span> <span class=\"token string\">'/'</span><span class=\"token punctuation\">,</span>\n <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n component<span class=\"token operator\">:</span> RefreshSession<span class=\"token punctuation\">,</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n\n<span class=\"token keyword\">function</span> <span class=\"token function\">RefreshSession</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n <span class=\"token keyword\">const</span> <span class=\"token punctuation\">{</span> redirectToPath <span class=\"token punctuation\">}</span> <span class=\"token operator\">=</span> Route<span class=\"token punctuation\">.</span><span class=\"token function\">useSearch</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n <span class=\"token function\">useEffect</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n   Session<span class=\"token punctuation\">.</span><span class=\"token function\">attemptRefreshingSession</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">then</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span>refreshed<span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n     <span class=\"token comment\">// Rotated tokens are now in cookies; a full navigation re-runs the SSR session check.</span>\n     <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>refreshed<span class=\"token punctuation\">)</span> window<span class=\"token punctuation\">.</span>location<span class=\"token punctuation\">.</span>href <span class=\"token operator\">=</span> redirectToPath\n     <span class=\"token keyword\">else</span> <span class=\"token function\">redirectToAuth</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> redirectBack<span class=\"token operator\">:</span> <span class=\"token boolean\">false</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n   <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">[</span>redirectToPath<span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span>\n <span class=\"token keyword\">return</span> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>p</span><span class=\"token punctuation\">></span></span><span class=\"token plain-text\">Refreshing your session...</span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>p</span><span class=\"token punctuation\">></span></span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p><code class=\"language-text\">Session.attemptRefreshingSession()</code> calls <code class=\"language-text\">/api/auth/session/refresh</code>. SuperTokens rotates both tokens on every refresh, so a stolen refresh token stops working after one use. On success, a full navigation sends the new access token cookie with the next server request. On failure, the refresh token has expired or been revoked, and the user signs in again.</p>\n<p>The route uses <code class=\"language-text\">window.location.href</code> instead of router navigation on purpose. A full page load sends a fresh request with the new cookie. Both <code class=\"language-text\">beforeLoad</code> and the SSR render then see the refreshed session in one pass, with no stale router state.</p>\n<h2 id=\"step-7-protect-routes-with-beforeload\" style=\"position:relative;\"><a href=\"#step-7-protect-routes-with-beforeload\" aria-label=\"step 7 protect routes with beforeload permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Step 7: Protect Routes with beforeLoad</h2>\n<p>TanStack Router authentication for pages uses a pathless layout route whose <code class=\"language-text\">beforeLoad</code> calls the session server function. Every TanStack Start protected route nested under it inherits the check.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"91686015495746490000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied!\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`// src/routes/_authed.tsx\nimport { createFileRoute, redirect } from '@tanstack/react-router'\nimport { getUserSession } from '#/server/auth'\n\nexport const Route = createFileRoute('/_authed')({\n beforeLoad: async ({ location }) => {\n   const session = await getUserSession()\n   if (session.status === 'needs-refresh') {\n     throw redirect({ href: \\`/refresh-session?redirectToPath=\\${encodeURIComponent(location.href)}\\` })\n   }\n   if (session.status === 'unauthenticated') {\n     throw redirect({ href: \\`/auth?redirectToPath=\\${encodeURIComponent(location.href)}\\` })\n   }\n   return { userId: session.userId }\n },\n})`, `91686015495746490000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                <svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"ts\"><pre class=\"language-ts\"><code class=\"language-ts\"><span class=\"token comment\">// src/routes/_authed.tsx</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> createFileRoute<span class=\"token punctuation\">,</span> redirect <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@tanstack/react-router'</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> getUserSession <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'#/server/auth'</span>\n\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">const</span> Route <span class=\"token operator\">=</span> <span class=\"token function\">createFileRoute</span><span class=\"token punctuation\">(</span><span class=\"token string\">'/_authed'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n <span class=\"token function-variable function\">beforeLoad</span><span class=\"token operator\">:</span> <span class=\"token keyword\">async</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> location <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n   <span class=\"token keyword\">const</span> session <span class=\"token operator\">=</span> <span class=\"token keyword\">await</span> <span class=\"token function\">getUserSession</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n   <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>session<span class=\"token punctuation\">.</span>status <span class=\"token operator\">===</span> <span class=\"token string\">'needs-refresh'</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n     <span class=\"token keyword\">throw</span> <span class=\"token function\">redirect</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> href<span class=\"token operator\">:</span> <span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">/refresh-session?redirectToPath=</span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span><span class=\"token function\">encodeURIComponent</span><span class=\"token punctuation\">(</span>location<span class=\"token punctuation\">.</span>href<span class=\"token punctuation\">)</span><span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token template-punctuation string\">`</span></span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n   <span class=\"token punctuation\">}</span>\n   <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>session<span class=\"token punctuation\">.</span>status <span class=\"token operator\">===</span> <span class=\"token string\">'unauthenticated'</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n     <span class=\"token keyword\">throw</span> <span class=\"token function\">redirect</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> href<span class=\"token operator\">:</span> <span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">/auth?redirectToPath=</span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span><span class=\"token function\">encodeURIComponent</span><span class=\"token punctuation\">(</span>location<span class=\"token punctuation\">.</span>href<span class=\"token punctuation\">)</span><span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token template-punctuation string\">`</span></span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n   <span class=\"token punctuation\">}</span>\n   <span class=\"token keyword\">return</span> <span class=\"token punctuation\">{</span> userId<span class=\"token operator\">:</span> session<span class=\"token punctuation\">.</span>userId <span class=\"token punctuation\">}</span>\n <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span></code></pre></div>\n<p><code class=\"language-text\">beforeLoad</code> is isomorphic. On the first page load it runs on the server during SSR. During client-side navigation it runs in the browser. That is why <code class=\"language-text\">getUserSession</code> is a server function and not a plain function. In the browser, calling it sends an HTTP request to the server, where the cookies and <code class=\"language-text\">supertokens-node</code> live.</p>\n<p>The underscore prefix makes <code class=\"language-text\">_authed</code> pathless, so <code class=\"language-text\">src/routes/_authed/dashboard.tsx</code> serves <code class=\"language-text\">/dashboard</code>, not <code class=\"language-text\">/_authed/dashboard</code>. Whatever <code class=\"language-text\">beforeLoad</code> returns becomes route context, and child routes read it with <code class=\"language-text\">Route.useRouteContext()</code>.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"67259804459718730000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied!\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`// src/routes/_authed/dashboard.tsx\nimport { createFileRoute } from '@tanstack/react-router'\nimport { getMyNotes } from '#/server/notes'\n\nexport const Route = createFileRoute('/_authed/dashboard')({\n loader: () => getMyNotes(),\n component: Dashboard,\n})\n\nfunction Dashboard() {\n const { userId } = Route.useRouteContext()\n const { notes } = Route.useLoaderData()\n return (\n   <main>\n     <h1>Dashboard</h1>\n     <p>Signed in as {userId}</p>\n     <ul>{notes.map((n) => <li key={n}>{n}</li>)}</ul>\n   </main>\n )\n}`, `67259804459718730000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                <svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"tsx\"><pre class=\"language-tsx\"><code class=\"language-tsx\"><span class=\"token comment\">// src/routes/_authed/dashboard.tsx</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> createFileRoute <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'@tanstack/react-router'</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> getMyNotes <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'#/server/notes'</span>\n\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">const</span> Route <span class=\"token operator\">=</span> <span class=\"token function\">createFileRoute</span><span class=\"token punctuation\">(</span><span class=\"token string\">'/_authed/dashboard'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n <span class=\"token function-variable function\">loader</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token function\">getMyNotes</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n component<span class=\"token operator\">:</span> Dashboard<span class=\"token punctuation\">,</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n\n<span class=\"token keyword\">function</span> <span class=\"token function\">Dashboard</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n <span class=\"token keyword\">const</span> <span class=\"token punctuation\">{</span> userId <span class=\"token punctuation\">}</span> <span class=\"token operator\">=</span> Route<span class=\"token punctuation\">.</span><span class=\"token function\">useRouteContext</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n <span class=\"token keyword\">const</span> <span class=\"token punctuation\">{</span> notes <span class=\"token punctuation\">}</span> <span class=\"token operator\">=</span> Route<span class=\"token punctuation\">.</span><span class=\"token function\">useLoaderData</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n <span class=\"token keyword\">return</span> <span class=\"token punctuation\">(</span>\n   <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>main</span><span class=\"token punctuation\">></span></span><span class=\"token plain-text\">\n     </span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>h1</span><span class=\"token punctuation\">></span></span><span class=\"token plain-text\">Dashboard</span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>h1</span><span class=\"token punctuation\">></span></span><span class=\"token plain-text\">\n     </span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>p</span><span class=\"token punctuation\">></span></span><span class=\"token plain-text\">Signed in as </span><span class=\"token punctuation\">{</span>userId<span class=\"token punctuation\">}</span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>p</span><span class=\"token punctuation\">></span></span><span class=\"token plain-text\">\n     </span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>ul</span><span class=\"token punctuation\">></span></span><span class=\"token punctuation\">{</span>notes<span class=\"token punctuation\">.</span><span class=\"token function\">map</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span>n<span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>li</span> <span class=\"token attr-name\">key</span><span class=\"token script language-javascript\"><span class=\"token script-punctuation punctuation\">=</span><span class=\"token punctuation\">{</span>n<span class=\"token punctuation\">}</span></span><span class=\"token punctuation\">></span></span><span class=\"token punctuation\">{</span>n<span class=\"token punctuation\">}</span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>li</span><span class=\"token punctuation\">></span></span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">}</span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>ul</span><span class=\"token punctuation\">></span></span><span class=\"token plain-text\">\n   </span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>main</span><span class=\"token punctuation\">></span></span>\n <span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>The loader calls <code class=\"language-text\">getMyNotes</code>, which runs <code class=\"language-text\">authMiddleware</code> again on the server. That second check is deliberate. The layout keeps signed-out users off the page; the middleware keeps signed-out callers away from the data.</p>\n<h2 id=\"step-8-sign-out-and-show-session-state-in-the-ui\" style=\"position:relative;\"><a href=\"#step-8-sign-out-and-show-session-state-in-the-ui\" aria-label=\"step 8 sign out and show session state in the ui permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Step 8: Sign Out and Show Session State in the UI</h2>\n<p>SuperTokens exposes session state to TanStack Start components through <code class=\"language-text\">useSessionContext()</code>, and ends the session with <code class=\"language-text\">signOut()</code>. Both come from the session recipe.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"71458720437515624000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied!\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`// src/components/SessionStatus.tsx\nimport { signOut, useSessionContext } from 'supertokens-auth-react/recipe/session'\n\nexport function SessionStatus() {\n const session = useSessionContext()\n if (session.loading) return null\n if (!session.doesSessionExist) return <a href=&quot;/auth&quot;>Sign in</a>\n return (\n   <span>\n     {session.userId}{' '}\n     <button onClick={async () => { await signOut(); window.location.href = '/' }}>Sign out</button>\n   </span>\n )\n}`, `71458720437515624000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                <svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"tsx\"><pre class=\"language-tsx\"><code class=\"language-tsx\"><span class=\"token comment\">// src/components/SessionStatus.tsx</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> signOut<span class=\"token punctuation\">,</span> useSessionContext <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'supertokens-auth-react/recipe/session'</span>\n\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">function</span> <span class=\"token function\">SessionStatus</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n <span class=\"token keyword\">const</span> session <span class=\"token operator\">=</span> <span class=\"token function\">useSessionContext</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>session<span class=\"token punctuation\">.</span>loading<span class=\"token punctuation\">)</span> <span class=\"token keyword\">return</span> <span class=\"token keyword\">null</span>\n <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token operator\">!</span>session<span class=\"token punctuation\">.</span>doesSessionExist<span class=\"token punctuation\">)</span> <span class=\"token keyword\">return</span> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>a</span> <span class=\"token attr-name\">href</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>/auth<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span><span class=\"token plain-text\">Sign in</span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>a</span><span class=\"token punctuation\">></span></span>\n <span class=\"token keyword\">return</span> <span class=\"token punctuation\">(</span>\n   <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>span</span><span class=\"token punctuation\">></span></span><span class=\"token plain-text\">\n     </span><span class=\"token punctuation\">{</span>session<span class=\"token punctuation\">.</span>userId<span class=\"token punctuation\">}</span><span class=\"token punctuation\">{</span><span class=\"token string\">' '</span><span class=\"token punctuation\">}</span><span class=\"token plain-text\">\n     </span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>button</span> <span class=\"token attr-name\">onClick</span><span class=\"token script language-javascript\"><span class=\"token script-punctuation punctuation\">=</span><span class=\"token punctuation\">{</span><span class=\"token keyword\">async</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span> <span class=\"token keyword\">await</span> <span class=\"token function\">signOut</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> window<span class=\"token punctuation\">.</span>location<span class=\"token punctuation\">.</span>href <span class=\"token operator\">=</span> <span class=\"token string\">'/'</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">}</span></span><span class=\"token punctuation\">></span></span><span class=\"token plain-text\">Sign out</span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>button</span><span class=\"token punctuation\">></span></span><span class=\"token plain-text\">\n   </span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>span</span><span class=\"token punctuation\">></span></span>\n <span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p><code class=\"language-text\">signOut()</code> revokes the session on the core and clears the cookies. The full-page navigation that follows makes the next server request arrive without a session, so protected routes redirect as expected.</p>\n<h2 id=\"test-it\" style=\"position:relative;\"><a href=\"#test-it\" aria-label=\"test it permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Test It</h2>\n<p>A working SuperTokens setup in TanStack Start passes three checks. Sign-up lands on the protected page. A direct <code class=\"language-text\">curl</code> to a protected server function returns 401. An expired access token triggers a silent refresh.</p>\n<p><strong>Check 1: sign up and land on the protected page.</strong> Run <code class=\"language-text\">npm run dev</code>, open <code class=\"language-text\">http://localhost:3000/dashboard</code>, and confirm the redirect. Signed out, the server answers before any page renders:</p>\n<ul>\n<li>GET /dashboard -> HTTP 307 Location: /auth?redirectToPath=%2Fdashboard</li>\n<li>The 307 comes from <code class=\"language-text\">beforeLoad</code> during SSR, so the server never sends the protected page’s HTML.</li>\n<li>Sign up on that page, and SuperTokens returns you to <code class=\"language-text\">/dashboard</code> showing your user ID.</li>\n</ul>\n<p><strong>Check 2: call a protected server function directly.</strong> Open DevTools, load <code class=\"language-text\">/dashboard</code>, and copy a request URL from the Network tab that starts with <code class=\"language-text\">/_serverFn/</code>. Then call it from a terminal with no cookies:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"15765109034613811000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied!\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`curl -i -H &quot;Origin: http://localhost:3000&quot; &quot;http://localhost:3000/_serverFn/<id>&quot;\nHTTP/1.1 401`, `15765109034613811000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                <svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token function\">curl</span> -i -H <span class=\"token string\">\"Origin: http://localhost:3000\"</span> <span class=\"token string\">\"http://localhost:3000/_serverFn/&lt;id>\"</span>\nHTTP/1.1 <span class=\"token number\">401</span></code></pre></div>\n<p>The <code class=\"language-text\">Origin</code> header matters for this test. TanStack Start <code class=\"language-text\">1.168</code> applies a CSRF middleware to every server function by default. Without an <code class=\"language-text\">Origin</code>, <code class=\"language-text\">Referer</code> or <code class=\"language-text\">Sec-Fetch-Site</code> header, it returns <code class=\"language-text\">403 Forbidden</code> before your code runs. That 403 proves the CSRF layer works, not your auth. Adding a same-origin <code class=\"language-text\">Origin</code> header gets past CSRF, which takes one flag in <code class=\"language-text\">curl</code>.</p>\n<p>The 401 that follows comes from <code class=\"language-text\">authMiddleware</code>. That is the real test: a caller who can forge any header still gets nothing without a session. Add this check to CI. A broken route guard shows up the moment someone opens the page. A missing middleware stays invisible until someone calls the endpoint directly.</p>\n<p><strong>Check 3: delete the access token and confirm silent refresh.</strong> While signed in, open DevTools, go to Application, then Cookies, and delete <code class=\"language-text\">sAccessToken</code>. Reload <code class=\"language-text\">/dashboard</code>. The browser briefly visits <code class=\"language-text\">/refresh-session</code>, then lands back on <code class=\"language-text\">/dashboard</code> still signed in. In the Network tab, <code class=\"language-text\">POST /api/auth/session/refresh</code> returns 200 and sets new cookies.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 539px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/6ee38bf7b73ad4de8d8c6a5a2f3d8b36/10f9a/Sign-In-UI.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 125.31645569620254%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAZCAIAAAC+dZmEAAAACXBIWXMAAAsTAAALEwEAmpwYAAACG0lEQVQ4y51US4/TMBDeX8EFcQDEsqAFgVQ4IC5w4w9w4cTfQhz2jJA49C/w6HbT03KpVPWRR9EmTdzEtePH2EaJaTZt3YL4NEo8nvkynkd8ZIzRWqsdODcbk6lxZIxp6xZSSkopAHDOt4xaawBwk+2CENLtdj3P+3l5WZa0/d2/kCmljLGiKPI8pyXDzDD5b2SlFEIoSZI0TRHKUlREuUH0ILkxbCVvt9cWvZdcGZVu6mmrbRntOm+TrQKSpVcR55IxNpvNptOp7/uTyWQ8HjPGpJSc8z+eu5GVUoSVAGD7ZEEIwRhLKaGGDe6ILIQYDodKVR7zNYIgiKKomRnHsZucueDWzGvIGvWcqEMFa/e5WTPG9CYcZKswxggh7RY1TocmzCpJkvi+b20Hfom9Oed5jhCK43ixWHDOxRpSClm9Xa2ySlmWCCFKqVIKAJo8hdSEa8qrJ6gqiIO8XC5Ho1Ecx0prqPnV+QFWJYQZzBGECLisRtV9bFwUKMuKotiocFVk3Z5wR2SM8cwPgjAKoyiK5m0JawnCiBBqr4prMufcGPPh49mNW/dOHj8/Pu3cOXny8vWbt+/ed168uvvg6fFp5/6jZzdvP/z0+UvjvxF5Pv/19XuvdzE4vxj02+INzvter+99+9GP42Q78u4ddgCOPgOAEKKsR7rAOEmzNENxmsVpdrVI8WrFhWD1tNsuXJP/G26ybsv+hH4DQwyanQxN7loAAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Sign-In-UI\"\n        title=\"Sign-In-UI\"\n        src=\"/static/6ee38bf7b73ad4de8d8c6a5a2f3d8b36/10f9a/Sign-In-UI.png\"\n        srcset=\"/static/6ee38bf7b73ad4de8d8c6a5a2f3d8b36/c26ae/Sign-In-UI.png 158w,\n/static/6ee38bf7b73ad4de8d8c6a5a2f3d8b36/6bdcf/Sign-In-UI.png 315w,\n/static/6ee38bf7b73ad4de8d8c6a5a2f3d8b36/10f9a/Sign-In-UI.png 539w\"\n        sizes=\"(max-width: 539px) 100vw, 539px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 630px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/0802727d78ed4312f28ca897187e5d6d/3c051/Dashboard.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 48.734177215189874%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAIAAAA7N+mxAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAn0lEQVQoz6XRPQ+DIBAGYP//P5PEkIaNi9rYAIETF4arSsCELh2qNPHZbnhzXw0ADMPwLMZxBAApZd/3UkqlVL7U5Bsaa23btl3XCSE450SUc04p/RVGRMaYEOJRhBBijPu+b9tGRO/iNLyuKyJ675VSWutpml7Fp9RaG2OudiYi59yyLIg4z7P33lobQqiPfbZe+lK5dvql3vnWq+6ED/p5RzPoGPc3AAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Dashboard\"\n        title=\"Dashboard\"\n        src=\"/static/0802727d78ed4312f28ca897187e5d6d/f058b/Dashboard.png\"\n        srcset=\"/static/0802727d78ed4312f28ca897187e5d6d/c26ae/Dashboard.png 158w,\n/static/0802727d78ed4312f28ca897187e5d6d/6bdcf/Dashboard.png 315w,\n/static/0802727d78ed4312f28ca897187e5d6d/f058b/Dashboard.png 630w,\n/static/0802727d78ed4312f28ca897187e5d6d/3c051/Dashboard.png 760w\"\n        sizes=\"(max-width: 630px) 100vw, 630px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<h2 id=\"supertokens-vs-clerk-vs-workos-vs-better-auth-for-tanstack-start\" style=\"position:relative;\"><a href=\"#supertokens-vs-clerk-vs-workos-vs-better-auth-for-tanstack-start\" aria-label=\"supertokens vs clerk vs workos vs better auth for tanstack start permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>SuperTokens vs Clerk vs WorkOS vs Better Auth for TanStack Start</h2>\n<p>SuperTokens, Clerk, WorkOS and Better Auth all work with TanStack Start. They differ most on hosting, pricing model and who owns the user data. The table compares them on TanStack-specific criteria, using each vendor’s published pricing as of October 2026.</p>\n<table>\n<thead>\n<tr>\n<th>Criteria</th>\n<th>SuperTokens</th>\n<th>Clerk</th>\n<th>WorkOS (AuthKit)</th>\n<th>Better Auth</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>Official TanStack Start package</td>\n<td>No; framework-agnostic <code class=\"language-text\">supertokens-node/custom</code> helpers</td>\n<td>Yes, <code class=\"language-text\">@clerk/tanstack-react-start</code></td>\n<td>Yes, <code class=\"language-text\">@workos/authkit-tanstack-react-start</code> (pre-1.0)</td>\n<td>Yes, <code class=\"language-text\">better-auth/tanstack-start</code></td>\n</tr>\n<tr>\n<td>Self-hosting</td>\n<td>Yes, open-source core</td>\n<td>No</td>\n<td>No</td>\n<td>Yes, library in your app</td>\n</tr>\n<tr>\n<td>Pricing model</td>\n<td>Free self-hosted; managed free under 5,000 MAU, then $0.02/MAU</td>\n<td>Free to 50,000 MRU; Pro $25/mo plus $0.02/MRU over 50,000</td>\n<td>Free to 1M MAU; $125 per SSO connection/mo</td>\n<td>Free (MIT)</td>\n</tr>\n<tr>\n<td>Prebuilt UI</td>\n<td>Yes, React</td>\n<td>Yes, React</td>\n<td>Yes, hosted</td>\n<td>No official UI; community libraries</td>\n</tr>\n<tr>\n<td>SSR session helper</td>\n<td><code class=\"language-text\">getSessionForSSR()</code></td>\n<td><code class=\"language-text\">auth()</code></td>\n<td><code class=\"language-text\">getAuth()</code></td>\n<td><code class=\"language-text\">auth.api.getSession()</code></td>\n</tr>\n<tr>\n<td>MFA</td>\n<td>Add-on, 100/mo minimum)</td>\n<td>Pro and above</td>\n<td>Supported</td>\n<td><code class=\"language-text\">two-factor</code> plugin</td>\n</tr>\n<tr>\n<td>Enterprise SSO (SAML)</td>\n<td>Yes</td>\n<td>1 connection on Pro, then $75/mo each</td>\n<td>Yes, $125 per connection/mo</td>\n<td><code class=\"language-text\">@better-auth/sso</code> plugin</td>\n</tr>\n<tr>\n<td>Data ownership</td>\n<td>Full when self-hosted</td>\n<td>Vendor-hosted</td>\n<td>Vendor-hosted</td>\n<td>Full, your database</td>\n</tr>\n</tbody>\n</table>\n<p>Choose Clerk or WorkOS for the fastest hosted setup, with WorkOS leaning toward B2B SSO. Choose Better Auth if you want a library and are happy to build your own UI. Choose SuperTokens if you want self-hosting, a prebuilt UI and rotating refresh tokens together. See the full [SuperTokens vs Better Auth]([LINK: existing SuperTokens vs Better Auth comparison page]) comparison and the <a href=\"https://supertokens.com/blog/supertokens-vs-clerk\" target=\"_blank\" rel=\"nofollow\">SuperTokens vs Clerk</a> breakdown for more detail.</p>\n<h2 id=\"production-checklist\" style=\"position:relative;\"><a href=\"#production-checklist\" aria-label=\"production checklist permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Production Checklist</h2>\n<p>A TanStack Start app using SuperTokens needs these changes before it handles real users. Most mirror TanStack’s own <a href=\"https://tanstack.com/start/latest/docs/framework/react/guide/authentication-overview\" target=\"_blank\" rel=\"nofollow\">production auth checklist</a>.</p>\n<ul>\n<li>Serve the app and API over HTTPS only.</li>\n<li>Set <code class=\"language-text\">apiDomain</code> and <code class=\"language-text\">websiteDomain</code> to your production URLs.</li>\n<li>Set <code class=\"language-text\">cookieDomain</code> in <code class=\"language-text\">Session.init</code> if the API and site sit on different subdomains.</li>\n<li>Configure CORS with credentials if the API runs on a separate origin.</li>\n<li>Rate limit sign-in, sign-up and password-reset endpoints.</li>\n<li>Add <code class=\"language-text\">authMiddleware</code> to every server function that reads or writes private data.</li>\n<li>Add <code class=\"language-text\">.validator()</code> to every server function that accepts input.</li>\n<li>Keep TanStack Start’s default CSRF protection on for server functions.</li>\n<li>Move off <code class=\"language-text\">try.supertokens.com</code> to the managed service or your own core.</li>\n<li>Set <code class=\"language-text\">SUPERTOKENS_API_KEY</code> and keep OAuth client secrets in server-only environment variables.</li>\n</ul>\n<h2 id=\"faq\" style=\"position:relative;\"><a href=\"#faq\" aria-label=\"faq permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>FAQ</h2>\n<h3 id=\"does-tanstack-start-have-built-in-authentication\" style=\"position:relative;\"><a href=\"#does-tanstack-start-have-built-in-authentication\" aria-label=\"does tanstack start have built in authentication permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><strong>Does TanStack Start Have Built-In Authentication?</strong></h3>\n<p>No. TanStack Start provides the building blocks: server functions, middleware, <code class=\"language-text\">beforeLoad</code> and cookie sessions. It does not provide login, sign-up or token management. You add those with a library or provider such as SuperTokens. It supplies the auth API, a prebuilt UI and session handling with rotating refresh tokens.</p>\n<h3 id=\"does-supertokens-work-with-tanstack-start\" style=\"position:relative;\"><a href=\"#does-supertokens-work-with-tanstack-start\" aria-label=\"does supertokens work with tanstack start permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><strong>Does SuperTokens Work with TanStack Start?</strong></h3>\n<p>Yes. SuperTokens’ Node SDK includes framework-agnostic helpers in <code class=\"language-text\">supertokens-node/custom</code> that work with Web <code class=\"language-text\">Request</code> and <code class=\"language-text\">Response</code> objects. Its auth API mounts on a TanStack Start server route, and its session check runs inside server functions. The React SDK, <code class=\"language-text\">supertokens-auth-react</code>, provides the login UI.</p>\n<h3 id=\"is-beforeload-enough-to-protect-a-route-in-tanstack-start\" style=\"position:relative;\"><a href=\"#is-beforeload-enough-to-protect-a-route-in-tanstack-start\" aria-label=\"is beforeload enough to protect a route in tanstack start permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><strong>Is beforeLoad Enough to Protect a Route in TanStack Start?</strong></h3>\n<p>No. <code class=\"language-text\">beforeLoad</code> stops users from seeing a page, but server functions are public HTTP endpoints that anyone can call directly. Check the session inside every server function that reads or writes private data. Shared middleware, like <code class=\"language-text\">authMiddleware</code> in this guide, keeps that check in one place.</p>\n<h3 id=\"how-do-i-get-the-current-user-in-a-tanstack-start-loader\" style=\"position:relative;\"><a href=\"#how-do-i-get-the-current-user-in-a-tanstack-start-loader\" aria-label=\"how do i get the current user in a tanstack start loader permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><strong>How Do I Get the Current User in a TanStack Start Loader?</strong></h3>\n<p>Call a server function from <code class=\"language-text\">beforeLoad</code> that reads the request with <code class=\"language-text\">getRequest()</code> and verifies it with SuperTokens’ <code class=\"language-text\">getSessionForSSR()</code>. Return the user ID from <code class=\"language-text\">beforeLoad</code>. TanStack Router adds it to route context, and child routes read it with <code class=\"language-text\">Route.useRouteContext()</code>. Loaders that fetch private data should call protected server functions, which check the session again.</p>\n<h3 id=\"can-i-self-host-authentication-for-tanstack-start\" style=\"position:relative;\"><a href=\"#can-i-self-host-authentication-for-tanstack-start\" aria-label=\"can i self host authentication for tanstack start permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><strong>Can I Self-Host Authentication for TanStack Start?</strong></h3>\n<p>Yes. SuperTokens is open source, so you can run the SuperTokens core with Docker on your own infrastructure or use the managed service. Self-hosting keeps user data in your own database and removes per-user fees for core auth. Add-ons such as MFA are still billed per user.</p>\n<h3 id=\"how-does-supertokens-handle-token-refresh-in-tanstack-start-ssr\" style=\"position:relative;\"><a href=\"#how-does-supertokens-handle-token-refresh-in-tanstack-start-ssr\" aria-label=\"how does supertokens handle token refresh in tanstack start ssr permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><strong>How Does SuperTokens Handle Token Refresh in TanStack Start SSR?</strong></h3>\n<p>Access tokens are short-lived, and refresh tokens rotate on every use. When a server-side check finds an expired access token, the app redirects to a client-only refresh route. That route calls <code class=\"language-text\">Session.attemptRefreshingSession()</code>, which sets new cookies, then returns the user to the original page.</p>\n<h3 id=\"supertokens-vs-clerk-for-tanstack-start-which-should-i-use\" style=\"position:relative;\"><a href=\"#supertokens-vs-clerk-for-tanstack-start-which-should-i-use\" aria-label=\"supertokens vs clerk for tanstack start which should i use permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><strong>SuperTokens vs Clerk for TanStack Start: Which Should I Use?</strong></h3>\n<p>Clerk is a hosted service with an official TanStack Start SDK and per-user pricing above its free tier. SuperTokens is open source and self-hostable, with no per-user fee for core auth when self-hosted. Choose Clerk for the fastest hosted setup. Choose SuperTokens for data ownership and cost control at scale.</p>\n<h2 id=\"conclusion\" style=\"position:relative;\"><a href=\"#conclusion\" aria-label=\"conclusion permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Conclusion</h2>\n<p>That completes SuperTokens on TanStack Start. The app has an auth API on a server route, sessions verified in server functions, protected routes, and silent token refresh during SSR. For the session details, read the <a href=\"https://supertokens.com/docs/additional-verification/session-verification/protect-api-routes\" target=\"_blank\" rel=\"nofollow\">SuperTokens session verification docs</a>. Questions about TanStack Start specifically? Ask in the <a href=\"https://discord.com/invite/supertokens-com-603466164219281420\" target=\"_blank\" rel=\"nofollow\">SuperTokens Discord</a>.</p>","frontmatter":{"date":"October 11, 2026","updated":null,"title":"How to Add Authentication to TanStack Start with SuperTokens (2026 Guide)","cover":"how-to-add-auth-to-tanstack-start-with-supertokens.png","author":"Mostafa Ibrahim","description":"Learn how to add authentication to TanStack Start with SuperTokens, including sessions and protected routes."},"fields":{"slug":"/how-to-add-auth-to-tanstack-start-with-supertokens/"}},"site":{"siteMetadata":{"title":"SuperTokens Blog"}}},"pageContext":{"id":"54aa7fd3-005b-577c-8496-c795a46ba22d","fields__slug":"/how-to-add-auth-to-tanstack-start-with-supertokens/","__params":{"fields__slug":"how-to-add-auth-to-tanstack-start-with-supertokens"}}},
    "staticQueryHashes": []}