Prerequisites
Before deploying, ensure you have:- A Vercel account
- A MongoDB Atlas cluster (or publicly accessible MongoDB instance)
- An OpenAI API key (or compatible OpenAI gateway)
- SecondMe OAuth application (
CLIENT_ID/CLIENT_SECRET) - GitHub OAuth App (
GITHUB_ID/GITHUB_SECRET) - Google OAuth application (
GOOGLE_CLIENT_ID/GOOGLE_CLIENT_SECRET)
MongoDB Atlas Setup
Configure network access
In
Network Access, allow connections from Vercel:- For initial setup, you can use
0.0.0.0/0to allow all IPs - Once stable, restrict access to specific IP ranges for better security
Environment Variables Configuration
In your Vercel project settings, configure the following environment variables. Select the appropriate environments (Production/Preview/Development) as needed:Optional variables:
OPENAI_BASE_URLdefaults to the official OpenAI API endpoint if omittedOPENAI_MODELdefaults togpt-4o-miniif omittedNEXTAUTH_SECRETshould be at least 32 characters long (use a random string generator)
OAuth Callback Configuration
Configure the callback URLs in each OAuth provider console:SecondMe
GitHub
Deployment Steps
Import project in Vercel
- Go to Vercel Dashboard
- Click
Add New Project - Import your repository
Configure framework
Vercel will usually auto-detect Next.js. If not, select:
- Framework Preset: Next.js
Add environment variables
In the project configuration, add all the environment variables listed above.
Post-Deployment Verification
After deployment, verify these essential features:- Homepage loads normally and displays the question list
- SecondMe login completes successfully and returns to the site
- Question detail page shows AI streaming replies when comments are submitted
- Interactions persist - likes, dislikes, and favorites save and persist after refresh
- User profile page displays statistics and activity feed correctly
Troubleshooting
MongoDB Connection Error
Error: Please define the MONGODB_URI...
Error: Please define the MONGODB_URI...
This error indicates:
MONGODB_URIis missing or incorrectly formatted- MongoDB Atlas user permissions are incorrect
- IP whitelist doesn’t include Vercel’s IPs
- Verify the environment variable is set in Vercel
- Check MongoDB Atlas network access settings
- Ensure database user has proper permissions
OAuth Login Issues
Login callback fails (invalid_state / doesn't return)
Login callback fails (invalid_state / doesn't return)
Common causes:
NEXTAUTH_URLdoesn’t match the actual domain- OAuth callback URL not configured for current domain
- Old cookies causing state mismatch when switching domains
- Ensure
NEXTAUTH_URLexactly matches your deployment URL - Verify callback URLs in OAuth provider consoles
- Clear browser cookies and try again
OpenAI API Errors
AI responses fail or error
AI responses fail or error
Possible issues:
OPENAI_API_KEYis invalid or has insufficient credits- Custom
OPENAI_BASE_URLgateway is unavailable OPENAI_MODELdoesn’t exist or you lack access
- Verify API key validity and credit balance
- Test custom gateway endpoint if configured
- Confirm model name and access permissions
Local Testing Commands
Before deploying, you can test locally using:Next Steps
After successful deployment:- Set up custom domains in Vercel settings
- Configure production OAuth providers with your custom domain
- Monitor application performance in Vercel Analytics
- Review MongoDB Atlas metrics and set up alerts