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 ab0fd927..20d3ea06 100644 --- a/app/models/spree/user.rb +++ b/app/models/spree/user.rb @@ -7,6 +7,8 @@ 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? + 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 653f25bb..33318dca 100644 --- a/lib/spree/auth_configuration.rb +++ b/lib/spree/auth_configuration.rb @@ -5,6 +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 :lockable, :boolean, default: false + preference :omniauthable, :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 c3e6e544..2f3c7991 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -100,4 +100,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