A minimal TanStack Start app demonstrating Discord OAuth2 login with @discordkit/oauth and authenticated API calls with @discordkit/client. The TanStack Start counterpart to with-nextjs and with-astro — same flow, idiomatic TanStack Start shape.
- Login — redirect to Discord's consent screen (Authorization Code + PKCE).
- Callback — verify CSRF state, exchange the code, store tokens in a session cookie.
- Profile + guilds — fetched server-side in the
/dashboardroute loader (/oauth2/@meandgetCurrentUserGuilds), rendered directly. - Refresh / Logout — server routes hit from the dashboard's buttons.
🏗️ How it's wired #
src/lib/discord.ts— the OAuth2 client + login/callback handler (@discordkit/oauth). The@sensitiveclient secret never reaches the client.src/lib/session.ts— a signed (jose) httpOnly session cookie. Uses TanStack Start'sgetCookie/setCookie(ambient request context).src/routes/api.auth.*.ts— server routes for login/callback/refresh/logout (createFileRoute(...)({ server: { handlers } }), Web-standard Request→Response).src/routes/index.tsx— public landing (login button / link to dashboard).src/routes/dashboard.tsx— protected: abeforeLoadguard redirects unauthenticated visitors to/, and theloaderfetches profile + guilds server-side.src/server.ts— custom server entry that starts MSW for E2E (whenDISCORD_E2E_MOCKis set); otherwise behaves like the default entry.
Runs under Vite+ (vp dev/vp build) with the nitro/vite plugin providing the server runtime. The shared E2E flow lives in @discordkit/e2e and is reused verbatim across examples.
🔑 Environment variables #
Copy .env.schema to .env and fill these in. .env is gitignored; .env.schema is committed and declares the shape, which Varlock validates at build and start.
| Variable | Required | Where to get it |
|---|---|---|
DISCORD_CLIENT_ID |
yes | Developer Portal → your app → OAuth2 → General. Public; it appears in the authorize URL. |
DISCORD_CLIENT_SECRET |
yes | Same page, Reset Secret. Server-only — never send it to the browser. |
DISCORD_REDIRECT_URI |
yes | You choose it, then register the exact URL under OAuth2 → Redirects. Locally: http://localhost:3000/api/auth/callback |
SESSION_SECRET |
yes | Generate one: openssl rand -base64 32. Signs the session cookie. |
📦 Setup #
-
Create a Discord application at the developer dashboard.
-
Copy the Client ID + Client Secret (OAuth2 → General).
-
Add
http://localhost:3000/api/auth/callbackunder OAuth2 → Redirects. -
Copy
.env.schemato a local.env(gitignored — never commit secrets):DISCORD_CLIENT_ID=your_client_id DISCORD_CLIENT_SECRET=your_client_secret DISCORD_REDIRECT_URI=http://localhost:3000/api/auth/callback SESSION_SECRET=$(openssl rand -base64 32) -
vp installthenvp run dev, and open http://localhost:3000.
A note on production #
This keeps session handling intentionally small to focus on the OAuth2 flow. For a production app, consider a dedicated auth library — @discordkit/oauth provides the framework-agnostic primitives such libraries build on.