Multi-tenancy requires Spree Enterprise Edition license.
- customer accounts
- staff accounts (admin users), staff can manage multiple tenants (it’s the standard invitation flow)
- products, categories, and other catalog data
- orders and order history
- shipping and payment methods
- tax rates and zones
- store settings (name, logo, etc.)
- etc.
If you need individual seller/supplier/vendor accounts but shared product listings under one site you should use multi vendor recipe instead.
Prerequisites
- You need to be on Spree 5.1+, we recommend using CLI to setup your Spree application.
- You need to set 2 environment variables in your
backenddirectory:KEYGEN_ACCOUNT_IDKEYGEN_LICENSE_KEY
- Both PostgreSQL and MySQL are supported
You will need to add these environment variables to your CI/CD pipeline and staging/production environments.
Installing gems
-
Add the following code to your
Gemfile: -
Install gems:
-
Run generators:
This will copy and run migrations for
spree_enterpriseandspree_multi_tenantgems.
Setting root domain
Usually multi-tenant applications are configured to use subdomains for each tenant. For example, if your root domain isexample.com, you can have tenants like tenant1.example.com, tenant2.example.com, etc.
To make it work you need to set the Spree.root_domain in your config/initializers/spree.rb file, eg.
localhost as root domain, and use tenant1.localhost, tenant2.localhost, etc. for tenants.
Customer User Class adjustment
We need to make slight adjustments to the user class so it can work in a multi-tenant environment. You need to remove:validatable module. This module is responsible for validating the user’s email uniqueness. We need to change it to validate it in the scope of the tenant.
backend/app/models/spree/user.rb

