Overview
SlugShare requires several environment variables to connect to the database, secure authentication, and enable Google OAuth sign-in.Creating the .env File
Create a.env file in the webserver directory (the root of your Next.js application):
Required Environment Variables
Database URL
Add your PostgreSQL connection string:Format breakdown:Example connection strings:
user- Your PostgreSQL usernamepassword- Your PostgreSQL passwordlocalhost:5432- Database host and portdatabase_name- Your database namesslmode=require- Enable SSL connection (recommended for production)
For local development without SSL, you can use
?sslmode=disable instead.NextAuth Secret
Generate and add an authentication secret:Copy the output and add it to your
.env:NextAuth.js v5 uses
AUTH_SECRET (not NEXTAUTH_SECRET from v4).Google OAuth Credentials
Add your Google OAuth credentials for sign-in functionality:See the Google OAuth Setup section below for instructions on obtaining these credentials.
Complete .env Template
Your final.env file should look like this:
Optional Environment Variables
Prisma Accelerate
If you’re using Prisma Accelerate for connection pooling and caching:Alternative PostgreSQL URL
Some hosting providers usePOSTGRES_URL instead of DATABASE_URL:
Google OAuth Setup
To enable Google sign-in, you need to create OAuth credentials in Google Cloud Console:Access Google Cloud Console
Navigate to Google Cloud Console
Create or select a project
- Click the project dropdown in the top navigation
- Select an existing project or click New Project
- Give your project a name (e.g., “SlugShare”)
Create OAuth credentials
- Go to APIs & Services > Credentials
- Click Create Credentials > OAuth client ID
- Select Application type: Web application
- Give it a name (e.g., “SlugShare Web Client”)
Verifying Environment Variables
After setting up your.env file, verify it’s being loaded correctly:
Next.js automatically loads environment variables from
.env files. You don’t need to install dotenv separately.Security Best Practices
- Never commit
.envto git - It’s already in.gitignore - Use different secrets for development and production
- Rotate secrets regularly - Especially after team member changes
- Use SSL for database connections - Add
sslmode=requireto production URLs - Restrict OAuth redirect URIs - Only add URLs you actually use
Troubleshooting
”Cannot connect to database”
Check that:- Your PostgreSQL server is running
- The
DATABASE_URLformat is correct - Username and password are correct
- The database exists (you may need to create it first)
“Invalid AUTH_SECRET”
Ensure:- The secret is at least 32 characters
- There are no extra spaces or quotes
- You’ve restarted the dev server after adding it
”Google OAuth not working”
Verify:GOOGLE_CLIENT_IDandGOOGLE_CLIENT_SECRETare correct- The redirect URI in Google Cloud Console matches your app URL exactly
- Google+ API is enabled in your Google Cloud project