From 2a0140e6a060c6b2fa6d903957a41d85b3c8c356 Mon Sep 17 00:00:00 2001 From: zernie Date: Wed, 24 Feb 2021 16:38:35 +0300 Subject: [PATCH 1/2] feat: add omniauthable config --- README.md | 13 +++++++++++++ app/models/spree/user.rb | 1 + lib/spree/auth_configuration.rb | 1 + spec/models/user_spec.rb | 10 ++++++++++ spec/support/omniauthable_helpers.rb | 11 +++++++++++ 5 files changed, 36 insertions(+) create mode 100644 spec/support/omniauthable_helpers.rb diff --git a/README.md b/README.md index 9196c0e9..99bf89ea 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,19 @@ Devise.setup do |config| end ``` +### Omniauthable + +To enable Devise's Omniauthable module, you must follow the [devise wiki](https://github.com/heartcombo/devise/wiki/OmniAuth:-Overview) with _just one exception_: +Instead of adding +```ruby +devise :omniauthable, omniauth_providers: %i[...] +``` +you should add this line to an initializer (typically `config/initializers/spree.rb`): + +```ruby +Spree::Auth::Config[:omniauthable_providers] = %i[...] +``` + Using in an existing application -------------------------------- diff --git a/app/models/spree/user.rb b/app/models/spree/user.rb index 3e21c52d..3e04efcf 100644 --- a/app/models/spree/user.rb +++ b/app/models/spree/user.rb @@ -9,6 +9,7 @@ class User < Spree::Base devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :encryptable devise :confirmable if Spree::Auth::Config[:confirmable] + devise :omniauthable, omniauth_providers: Spree::Auth::Config[:omniauthable] if Spree::Auth::Config[:omniauthable].present? acts_as_paranoid after_destroy :scramble_email_and_password diff --git a/lib/spree/auth_configuration.rb b/lib/spree/auth_configuration.rb index 653f25bb..5efd9232 100644 --- a/lib/spree/auth_configuration.rb +++ b/lib/spree/auth_configuration.rb @@ -5,6 +5,7 @@ class AuthConfiguration < Preferences::Configuration preference :registration_step, :boolean, default: true preference :signout_after_password_change, :boolean, default: true preference :confirmable, :boolean, default: false + preference :omniauthable_providers, :array, default: [] preference :draw_frontend_routes, :boolean, default: true preference :draw_backend_routes, :boolean, default: true end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index e3ab8936..effb4729 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -96,4 +96,14 @@ expect(Spree::User.ancestors).not_to include(Devise::Models::Confirmable) end end + + describe "omniauthable" do + it "loads Devise's :omniauthable module when :omniauthable is set", omniauthable: %[twitter] do + expect(Spree::User.ancestors).to include(Devise::Models::Omniauthable) + end + + it "does not load Devise's :omniauthable module when :omniauthable is nil", omniauthable: nil do + expect(Spree::User.ancestors).not_to include(Devise::Models::Omniauthable) + end + end end diff --git a/spec/support/omniauthable_helpers.rb b/spec/support/omniauthable_helpers.rb new file mode 100644 index 00000000..bf769aae --- /dev/null +++ b/spec/support/omniauthable_helpers.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +RSpec.configure do |config| + config.before do |example| + if example.metadata.key?(:omniauthable) + stub_spree_preferences(Spree::Auth::Config, omniauthable: example.metadata[:omniauthable]) + + # load File.expand_path('../../../app/models/spree/user.rb', __FILE__) + end + end +end From e252bc3ae88e79b09133d55e7912f4c57dc092c8 Mon Sep 17 00:00:00 2001 From: zernie Date: Wed, 2 Jun 2021 20:23:53 +0300 Subject: [PATCH 2/2] feat: add lockable setting --- app/models/spree/user.rb | 1 + lib/spree/auth_configuration.rb | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/app/models/spree/user.rb b/app/models/spree/user.rb index b8157612..20d3ea06 100644 --- a/app/models/spree/user.rb +++ b/app/models/spree/user.rb @@ -8,6 +8,7 @@ class User < Spree::Base :rememberable, :trackable, :validatable, :encryptable devise :confirmable if Spree::Auth::Config[:confirmable] devise :omniauthable, omniauth_providers: Spree::Auth::Config[:omniauthable] if Spree::Auth::Config[:omniauthable].present? + devise :lockable if Spree::Auth::Config[:lockable].present? if defined?(Spree::SoftDeletable) include Spree::SoftDeletable diff --git a/lib/spree/auth_configuration.rb b/lib/spree/auth_configuration.rb index 5efd9232..33318dca 100644 --- a/lib/spree/auth_configuration.rb +++ b/lib/spree/auth_configuration.rb @@ -5,7 +5,8 @@ class AuthConfiguration < Preferences::Configuration preference :registration_step, :boolean, default: true preference :signout_after_password_change, :boolean, default: true preference :confirmable, :boolean, default: false - preference :omniauthable_providers, :array, default: [] + preference :lockable, :boolean, default: false + preference :omniauthable, :array, default: [] preference :draw_frontend_routes, :boolean, default: true preference :draw_backend_routes, :boolean, default: true end