Get Started
This quickstart demonstrates how to add Auth0 authentication to a TanStack Start React application. You’ll build a server-rendered app with login, logout, protected routes, and user profile information using the Auth0 TanStack Start React SDK.1
Create a new project
Scaffold a new TanStack Start project for this Quickstart:Open the project:
If this is the first time you’ve run
@tanstack/cli on your machine, npm asks to confirm installing it: answer y to continue.2
Install the Auth0 TanStack Start SDK
3
Add Tailwind CSS
Install Tailwind CSS and its Vite plugin:Update Replace the contents of
vite.config.ts to add the Tailwind plugin and lock the dev server to port 3000:vite.config.ts
Vite fails immediately when you use
strictPort: true instead of silently switching ports. Your Auth0 application’s callback URL is locked to port 3000, and since the browser gets redirected to a port nothing is listening on, a silent port change leaves you stuck on a blank page after approving the login screen.src/styles.css with:src/styles.css
4
Create project files
Create the additional files you need for Auth0 integration:
5
Setup your Auth0 App
Next up, you need to create a new app on your Auth0 tenant and add the environment variables to your project.You have three options to set up your Auth0 app: use the Quick Setup tool (recommended), run a CLI command, or configure manually via the Dashboard:
- Quick Setup (recommended)
- CLI
- Dashboard
Create an Auth0 app and copy the pre-filled
.env file with the right configuration values.6
Create the Auth0 server instance
Add the Auth0 server instance to
src/auth.server.ts. The Auth0 server instance reads the configuration from the environment variables you just set:src/auth.server.ts
7
Register the Auth0 middleware
Add the following to
src/start.ts to register the Auth0 request middleware:src/start.ts
Import
auth0Middleware from the /server/middleware subpath, not the /server barrel, since start.ts is also compiled into the client bundle.This middleware automatically mounts the following authentication routes:
/auth/login- Login route/auth/callback- Callback route/auth/logout- Logout route/auth/profile- User profile route/auth/backchannel-logout- Backchannel logout route
8
Wire Auth0 into the router
Update
src/router.tsx so the router context carries Auth0’s resolved auth state. TanStack Start requires this file to export a function named getRouter:src/router.tsx
9
Update the root route
Update
src/routes/__root.tsx to resolve auth state before render and wrap the app in Auth0Provider:src/routes/__root.tsx
This replaces
createRootRoute with createRootRouteWithContext<RouterContext>() so type the route tree as context.auth0.10
Create Login, Logout and Profile Components
Add the component code to the files created in Step 4:Then replace the placeholder content of
src/routes/index.tsx (the Welcome to TanStack Start page from the scaffold) with:src/routes/index.tsx
useLogin and useLogout perform a full browser navigation rather than a client-side router transition because /auth/* is handled by the server middleware and the session cookie requires a page reload to take effect.11
Protect a route
Add a protected
/dashboard route to src/routes/dashboard.tsx using the requireAuth guard. It redirects unauthenticated users to /auth/login on the server before any HTML is sent to the browser:src/routes/dashboard.tsx
12
Run your app
Your app is available at http://localhost:3000.
CheckpointYou should now have a fully functional Auth0 login page running on your localhost