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+, we recommend using spree_starter as a base for your application
  • You need to have 2 environment variables set:
    • KEYGEN_ACCOUNT_ID
    • KEYGEN_LICENSE_KEY
  • We support both PostgreSQL and MySQL databases
Environment variables will be provided to you after purchasing the Spree Enterprise Edition license.
You will need to add these environment variables to your CI/CD pipeline and production environments.

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):
rails g devise Spree::AdminUser
Replace the generated Devise code with the following:
class Spree::AdminUser < Spree::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
        :recoverable, :rememberable, :validatable

  # Spree modules
  include Spree::UserMethods
end
If you already have a custom Admin user class, you can add the following code to it:
  # Spree modules
  include Spree::UserMethods
This will make sure that the Admin user class works well with Spree ecosystem. Register new model in config/initializers/spree.rb:
Spree.admin_user_class = "Spree::AdminUser"

Installing gems

  1. Add the following code to your Gemfile:
    source "https://license:#{ENV['KEYGEN_LICENSE_KEY']}@rubygems.pkg.keygen.sh/#{ENV['KEYGEN_ACCOUNT_ID']}" do
      gem 'spree_enterprise'
      gem 'spree_multi_tenant'
    end
    
  2. Install gems:
    bundle install
    
  3. Run generators:
    bundle exec rails g spree_enterprise:install && bundle exec rails g spree_multi_tenant:install
    
    This will copy and run migrations for spree_enterprise and spree_multi_tenant gems.