Environment Variables - kjanat/livedash-node GitHub Wiki
Environment Variables Setup
This document explains how to configure environment variables for the LiveDash Node.js application running on Cloudflare Workers with Next.js and Auth.js.
Overview
The application uses multiple environment files with a specific loading order:
.env
- Default fallback values (lowest priority).env.development
- Development-specific variables.env.production
- Production-specific variables.env.local
- Local overrides (highest priority, git-ignored).dev.vars
- Wrangler/Cloudflare Workers development variables (git-ignored)
Note: Later files override earlier ones.
.env.local
always has the highest priority for Next.js, while.dev.vars
is used specifically by Wrangler for local Cloudflare Workers development.
File Descriptions
.env
(Base Configuration)
Shared defaults and public variables that work across all environments.
Required Variables:
# NextAuth.js Base URL
NEXTAUTH_URL=http://localhost:3000
# JWT Secret (use random string in production)
NEXTAUTH_SECRET=your-secret-here
# Database Connection
DATABASE_URL="file:./dev.db"
.env.development
(Development Settings)
Local development database connections, debug flags, and development-specific configurations.
Required Variables:
# Development environment flag
NODE_ENV=development
# Local development URL
NEXTAUTH_URL=http://localhost:3000
# Development JWT secret (can be fixed for consistency)
NEXTAUTH_SECRET=development-fixed-secret
# Local SQLite database
DATABASE_URL="file:./dev.db"
# Debug flags (optional)
AUTH_DEBUG=true
.env.production
(Production Settings)
Production database URLs, API endpoints, and production-specific configurations.
Required Variables:
# Production environment flag
NODE_ENV=production
# Production URL
NEXTAUTH_URL=https://your-domain.com
# Production JWT secret (must be random and secure)
NEXTAUTH_SECRET=production-random-secret
# Cloudflare D1 database connection
DATABASE_URL="your-d1-connection-string"
.env.local
(Personal Overrides)
Personal secrets, local overrides, and sensitive data. This file is git-ignored.
Required Variables:
# Personal auth secrets
AUTH_SECRET=your-random-secret
# OAuth Provider Credentials
AUTH_GITHUB_ID=your-github-client-id
AUTH_GITHUB_SECRET=your-github-client-secret
# Personal database overrides (if needed)
DATABASE_URL="your-personal-db-connection"
# API Keys
CLOUDFLARE_API_TOKEN=your-cloudflare-token
.dev.vars
(Wrangler Development Variables)
Used specifically by Wrangler for local Cloudflare Workers development. This file is git-ignored.
Required Variables:
# Auth secrets for Workers runtime
AUTH_SECRET=your-random-secret
# OAuth Provider Credentials (same as .env.local)
AUTH_GITHUB_ID=your-github-client-id
AUTH_GITHUB_SECRET=your-github-client-secret
# Database connection for Workers
DATABASE_URL="file:./dev.db"
# Workers-specific variables
NODE_ENV=development
AUTH_URL=http://localhost:8787
Note:
.dev.vars
is used when runningpnpm run preview
orwrangler dev
, while.env.local
is used forpnpm run dev
(Next.js dev server).
Auth.js v5 Configuration
With Auth.js v5, you can use automatic environment variable inference by following the AUTH_*
naming convention:
OAuth Providers
# GitHub OAuth
AUTH_GITHUB_ID=your-github-client-id
AUTH_GITHUB_SECRET=your-github-client-secret
# Google OAuth
AUTH_GOOGLE_ID=your-google-client-id
AUTH_GOOGLE_SECRET=your-google-client-secret
# Discord OAuth
AUTH_DISCORD_ID=your-discord-client-id
AUTH_DISCORD_SECRET=your-discord-client-secret
Core Auth Variables
# Required: Random secret for JWT signing
AUTH_SECRET=your-random-secret
# Optional: Custom auth URL
AUTH_URL=https://your-domain.com
# Optional: Trust proxy headers
AUTH_TRUST_HOST=true
Cloudflare-Specific Variables
Wrangler Configuration
These variables should also be configured in wrangler.jsonc
:
# Cloudflare D1 Database
DATABASE_URL=your-d1-connection-string
# Worker Environment
NODE_ENV=production
# Compatibility settings
NODEJS_COMPAT=true
D1 Database Connection
# Development (local SQLite)
DATABASE_URL="file:./dev.db"
# Production (Cloudflare D1)
DATABASE_URL="cloudflare-d1://your-database-id"
Generating Secrets
AUTH_SECRET / NEXTAUTH_SECRET
Cross-platform command to generate a secure random secret:
# Using Node.js (works on all platforms)
node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"
# Using OpenSSL (Linux/macOS/WSL)
openssl rand -hex 32
# Using PowerShell (Windows)
[System.Web.Security.Membership]::GeneratePassword(64, 0)
# Online alternative
# Visit: https://generate-secret.vercel.app/32
OAuth Client Secrets
These are provided by OAuth providers when you create applications:
- GitHub: https://github.com/settings/applications/new
- Google: https://console.developers.google.com/
- Discord: https://discord.com/developers/applications
Runtime Differences
pnpm run dev
)
Development (- Uses Next.js dev server (Node.js runtime)
- Loads
.env.development
+.env.local
- Uses local SQLite database
- Hot reload enabled
pnpm run preview
)
Preview (- Uses Cloudflare Workers runtime (
workerd
) - Loads
.dev.vars
for environment variables - May have different environment variable handling
- More accurate to production
Production Deployment
- Cloudflare Workers runtime
- Environment variables from Wrangler configuration
- D1 database binding
- Production OAuth callbacks
Example Complete Setup
.env
# Base configuration
NEXTAUTH_URL=http://localhost:3000
NEXTAUTH_SECRET=development-fallback-secret
DATABASE_URL="file:./dev.db"
.env.development
NODE_ENV=development
NEXTAUTH_URL=http://localhost:3000
NEXTAUTH_SECRET=development-fixed-secret
DATABASE_URL="file:./dev.db"
AUTH_DEBUG=true
.env.production
NODE_ENV=production
NEXTAUTH_URL=https://livedash.your-domain.com
NEXTAUTH_SECRET=CHANGE-ME-TO-RANDOM-SECRET
DATABASE_URL="cloudflare-d1://your-d1-database-id"
.env.local
(git-ignored)
# Generated with: node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"
AUTH_SECRET=a8e6a602f61cc46530bf78ee9dfa9022528a5e1170b2b8687303f86934170319
# OAuth credentials from GitHub
AUTH_GITHUB_ID=your-actual-github-client-id
AUTH_GITHUB_SECRET=your-actual-github-client-secret
# Cloudflare API token (if needed for deployment)
CLOUDFLARE_API_TOKEN=your-actual-cloudflare-token
.dev.vars
(git-ignored, for Wrangler)
# Same secrets as .env.local but for Wrangler/Workers development
AUTH_SECRET=a8e6a602f61cc46530bf78ee9dfa9022528a5e1170b2b8687303f86934170319
AUTH_GITHUB_ID=your-actual-github-client-id
AUTH_GITHUB_SECRET=your-actual-github-client-secret
# Workers-specific URL (different port from Next.js dev)
AUTH_URL=http://localhost:8787
# Database for Workers development
DATABASE_URL="file:./dev.db"
Cloudflare API token (if needed for deployment)
CLOUDFLARE_API_TOKEN=your-actual-cloudflare-token
Troubleshooting
Common Issues
-
Environment variables not loading in preview mode
- Run
pnpm run cf-typegen
to generate Cloudflare types - Check
wrangler.jsonc
configuration - Ensure
.dev.vars
file exists for Wrangler development
- Run
-
Auth.js not finding variables
- Ensure variables follow
AUTH_*
naming convention - Check
.env.local
exists forpnpm run dev
- Check
.dev.vars
exists forpnpm run preview
- Ensure variables follow
-
Database connection errors
- Verify
DATABASE_URL
format for your environment - For D1: ensure database binding is configured in Wrangler
- Verify
Verification Commands
# Check which environment variables are loaded
pnpm run dev --debug
# Generate Cloudflare environment types
pnpm run cf-typegen
# Test Wrangler configuration
wrangler dev --local
# Verify database connection
wrangler d1 execute your-db --command "SELECT 1"
Security Best Practices
- Never commit
.env.local
- It's git-ignored for a reason - Use different secrets per environment - Development vs Production
- Rotate secrets regularly - Especially in production
- Use minimal permissions - For API tokens and OAuth apps
- Validate environment variables - In your application startup code