Files
orangecat/docs/development/environment-management.md
CatoandClaude Opus 5.5 24c3d140ae docs: archive point-in-time records, fix live docs against the tree (#1349)
* docs: archive point-in-time records, drop broken guide symlinks

Status reports, progress trackers, audits, session summaries, completed
migration plans and the 2025 roadmap move to docs/archive/ (same relative
path), with a README saying they describe the day they were written.
.ai-guides/ held only symlinks into a docs/guides/ai/ that no longer
exists, tests/unit/docs/ held a stray copy of an old getting-started page,
and docs/operations/PORT_MANAGEMENT_README.md duplicated port-management.md;
all three are deleted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PJkbo18cRNXtgPWZN2S9cD

* docs: correct drifted paths, links and versions in current docs

Fix in place the reference docs agents use: point paths at where the code
lives now (src/stores/auth.ts, src/lib/supabase/*, scripts/dev/*,
docs/reference/api/CONVENTIONS.md, the squashed baseline migration),
rewrite the development, features, operations and API index pages to list
documents that exist, say plainly where something is gone (/api/metrics,
the CSP report route, .envrc in the repo, Loki's `migrate` script), and
correct the Next.js major. docs-truth findings: 468 -> 2 (both "Next 30/90
Days" headings in the legal overview, a checker false positive).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PJkbo18cRNXtgPWZN2S9cD

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-19 14:09:06 +02:00

6.2 KiB

🔐 Environment Variable Management

Complete guide to managing environment variables safely in OrangeCat development.

Note (2026-06): Production is self-hosted on Hetzner ("bitbaum", behind Caddy). The production env lives in /opt/orangecat/app/.env on the box; deploys go through the self-host flow (scripts/deploy-selfhost.sh / the GitHub Actions deploy job). This guide covers local development env only.

🚨 The Problem

Traditional .env files are fragile:

  • Easy to accidentally overwrite with cp commands
  • 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 cd into 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

3. Environment Manager

Comprehensive tool for environment management.

node scripts/utils/env-manager.js --help

Commands:

  • setup - Create initial .env.local
  • backup - Create backup before changes
  • validate - Check required variables
  • restore - Restore from backup
  • list - 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

# 3. Validate setup
node scripts/utils/env-manager.js validate

# 4. Start developing
pnpm run dev

Daily Development

# Just cd into the project - environment loads automatically!
cd orangecat
pnpm 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.local is gitignored
  • .env-backups/ is gitignored
  • .envrc is generated locally by scripts/setup-env.sh (no secrets); it does not exist in the repo

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

# Check tokens are set
grep -E "GITHUB_TOKEN" .env.local

📋 File Structure

orangecat/
├── .envrc                    # direnv configuration (generated by setup-env.sh, not committed)
├── .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
│   └── 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:

  1. Check the troubleshooting section above
  2. Run validation: node scripts/utils/env-manager.js validate
  3. Check backups: node scripts/utils/env-manager.js list
  4. Restore if needed: node scripts/utils/env-manager.js restore

Last Updated: 2026-06-15 System Version: v2.0 (OAuth + direnv) Safety Level: 🔒 Maximum (Zero credential loss risk)