Multi-Tenant Authentication
Overview
The do.dev application supports multi-tenant authentication through organizations and teams. Users can belong to multiple organizations with different roles and permissions within each organization.
Key Features
1. Organization-Based Access
- Users can be members of multiple organizations
- Each organization has its own set of members, roles, and permissions
- Organization isolation ensures data privacy between tenants
2. Database Structure
userstable for individual user profilesorganizationstable for tenant isolationorganizationMemberstable for user-organization relationships with roles- Indexes ensure proper access control and performance
3. Authentication Flow
Sign Up
- User provides email/password on the do.dev sign-up page
- System creates user account with default "waitlist" role
- Admin approval required for access to platform features
- User can be invited to organizations after approval
Sign In
- User provides credentials through Clerk authentication
- System verifies user account and role status
- User gains access based on their role and organization memberships
- Dashboard shows organizations the user belongs to
OAuth (GitHub/Google)
- User authenticates with OAuth provider
- Clerk handles OAuth flow and user creation
- User sync creates account in Convex with default "waitlist" role
- Admin approval process same as email/password signup
Implementation Details
Frontend Organization Management
// hooks/useOrganizations.ts
export function useOrganizations() {
// Get user's organizations and current organization
// Handle organization switching
// Manage organization permissions
}
// components/organization-switcher.tsx
export function OrganizationSwitcher() {
// UI component for switching between organizations
// Shows user's role in each organization
}Backend Queries
// Get user's organizations
getUserOrganizations({ userId: "usr_xxxxx" })
// Check user role in organization
getUserRoleInOrganization({ userId: "usr_xxxxx", organizationId: "org_xxxxx" })
// Get organization members
getOrganizationMembers({ organizationId: "org_xxxxx" })Organization Management
Creating Organizations
// Create new organization
await convex.mutation(api.organizations.create, {
name: "My Company",
slug: "my-company",
ownerId: currentUser.id
});Inviting Members
// Invite user to organization
await convex.mutation(api.organizations.inviteMember, {
organizationId: "org_xxxxx",
email: "user@example.com",
role: "member"
});Benefits
- Multi-Tenant Support: Complete isolation between organizations
- Flexible Roles: Different roles per organization (owner, admin, member)
- Scalable Architecture: Supports enterprise customers with teams
- Data Isolation: Organization data is completely segregated
Considerations
- Users can belong to multiple organizations
- Role-based access control per organization
- Organization switching requires permission checks
- Admin approval required for new user accounts