You can use the official
solidus_auth_devise
gem
to provide a Spree::User model and basic authentication for Solidus. See its
documentation for additional setup instructions.
Stores require a User model in order to take advantage of all of Solidus's
features. This model can have any name, and Solidus can integrate with your
existing Rails application's existing User model.
By default, Solidus provides a
Spree::LegacyUser model
that
offers the bare minimum functionality of a user. The model is only suitable for
testing and should not be used in a production environment.
The rest of this article outlines the steps required should you decide to create
a User model from scratch, use an authentication solution like
Devise
, or integrate your application's existing User model.
Note that while your user model can have whatever name you like, this article
uses the model MyStore::User for its examples.
No matter what gem or extension you use for your store's User model, your
application's Spree.user_class value needs to be set. By default, Solidus sets
the Spree.user_class to Spree::LegacyUser.
You can configure a custom Spree.user_class in your application's
/config/initializers/spree.rb file:
Spree.user_class = "MyStore::User"
If you use the solidus_auth_devise gem, your Spree.user_class is set to
Spree::User when you run the gem's solidus:auth:install generator.
Once you have set the Spree.user_class, you can start integrating with the
features that are associated with the user_class.
After you have created your custom User model and its corresponding database
table, you can use the spree:custom_user generator to generate Solidus's
required User table columns and some authentication helpers:
bundle exec rails generate spree:custom_user MyStore::User
Then, you can migrate your database to add the Solidus-specific User table
columns:
bundle exec rails db:migrate
If you use the spree:custom_user generator:
Spree.user_class is updated to your specified class.solidus_frontend and
solidus_backend views and are sent to the application controller, making it
available throughout your application.spree_current_user method is defined and is sent to the application
controller and the Spree::Api::BaseController, making it available
throughout your application.Solidus requires that your User model's database table includes at least the
following columns:
spree_api_key: A string with a user's API key. This should be limited to 48
characters.bill_address_id: An integer that provides the ID of the Spree::Address
that should be used as the current user's billing address.ship_address_id: An integer that provides the ID of the Spree::Address
that should be used as the current user's shipping address.These three columns are generated for you by the
spree:custom_user
generator
.
It also requires that you have a
spree_current_user
helper method.
Note that if you use the stock solidus_frontend or solidus_backend gems,
your user should also have a password column. You can set up a password column
however you see fit.
If you use the stock solidus_frontend or solidus_backend gems, you need to
provide a spree_current_user helper method. Because you likely need to
reference the current user throughout your application, we recommend adding it
to your application_controller.rb.
If you use an authentication gem that defines a current_user (like Devise),
you may want to just wrap current_user in a spree_current_user method:
class ApplicationController < ActionController::Base
...
helper_method :spree_current_user
def spree_current_user
current_user
end
end
This helper can be generated for you by the
spree:custom_user
generator
.
If you use the stock solidus_frontend or solidus_backend gems, you need to
provide authentication helpers so that users can sign up, log in, and log out.
Because you likely need to reference the current user throughout your
application, we recommend adding it to you application_controller.rb:
class ApplicationController < ActionController::Base
...
helper_method :spree_login_path
helper_method :spree_signup_path
helper_method :spree_logout_path
def spree_login_path
login_path
end
def spree_signup_path
signup_path
end
def spree_logout_path
logout_path
end
end
These helpers can be generated for you by the
spree:custom_user
generator
.
The
Spree::UserMethods module
provides extensive
integration for a User model. User methods allow a User object to relate to
other major models in Solidus like Spree::Orders and Spree::StoreCredits.
To add user methods to your User model, include Spree::UserMethods in it:
module MyStore
class User
include Spree::UserMethods
...
In order for store administrators to use the solidus_backend admin, you need a
user with the Spree::Role of admin. You can give any existing user the
admin role like this:
user = MyStore::User.find_by(email: '[email protected]')
user.spree_roles << Spree::Role.find_or_create_by(name: 'admin')
Now, your user with the [email protected] email address should be able to
access the solidus_backend interface at mystore.com/admin.
Solidus is an open source platform supported by the community. We encourage everyone using Solidus to contribute back to the documentation and the code.
If you’re interested in contributing to the docs, get started with the contributing guidelines. If you see something that needs fixing and can’t do it yourself, please send us an email.