Spree Multi Tenant Installation
Learn how to setup a multi-tenant eCommerce (SaaS) with Spree
This installation instructions assume you have a clean Spree Starter installation and you’ve purchased the Spree Enterprise Edition license.
For migrating existing application from single-tenant to multi-tenant, please contact us at [email protected].
Prerequisites
- You need to be on Spree 5.1+
- You need to have 2 environment variables set:
KEYGEN_ACCOUNT_ID
KEYGEN_LICENSE_KEY
Environment variables will be provided to you after purchasing the Spree Enterprise Edition license.
Customer User Class
Please follow Authentication documentation to create a new user class.
Now we will need to make slight adjustments to the generated user class so it can work in a multi-tenant environment.
If you’re using Devise, you will 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.
Spree Multi-Tenant gem injects SpreeMultiTenant::CustomerUserConcern
that handles that instead.
Admin User Class
For our multi-tenant setup we will need a separate Admin user class. Our standard User class will be only used for customer accounts which will be isolated by tenant. Admin user classes will be shared across tenants allowing them to manage multiple tenants from a single admin panel.
Let’s create a new model with Devise (unless you already have one):
Replace the generated Devise code with the following:
If you already have a custom Admin user class, you can add the following code to it:
This will make sure that the Admin user class works well with Spree ecosystem.
Register new model in config/initializers/spree.rb
:
Installing gems
-
Add the following code to your
Gemfile
: -
Install gems:
-
Run generator:
This will copy and run migrations for
spree_multi_tenant
gem.