Skip to content

Make ENV vars optional for multi-tenant usage#9

Merged
ebrett merged 1 commit intomainfrom
feature/optional-env-vars
Dec 10, 2025
Merged

Make ENV vars optional for multi-tenant usage#9
ebrett merged 1 commit intomainfrom
feature/optional-env-vars

Conversation

@ebrett
Copy link
Owner

@ebrett ebrett commented Dec 10, 2025

Implements Option A from #7: Make ENV vars optional in global config and emphasize per-instance credential passing for multi-tenant applications.

Problem

The gem appeared to require NATIONBUILDER_CLIENT_ID and NATIONBUILDER_CLIENT_SECRET environment variables, but this was only due to the global configuration pattern shown in examples. The gem already supports passing credentials per-instance, which is the preferred pattern for multi-tenant applications.

Solution

This PR clarifies the documentation to show that:

  1. ENV vars are optional - credentials can be passed directly to Client.new
  2. Per-instance credentials are recommended - especially for multi-tenant apps
  3. Global config is optional - only needed if you want to set defaults

Changes

Documentation Updates (README.md)

  • Quick Start section: Now recommends per-instance credentials first, with global config as optional
  • Configuration Options: Reorganized to show instance configuration first (recommended), followed by optional global configuration
  • Multi-Tenant Usage: Moved earlier in document and enhanced with database storage example showing how to store OAuth credentials in database instead of ENV vars
  • Removed duplicate: Old Multi-Tenant section that was less comprehensive

Test Updates (spec/spec_helper.rb)

  • Made ENV vars optional in VCR configuration (only filters if set)
  • Added comments clarifying ENV vars are NOT required for tests to run
  • Tests already use per-instance credentials pattern

Benefits

No ENV variables required - Credentials passed per-instance
Better multi-tenant support - Store OAuth credentials in database
Flexible configuration - Mix global defaults with per-instance overrides
Cleaner architecture - Credentials in database, not environment
All tests passing - 92.51% coverage, no ENV vars needed

Example Usage

Before (appeared to require ENV vars)

# config/initializers/nationbuilder_api.rb
NationbuilderApi.configure do |config|
  config.client_id = ENV['NATIONBUILDER_CLIENT_ID']  # Required?
  config.client_secret = ENV['NATIONBUILDER_CLIENT_SECRET']  # Required?
  config.redirect_uri = ENV['NATIONBUILDER_REDIRECT_URI']
end

After (per-instance credentials, no ENV vars needed)

# app/services/nationbuilder_service.rb
client = NationbuilderApi::Client.new(
  client_id: nation.client_id,      # From database
  client_secret: nation.client_secret,  # From database
  redirect_uri: nation.redirect_uri,
  identifier: "nation_#{nation.id}"
)

Closes #7

Implements Option A from issue #7: Make ENV vars optional in global config
and emphasize per-instance credential passing for multi-tenant applications.

Changes:
- Updated README Quick Start to recommend per-instance credentials
- Made global configuration explicitly optional
- Moved and enhanced Multi-Tenant Usage section with database storage example
- Updated Configuration Options to show instance config first (recommended)
- Updated spec_helper to make ENV vars optional (only used for VCR filtering)
- All tests passing without requiring ENV vars

This change supports the recommended pattern for multi-tenant applications
where OAuth credentials are stored in the database and passed per-instance,
eliminating the need for environment variables.

Closes #7

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@ebrett ebrett merged commit b7412ce into main Dec 10, 2025
3 checks passed
@ebrett ebrett deleted the feature/optional-env-vars branch January 29, 2026 04:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ENV Var Requirement

1 participant