Fanvue OAuth Quick Start (Next.js App Router)
In a few minutes you’ll have a working Next.js app where users can “Login with Fanvue” using OAuth. We’ll start from the official Fanvue App Starter and run locally with pnpm.
What you’ll build
- Secure OAuth login with Fanvue
- Session-backed current-user page after sign-in
- Production-ready settings you can deploy anywhere
Prerequisites
- Node.js 18 or later
- pnpm installed
- Access to the Fanvue Developer area to create an app (Client ID/Secret)
1) Bootstrap from the Fanvue App Starter
Use the template repository: Fanvue App Starter
Pick one of these ways to start from the template:
-
Use this template on GitHub
- Open: Fanvue App Starter
- Click “Use this template” → “Create a new repository”
- Clone your new repository locally
-
Scaffold via degit
-
Clone and re-init
2) Set up local HTTPS proxy
OAuth requires HTTPS redirect URIs, even in local development. We’ll use mkcert and local-ssl-proxy to enable HTTPS locally.
Install mkcert
Generate SSL certificates
Replace my-fanvue-app with your app name:
This creates two files: my-fanvue-app.dev.pem and my-fanvue-app.dev-key.pem
Update hosts file
You’ll run the proxy later after configuring environment variables. The proxy will forward HTTPS traffic from port 3001 to your Next.js dev server on port 3000.
3) Create your Fanvue OAuth app
In the Fanvue Developer area, create a new App to obtain credentials:
- Client ID and Client Secret
- Redirect URI
- Development:
https://my-fanvue-app.dev:3001/api/oauth/callback(replacemy-fanvue-appwith your app name) - Production:
https://YOUR_DOMAIN/api/oauth/callback
- Development:
- Scopes
- For the starter, use:
read:self - The starter automatically includes required system scopes (
openid,offline_access,offline) in addition to what you set inOAUTH_SCOPES
- For the starter, use:
4) Configure environment variables
Create .env.local in the project root and set the following (replace my-fanvue-app with your app name):
- Never commit
.env.localto version control - EnsureOAUTH_SCOPESexactly matches what you configured in the Fanvue developer UI
5) Install and run locally
First, install dependencies and start the Next.js dev server:
In a separate terminal, start the local SSL proxy (replace my-fanvue-app with your app name):
Visit https://my-fanvue-app.dev:3001 (replace with your app name) and click “Login with Fanvue”. After authorizing, you’ll be redirected back and see your current user JSON rendered by the app.
6) How it works (at a glance)
- Next.js App Router with an OAuth callback at
/api/oauth/callback - Session cookie (
SESSION_COOKIE_NAME) signed withSESSION_SECRET - Server-side calls to
API_BASE_URLusing the authenticated session - Local SSL proxy forwards HTTPS requests to the Next.js dev server
7) Production deployment
- Set the same environment variables in your hosting provider
- In the Fanvue developer UI, add the production Redirect URI:
https://YOUR_DOMAIN/api/oauth/callback - Build and run
Recommended services
- Vercel for hosting
- Supabase if you also need a database
Troubleshooting
- Invalid redirect URI: The value in the Fanvue developer UI must exactly match
OAUTH_REDIRECT_URI(including the HTTPS protocol and port 3001) - Scope mismatch:
OAUTH_SCOPESmust exactly match the scopes selected in the Fanvue developer UI (starter usesread:self) - Missing session secret: Set
SESSION_SECRETto a random string with at least 16 characters - Certificate errors in browser: Run
mkcert -installto install the local CA certificate - Cannot reach app at .dev domain: Verify the hosts file entry with
cat /etc/hosts | grep my-fanvue-app.dev - Proxy connection refused: Ensure the Next.js dev server is running on port 3000 before starting the proxy
- Port already in use: If port 3000 or 3001 is in use, stop other services or use different ports (update proxy and redirect URI accordingly)
- Blank page after login: Open browser DevTools and your terminal; ensure all env vars are set, the proxy is running, and the callback route is reachable
Where to customize in the starter
- UI and login experience:
src/app/page.tsx - OAuth callback and session handling:
src/app/api/oauth/callback/route.ts - API calls after login: server routes under
src/app/api/**using the session’s access token
Next steps
- Add additional scopes in both the Fanvue developer UI and
OAUTH_SCOPES - Call other endpoints (e.g., chats, followers) from your server routes
- Protect pages with middleware that checks the session
Resources:
- Fanvue App Starter: github.com/fanvue/fanvue-app-starter