Supabase Auth Implementation Plan for @knowcode/doc-builder
Implementation Overview
This document outlines the complete plan for integrating Supabase authentication into @knowcode/doc-builder, providing a simple but secure authentication system that leverages Supabase's built-in auth functionality.
Goals
- Simplicity: Minimal configuration and setup required
- Security: Leverage Supabase's enterprise-grade authentication
- Clean Architecture: Remove insecure basic auth entirely
- Future-Proof: Single, secure authentication method
Architecture Summary
graph TD
subgraph "Build Time"
Config[doc-builder.config.js]
Build[Build Process]
Config --> Build
Build --> AuthJS[Generate auth.js]
Build --> LoginPage[Generate login.html]
Build --> SDK[Include Supabase SDK]
end
subgraph "Runtime"
User[User Visit]
User --> Check{Auth Check}
Check -->|No Auth| Login
Check -->|Has Auth| Access{Access Check}
Access -->|Allowed| Docs[Show Docs]
Access -->|Denied| Denied[Access Denied]
Login --> SupaAuth[Supabase Auth]
SupaAuth --> Check
end
subgraph "Supabase"
AuthUsers[auth.users]
DocAccess[docbuilder_access with domain]
AuthUsers -.-> DocAccess
end
Database Schema
-- Single table for user access control (simplified!)
CREATE TABLE docbuilder_access (
user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE,
domain TEXT NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW(),
PRIMARY KEY (user_id, domain)
);
-- Create index for faster lookups
CREATE INDEX idx_docbuilder_access_domain ON docbuilder_access(domain);
-- Enable Row Level Security
ALTER TABLE docbuilder_access ENABLE ROW LEVEL SECURITY;
-- RLS Policy: Users can only see their own access
CREATE POLICY "Users see own access" ON docbuilder_access
FOR SELECT USING (user_id = auth.uid());
Implementation Steps
Phase 1: Core Infrastructure
1.1 Update Configuration System
- Replace auth types with:
false
(no auth) or'supabase'
(secure auth) - Remove all basic auth configuration fields
- Clean up insecure authentication references
File: lib/config.js
// Updated auth configuration - REMOVE basic auth fields
auth: {
// Only Supabase fields remain (domain-based, no siteId!)
supabaseUrl: '',
supabaseAnonKey: ''
// Domain is detected automatically from window.location.host
}
1.2 Create Supabase Auth Module
New File: lib/supabase-auth.js
- Supabase client initialization
- Auth check functionality
- Access verification
- Session management helpers
1.3 Update Core Builder
File: lib/core-builder.js
- Detect Supabase auth configuration
- Generate appropriate auth files
- Include Supabase SDK in build output
Phase 2: Client-Side Implementation
2.1 Supabase Auth Script
Generated File: js/auth.js
// Clean Supabase-only auth checking logic
// - Check for valid session
// - Verify site access
// - Handle redirects
// - No fallback to basic auth
2.2 Login Page Generation
Generated File: login.html
- Email/password form
- Supabase auth integration
- Error handling
- Redirect management
2.3 Logout Functionality
- Clear Supabase session
- Redirect to login page
- Clean up any local storage
Phase 3: Build Process Updates
3.1 SDK Integration
- Download Supabase client library during build
- Include in output directory
- Optimize for production use
3.2 Environment Variable Support
# Support for environment variables
SUPABASE_URL=https://xxx.supabase.co
SUPABASE_ANON_KEY=xxx
DOC_SITE_ID=uuid
3.3 Build Validation
- Check for required Supabase configuration
- Validate connection to Supabase
- Warn about missing setup
Phase 4: User Management Tools
4.1 CLI Commands
File: cli.js
- Major updates needed
# Initialize Supabase auth for project
npx @knowcode/doc-builder auth:init
# Add site to Supabase database
npx @knowcode/doc-builder auth:add-site --domain docs.example.com --name "Company Docs"
# Grant user access to site
npx @knowcode/doc-builder auth:grant --email user@example.com --site-id xxx
# Revoke user access
npx @knowcode/doc-builder auth:revoke --email user@example.com --site-id xxx
# List users with access to site
npx @knowcode/doc-builder auth:list-users --site-id xxx
# List all sites user has access to
npx @knowcode/doc-builder auth:list-sites --email user@example.com
# Remove the old --no-auth flag (no longer needed)
# Add validation for Supabase configuration
4.2 Admin Script
New File: lib/admin/supabase-admin.js
- Simple Node.js script for user management
- Can be used standalone or via CLI
- Requires service role key for admin operations
Phase 5: Documentation and Migration
5.1 Documentation Updates
- Update main README with Supabase auth option
- Create detailed Supabase setup guide
- Add troubleshooting section
- Include migration guide from basic auth
5.2 Migration Path
- Scripts to migrate from basic auth to Supabase
- Bulk user import functionality
- Backward compatibility period
File Changes Summary
Modified Files
lib/config.js
- Replace basic auth with Supabase configurationlib/core-builder.js
- Remove basic auth, add Supabase auth generationhtml/js/auth.js
- Remove basic auth logic entirelycli.js
- MAJOR UPDATE: Add auth management commands, remove --no-auth flagpackage.json
- Add @supabase/supabase-js dependencyREADME.md
- Document Supabase auth (remove basic auth docs)
New Files
lib/supabase-auth.js
- Supabase auth utilitieslib/admin/supabase-admin.js
- Admin management scriptdocs/guides/supabase-setup-guide.md
- Setup documentationexamples/supabase-auth/
- Example configuration
Generated Files (at build time)
- Clean
js/auth.js
with Supabase-only support - Secure
login.html
with Supabase integration - Included Supabase SDK files
Removed Files/Code
- All basic auth logic from existing auth.js
- Hardcoded credentials from login page generation
- Basic auth configuration options
- Insecure client-side credential checking
- CLI
--no-auth
flag (replaced withauthentication: false
in config) - Basic auth preset configurations
Testing Plan
Unit Tests
- Configuration validation
- Auth script generation
- Build process with Supabase
Integration Tests
- Full build with Supabase auth
- Login/logout flow
- Access control verification
Manual Testing
- Deploy test site with Supabase auth
- Test user registration/login
- Verify access control
- Test session persistence
Rollout Strategy
Version 2.0.0 (Breaking Change)
- BREAKING: Remove all basic auth functionality
- Core Supabase authentication only
- Clean, secure implementation
- Migration guide for existing users
Version 2.0.1 (Stable)
- Bug fixes from 2.0.0 release
- Enhanced CLI tools
- User management utilities
Version 2.1.0 (Enhanced)
- Advanced features (roles, audit logs)
- Admin dashboard
- SSO support
Success Criteria
- Working Authentication: Users can log in via Supabase
- Access Control: Only authorized users see documentation
- Security: No more client-side credentials vulnerability
- Simple Setup: < 10 minutes to configure
- Clean Architecture: Single auth method, no legacy code
Risk Mitigation
- Complexity: Keep initial implementation minimal
- Breaking Changes: Clear migration path and documentation
- Performance: Cache auth checks appropriately
- Security: Follow Supabase best practices
- Support: Provide clear documentation and migration guide
Implementation Status: COMPLETE
All planned phases have been successfully implemented:
Phase 1: Core Infrastructure - COMPLETE
- Updated configuration system to remove basic auth
- Added Supabase SDK dependency
- Created Supabase auth module (
lib/supabase-auth.js
) - Updated core-builder.js integration
Phase 2: Client-Side Implementation - COMPLETE
- Generated Supabase auth script replaces basic auth
- Secure login page with Supabase integration
- Logout functionality with proper session cleanup
Phase 3: Build Process Updates - COMPLETE
- Supabase SDK integration in build output
- Environment variable support
- Build validation and error handling
Phase 4: User Management Tools - COMPLETE
- CLI commands:
auth:init
,auth:add-site
,auth:grant
,auth:revoke
,auth:list-users
- Updated settings command to show Supabase config
- Removed insecure
--no-auth
flag
Phase 5: Documentation and Migration - COMPLETE
- Comprehensive Supabase setup guide created
- Updated main README with new auth information
- Deprecated old authentication guide with security warnings
- Migration instructions for existing users
Next Steps for Deployment
Questions to Address
- Should we support multiple auth providers initially or just email/password?
- Do we need audit logging from day one?
- Should the admin tools be part of the main package or separate?
- What's the target timeline for the beta release?
This plan provides a clear path to implementing Supabase authentication while maintaining the simplicity that makes @knowcode/doc-builder attractive to users.