- Fix currency defaults (SATS → CHF) across forms, configs, and API routes - Remove @ts-nocheck from socialService.ts and improve type safety - Replace console.log statements with proper logger utility in production code - Additional type safety improvements and code quality enhancements - Documentation updates for improved maintainability All changes follow DRY, SSOT, and separation of concerns principles.
6.1 KiB
🔐 Environment Variable Management
Complete guide to managing environment variables safely in OrangeCat development.
🚨 The Problem
Traditional .env files are fragile:
- Easy to accidentally overwrite with
cpcommands - No automatic backups
- Manual token management
- Risk of committing secrets to git
✅ The Solution
OrangeCat now uses a comprehensive environment management system that prevents credential loss and provides secure authentication flows.
🛠️ System Components
1. direnv - Automatic Environment Loading
direnv automatically loads environment variables when you enter the project directory.
Installation:
./scripts/setup-env.sh # Installs and configures direnv
How it works:
- Variables load automatically when
cdinto the project - No more manual
source .env.local - Environment is isolated to the project directory
2. Interactive OAuth Login
Secure authentication without storing tokens in plain text.
GitHub Login:
node scripts/auth/github-login.js
- Uses GitHub's Device Flow OAuth
- No password/token exposure
- Automatic token storage in
.env.local
Vercel Login:
node scripts/auth/vercel-login.js
- Uses Vercel CLI authentication
- Automatic token generation and storage
3. Environment Manager
Comprehensive tool for environment management.
node scripts/utils/env-manager.js --help
Commands:
setup- Create initial.env.localbackup- Create backup before changesvalidate- Check required variablesrestore- Restore from backuplist- Show available backups
4. Automatic Backups
Before any change:
# Automatic backup created
.env-backups/.env.local.2025-12-02T10-30-45.backup
Manual backup:
node scripts/utils/env-manager.js backup
Restore from backup:
node scripts/utils/env-manager.js restore
🚀 Quick Start
First Time Setup
# 1. Run the automated setup
./scripts/setup-env.sh
# 2. Authenticate with services
node scripts/auth/github-login.js
node scripts/auth/vercel-login.js
# 3. Validate setup
node scripts/utils/env-manager.js validate
# 4. Start developing
npm run dev
Daily Development
# Just cd into the project - environment loads automatically!
cd orangecat
npm run dev # Variables are already loaded
🔧 Manual Management
Editing Environment Variables
Safe method:
# Use direnv's built-in editor
direnv edit .
Alternative:
# Manual edit with automatic backup
node scripts/utils/env-manager.js backup
nano .env.local # Edit file
node scripts/utils/env-manager.js validate
Checking Status
# Validate all required variables are set
node scripts/utils/env-manager.js validate
# List all backups
node scripts/utils/env-manager.js list
# Show help
node scripts/utils/env-manager.js help
🛡️ Safety Features
1. Automatic Backups
- Created before any environment changes
- Timestamped and hashed for uniqueness
- Stored in
.env-backups/(gitignored)
2. Validation
- Checks all required variables are set
- Warns about missing optional variables
- Prevents deployment with invalid config
3. No Git Commits
.env.localis gitignored.env-backups/is gitignored.envrcis tracked (safe, contains no secrets)
4. OAuth Security
- No plaintext tokens in scripts
- Device Flow authentication
- Tokens stored securely in environment
🔄 Migration from Old System
If you have an existing .env.local:
# 1. Create backup of current file
node scripts/utils/env-manager.js backup
# 2. Run setup (creates new structure)
./scripts/setup-env.sh
# 3. Manually copy your tokens from backup to new .env.local
# 4. Validate
node scripts/utils/env-manager.js validate
🐛 Troubleshooting
Environment Not Loading
# Check direnv status
direnv status
# Reload environment
direnv reload
# Check if hook is installed
echo $DIRENV_DIR
Missing Variables
# Validate current setup
node scripts/utils/env-manager.js validate
# Check .env.local exists
ls -la .env.local
# Check .envrc is allowed
direnv allow .
Authentication Issues
# Re-run GitHub login
node scripts/auth/github-login.js
# Re-run Vercel login
node scripts/auth/vercel-login.js
# Check tokens are set
grep -E "(GITHUB_TOKEN|VERCEL_TOKEN)" .env.local
📋 File Structure
orangecat/
├── .envrc # direnv configuration (tracked)
├── .env.local # Your environment variables (gitignored)
├── .env-backups/ # Automatic backups (gitignored)
│ └── .env.local.TIMESTAMP.backup
├── scripts/
│ ├── setup-env.sh # Automated setup script
│ ├── auth/ # OAuth login scripts
│ │ ├── github-login.js
│ │ └── vercel-login.js
│ └── utils/
│ └── env-manager.js # Environment management tool
└── docs/
└── development/
└── environment-management.md # This file
🎯 Best Practices
1. Never Edit .envrc Directly
# ❌ Wrong
nano .envrc
# ✅ Correct
direnv edit .
2. Always Backup Before Manual Changes
# Before editing .env.local manually
node scripts/utils/env-manager.js backup
nano .env.local
3. Use OAuth Authentication
# ❌ Manual token entry
echo "GITHUB_TOKEN=ghp_..." >> .env.local
# ✅ Secure OAuth flow
node scripts/auth/github-login.js
4. Validate After Changes
# Always validate after making changes
node scripts/utils/env-manager.js validate
📞 Getting Help
If you encounter issues:
- Check the troubleshooting section above
- Run validation:
node scripts/utils/env-manager.js validate - Check backups:
node scripts/utils/env-manager.js list - Restore if needed:
node scripts/utils/env-manager.js restore
Last Updated: December 2, 2025 System Version: v2.0 (OAuth + direnv) Safety Level: 🔒 Maximum (Zero credential loss risk)