- Stack Auth account and project created at https://app.stack-auth.com
- Google Cloud Console project for OAuth
Add these to your .env.local file:
NEXT_PUBLIC_STACK_PROJECT_ID=<your-project-id>
NEXT_PUBLIC_STACK_PUBLISHABLE_CLIENT_KEY=<your-publishable-client-key>
STACK_SECRET_SERVER_KEY=<your-secret-server-key>- Go to your Stack Auth dashboard
- Navigate to Authentication → OAuth Providers
- Enable Google provider
- Add your Google OAuth credentials:
- Client ID (from Google Cloud Console)
- Client Secret (from Google Cloud Console)
- In Stack Auth dashboard, go to Settings → URLs
- Add the following redirect URLs:
http://localhost:3001/handler/oauth-callback(for local development)https://your-production-domain.com/handler/oauth-callback(for production)- Any preview/staging URLs you need
- Go to Configuration → Domains
- Enable "Allow all localhost callbacks for development"
- Add your production domain(s)
- Go to Google Cloud Console
- Navigate to APIs & Services → Credentials
- Click Create Credentials → OAuth client ID
- Choose Web application
Add the following to your OAuth client:
Authorized JavaScript origins:
http://localhost:3001https://your-production-domain.com
Authorized redirect URIs:
http://localhost:3001/handler/oauth-callbackhttps://your-production-domain.com/handler/oauth-callback
Copy the Client ID and Client Secret to use in Stack Auth dashboard.
If you get a 404 on /handler/oauth-callback, ensure:
- The
[...stack]catch-all route exists atsrc/app/handler/[...stack]/page.tsx - Stack Auth is properly initialized in your app
This happens when a new user signs up. The fix is already implemented in src/auth/stack-auth.ts which:
- Checks if
serverMetadataexists - Creates a Freestyle identity if it doesn't
- Refetches the user to get the updated metadata
If you experience redirect loops:
- Check that your redirect URLs match exactly in both Stack Auth and Google Console
- Ensure cookies are not being blocked
- Verify environment variables are set correctly
- Start your development server:
npm run dev - Navigate to
http://localhost:3001 - Click through the onboarding flow
- When redirected to sign in, choose "Sign in with Google"
- Complete Google authentication
- You should be redirected back to your app
Before deploying to production:
- Update all redirect URLs to use your production domain
- Set environment variables in your hosting provider (Vercel, etc.)
- Ensure your production domain is added to both Stack Auth and Google Console
- Test the OAuth flow in production