This repository contains comprehensive guides and implementation strategies for building multi-tenant applications using Phoenix 1.8. The documentation focuses on two different multi-tenancy approaches with role-based authorization.
Multi-tenancy allows a single application instance to serve multiple tenants (organizations or teams) while maintaining proper data isolation. This repository documents two distinct approaches to implementing multi-tenancy in Phoenix 1.8 applications:
- Single-Organization Model: Each user belongs to exactly one organization at a time
- Multi-Organization Model: Users can belong to multiple organizations simultaneously with different roles in each
Both approaches use Phoenix 1.8's new Scopes feature and implement role-based authorization using Bodyguard.
The single-organization approach is simpler to implement and suitable for applications where users typically belong to just one organization. Key features:
- Organization ID is stored directly on the user record
- Each user has a single role (admin, manager, member) within their organization
- Simpler data structure and queries
- Includes system administrator functionality that spans across organizations
See: Single-Organization Guide
The multi-organization approach offers greater flexibility and is ideal for applications where users need to be members of multiple organizations with different roles in each. Key features:
- Uses a join table (organization_memberships) to connect users to organizations
- Users can have different roles in different organizations
- More complex data structure but offers greater flexibility
- Includes organization switching functionality
Both approaches include detailed implementation checklists to guide the development process:
Both guides cover:
- Database schema design and migrations
- Phoenix authentication integration with phx.gen.auth
- Using Phoenix 1.8's new Scopes for tenant isolation
- Role-based authorization with Bodyguard
- LiveView implementation with Phoenix 1.8's component-based approach
- Comprehensive testing strategies
- Choose the appropriate multi-tenancy model for your application requirements
- Follow the detailed implementation guide for your chosen approach
- Use the implementation checklist to track progress
- Reference the specific guides for specialized features (e.g., system administrator implementation)
This documentation is provided for educational purposes. Feel free to use these strategies in your own applications.