Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fix custom authentication generators and update docs (#12044)

Damian Legawiec1ccfa8f3682d
diff --git a/core/lib/generators/spree/custom_authentication/custom_authentication_generator.rb b/core/lib/generators/spree/custom_authentication/custom_authentication_generator.rb
new file mode 100644
index 0000000000..3d2eb72f03
--- /dev/null
+++ b/core/lib/generators/spree/custom_authentication/custom_authentication_generator.rb
@@ -0,0 +1,23 @@
+module Spree
+ class CustomAuthenticationGenerator < Rails::Generators::Base
+ desc 'Set up a Spree installation with a custom authentication helpers'
+
+ def self.source_paths
+ paths = superclass.source_paths
+ paths << File.expand_path('templates', __dir__)
+ paths.flatten
+ end
+
+ def generate
+ template 'authentication_helpers.rb.tt', 'lib/spree/authentication_helpers.rb'
+
+ file_action = File.exist?('config/initializers/spree.rb') ? :append_file : :create_file
+ send(file_action, 'config/initializers/spree.rb') do
+ %Q{
+ Rails.application.config.to_prepare do
+ require_dependency 'spree/authentication_helpers'
+ end\n}
+ end
+ end
+ end
+end
diff --git a/core/lib/generators/spree/custom_user/templates/authentication_helpers.rb.tt b/core/lib/generators/spree/custom_authentication/templates/authentication_helpers.rb.tt
similarity index 54%
rename from core/lib/generators/spree/custom_user/templates/authentication_helpers.rb.tt
rename to core/lib/generators/spree/custom_authentication/templates/authentication_helpers.rb.tt
index 6f301aa157..acfaee5a26 100644
--- a/core/lib/generators/spree/custom_user/templates/authentication_helpers.rb.tt
+++ b/core/lib/generators/spree/custom_authentication/templates/authentication_helpers.rb.tt
@@ -1,14 +1,4 @@
module Spree
- module CurrentUserHelpers
- def self.included(receiver)
- receiver.send :helper_method, :spree_current_user
- end
-
- def spree_current_user
- current_user
- end
- end
-
module AuthenticationHelpers
def self.included(receiver)
receiver.send :helper_method, :spree_login_path
@@ -30,7 +20,4 @@ module Spree
end
end
-ApplicationController.include Spree::AuthenticationHelpers
-ApplicationController.include Spree::CurrentUserHelpers
-
-Spree::Api::BaseController.include Spree::CurrentUserHelpers
+ApplicationController.include Spree::AuthenticationHelpers if defined?(ApplicationController)
diff --git a/core/lib/generators/spree/custom_user/custom_user_generator.rb b/core/lib/generators/spree/custom_user/custom_user_generator.rb
index fa8c8089f5..402bbc6f35 100644
--- a/core/lib/generators/spree/custom_user/custom_user_generator.rb
+++ b/core/lib/generators/spree/custom_user/custom_user_generator.rb
@@ -20,24 +20,19 @@ module Spree
def generate
migration_template 'migration.rb.tt', 'db/migrate/add_spree_fields_to_custom_user_table.rb'
- template 'authentication_helpers.rb.tt', 'lib/spree/authentication_helpers.rb'
+ template 'current_user_helpers.rb.tt', 'lib/spree/current_user_helpers.rb'
file_action = File.exist?('config/initializers/spree.rb') ? :append_file : :create_file
send(file_action, 'config/initializers/spree.rb') do
%Q{
Rails.application.config.to_prepare do
- require_dependency 'spree/authentication_helpers'
+ require_dependency 'spree/current_user_helpers'
end\n}
end
end
def self.next_migration_number(dirname)
- if ApplicationRecord.timestamped_migrations
- sleep 1 # make sure to get a different migration every time
- Time.new.utc.strftime('%Y%m%d%H%M%S')
- else
- format('%.3d', (current_migration_number(dirname) + 1))
- end
+ format('%.3d', (current_migration_number(dirname) + 1))
end
def klass
diff --git a/core/lib/generators/spree/custom_user/templates/current_user_helpers.rb.tt b/core/lib/generators/spree/custom_user/templates/current_user_helpers.rb.tt
new file mode 100644
index 0000000000..b3fca885b7
--- /dev/null
+++ b/core/lib/generators/spree/custom_user/templates/current_user_helpers.rb.tt
@@ -0,0 +1,15 @@
+module Spree
+ module CurrentUserHelpers
+ def self.included(receiver)
+ receiver.send :helper_method, :spree_current_user
+ end
+
+ def spree_current_user
+ current_user
+ end
+ end
+end
+
+
+ApplicationController.include Spree::CurrentUserHelpers if defined?(ApplicationController)
+Spree::Api::BaseController.include Spree::CurrentUserHelpers if defined?(Spree::Api::BaseController)
diff --git a/core/lib/generators/spree/custom_user/templates/migration.rb.tt b/core/lib/generators/spree/custom_user/templates/migration.rb.tt
index b21b1ee52c..b94abe38ee 100644
--- a/core/lib/generators/spree/custom_user/templates/migration.rb.tt
+++ b/core/lib/generators/spree/custom_user/templates/migration.rb.tt
@@ -1,7 +1,7 @@
class AddSpreeFieldsToCustomUserTable < ActiveRecord::Migration[4.2]
def up
add_column <%= table_name.inspect %>, :spree_api_key, :string, limit: 48
- add_column <%= table_name.inspect %>, :ship_address_id, :integer
- add_column <%= table_name.inspect %>, :bill_address_id, :integer
+ add_column <%= table_name.inspect %>, :ship_address_id, :bigint
+ add_column <%= table_name.inspect %>, :bill_address_id, :bigint
end
end
diff --git a/docs/developer/customization/authentication.mdx b/docs/developer/customization/authentication.mdx
index 72f85ca772..3d7dee10dc 100644
--- a/docs/developer/customization/authentication.mdx
+++ b/docs/developer/customization/authentication.mdx
@@ -28,8 +28,8 @@ To use your own authentication system with Spree, please follow these steps:
```
</Accordion>
- <Accordion title="2. Generate Spree Authentication Helpers">
- Next, you need to run the custom user generator for Spree which will create two files. The first is a migration that will add the necessary Spree fields to your users table, and the second is an extension that lives at `lib/spree/authentication_helpers.rb` to the `Spree::Core::AuthenticationHelpers` module inside of Spree.
+ <Accordion title="2. Update User model">
+ Next, you need to run the custom user generator for Spree which will create a migration that will add the necessary Spree fields to your users table.
Run this generator with this command:
@@ -43,10 +43,50 @@ To use your own authentication system with Spree, please follow these steps:
bin/rails db:migrate
```
- Next you will need to define some methods to tell Spree where to find your application's authentication routes.
+ This generator will also create a file at `lib/spree/current_user_helpers.rb` which will be automatically included in your application's controllers allowing you to override the `spree_current_user` method to return the current user of the request.
</Accordion>
- <Accordion title="3. Customize Spree Authentication Helpers">
- There are some authentication helpers of Spree's that you will need to possibly override. The file at `lib/spree/authentication_helpers.rb` contains the following code to help you do that:
+ <Accordion title="3. Include User concerns in your User model">
+ In your User Model please include the following lines:
+
+ ```ruby
+ include Spree::UserMethods
+ include Spree::UserAddress
+ include Spree::UserPaymentSource
+ ```
+
+ The first of these methods are the ones added for the `has_and_belongs_to_many` association called `spree_roles`. This association will retrieve all the roles that a user has for Spree.
+
+ The second of these is the `spree_orders` association. This will return all orders associated with the user in Spree. There's also a `last_incomplete_spree_order` method which will return the last incomplete spree order for the user. This is used internal to Spree to persist order data across a user's login sessions.
+
+ The third and fourth associations are for address information for a user. When a user places an order, the address information for that order will be linked to that user so that it is available for subsequent orders.
+
+ The next method is one called `has_spree_role?` which can be used to check if a user has a specific role. This method is used internally to Spree to check if the user is authorized to perform specific actions, such as accessing the admin section. Admin users of your system should be assigned the Spree admin role, like this:
+
+ ```ruby
+ user = User.find_by(email: '[email protected]')
+ user.spree_roles << Spree::Role.where(name: 'admin').first_or_create
+ ```
+
+ To test that this has worked, use the `has_spree_role?` method, like this:
+
+ ```ruby
+ user.has_spree_role?('admin')
+ ```
+
+ If this returns `true`, then the user has admin permissions within Spree.
+ </Accordion>
+ <Accordion title="4. Customize Spree Authentication Helpers (Optional)">
+ <Note>
+ This step is required if you're using `spree_backend` and/or `spree_frontend` gems.
+ </Note>
+
+ Run a generator to create a file at `lib/spree/authentication_helpers.rb` which will contain the following code to help you do that:
+
+ ```bash
+ bin/rails g spree:custom_authentication
+ ```
+
+ This will create a file at `lib/spree/authentication_helpers.rb` which will contain the following code to help you do that:
```ruby
module Spree
@@ -77,19 +117,6 @@ To use your own authentication system with Spree, please follow these steps:
end
```
- In your `ApplicationController` add these lines:
-
- ```ruby
- include Spree::AuthenticationHelpers
- include Spree::Core::ControllerHelpers::Auth
- include Spree::Core::ControllerHelpers::Order
- include Spree::Core::ControllerHelpers::Store
- include Spree::Core::ControllerHelpers::Currency
- include Spree::Core::ControllerHelpers::Locale
-
- helper 'spree/base', 'spree/locale', 'spree/currency', 'spree/store'
- ```
-
Each of the methods defined in this module return values that are the most common in Rails applications today, but you may need to customize them. In order, they are:
* `spree_current_user` Used to tell Spree what the current user of a request is.
@@ -116,36 +143,6 @@ To use your own authentication system with Spree, please follow these steps:
Any modifications made to `lib/spree/authentication_helpers.rb` while the server is running will require a restart, as with any other modification to other files in `lib`.
</Accordion>
- <Accordion title="4. Include Spree::UserMethods in your User model">
- In your User Model you have to add:
-
- ```ruby
- include Spree::UserMethods
- include Spree::UserAddress
- include Spree::UserPaymentSource
- ```
-
- The first of these methods are the ones added for the `has_and_belongs_to_many` association called `spree_roles`. This association will retrieve all the roles that a user has for Spree.
-
- The second of these is the `spree_orders` association. This will return all orders associated with the user in Spree. There's also a `last_incomplete_spree_order` method which will return the last incomplete spree order for the user. This is used internal to Spree to persist order data across a user's login sessions.
-
- The third and fourth associations are for address information for a user. When a user places an order, the address information for that order will be linked to that user so that it is available for subsequent orders.
-
- The next method is one called `has_spree_role?` which can be used to check if a user has a specific role. This method is used internally to Spree to check if the user is authorized to perform specific actions, such as accessing the admin section. Admin users of your system should be assigned the Spree admin role, like this:
-
- ```ruby
- user = User.find_by(email: '[email protected]')
- user.spree_roles << Spree::Role.where(name: 'admin').first_or_create
- ```
-
- To test that this has worked, use the `has_spree_role?` method, like this:
-
- ```ruby
- user.has_spree_role?('admin')
- ```
-
- If this returns `true`, then the user has admin permissions within Spree.
- </Accordion>
<Accordion title="5. Remove Auth Devise gem">
The `spree_auth_devise` gem is not needed when using an existing application authentication unless the goal is to have two separate authentication methods.