B2B Kit
Launch B2B SaaS with support for multiple organizations and plans, comes with a super admin dashboard to manage organizations and plans.
This documentation is for the B2B Kit, which is a set of utilities for building a B2B SaaS application with Indie Kit.
If you don't have the B2B Kit installed, you can install it by running:
Make sure you have valid Indie Kit license to clone this repo. Buy here Indi B2B Kit
Table of Contents
- Database Models
- Organizations
- Authentication & Route Wrappers
- Organization Roles and Permissions
- Invitations and Memberships
- Plans and Subscriptions
- Special Hooks
Database Models
Organizations
The B2B Kit implements a multi-organization system where users can belong to multiple organizations with different roles. Each organization can have its own subscription plan and settings.
Key features:
- Organization Structure: Each organization has a unique ID, name, slug, and optional image.
- Role-based Access: Members have different roles (owner, admin, user) determining their permissions.
- Onboarding Process: Organizations track onboarding status and collect onboarding data.
- Billing Integration: Organizations can have subscriptions with Stripe or LemonSqueezy.
Core Organization Model:
The organization model includes:
- Basic info (name, slug, image)
- Timestamps for creation and updates
- Onboarding status and data
- Billing information (customer IDs and subscription IDs)
- Plan reference
Authentication & Route Wrappers
The B2B Kit provides several authentication wrappers to secure API routes:
withAuthRequired
Used to secure routes that require user authentication.
withOrganizationAuthRequired
Used to secure routes that require organization-level authentication with role checks.
withSuperAdminAuthRequired
Used to secure routes that should only be accessible to super administrators.
Organization Roles and Permissions
The B2B Kit implements a role-based permission system with three roles:
- Owner: Highest level of access, can perform all actions within an organization
- Admin: Can manage organization settings, members, and most resources
- User: Basic access to use the application
Role hierarchy is defined in the hasHigherOrEqualRole
function:
This hierarchy is used to check if a user has sufficient permissions to perform actions.
Invitations and Memberships
Organization Memberships
The membership model creates a many-to-many relationship between users and organizations:
- Links a user to an organization
- Assigns a role (owner, admin, user)
- Includes timestamps for creation and updates
- Uses a composite primary key (organizationId, userId)
Organization Invites
The invite system allows organization members to invite others:
- Stores the invitee's email
- References the organization
- Specifies the intended role
- Includes an expiration timestamp
- Contains a unique token for verification
Plans and Subscriptions
Plan Model
The plan model defines different subscription tiers:
- Basic information (name, codename)
- Default flag to mark the default plan
- Pricing options (monthly, yearly, one-time)
- Integration with payment providers (Stripe, LemonSqueezy)
- Quotas defining feature limits
Subscription Management
Creating Plans (Admin)
Plans are created through the super admin interface:
- Navigate to
/super-admin/plans
- Click on "Create Plan"
- Fill in plan details including:
- Basic info (name, codename)
- Pricing options
- Payment provider IDs
- Feature quotas
Subscription URL Generation
The getSubscribeUrl
function generates URLs for subscription pages:
Example usage in a Next.js Link component:
Special Hooks
useOrganization
A React hook for accessing and managing the current organization:
Example usage:
Organization Utilities
The codebase includes several utility functions for organizations:
getUserOrganizations
: Fetches all organizations a user belongs togetUserOrganizationById
: Fetches a specific organization with its plancreateOrganization
: Creates a new organization and assigns the creator as ownerhasHigherOrEqualRole
: Checks if a user's role is sufficient for an actionuserBelongsToOrganization
: Verifies if a user is a member of an organization