Module: Spree::Core::ControllerHelpers::Auth

Extended by:
ActiveSupport::Concern
Includes:
TokenGenerator
Included in:
BaseController
Defined in:
lib/spree/core/controller_helpers/auth.rb

Instance Method Summary collapse

Methods included from TokenGenerator

#generate_token

Instance Method Details

#current_abilityObject

Needs to be overridden so that we use Spree’s Ability rather than anyone else’s.



19
20
21
# File 'lib/spree/core/controller_helpers/auth.rb', line 19

def current_ability
  @current_ability ||= Spree.ability_class.new(try_spree_current_user, { store: current_store })
end

#current_oauth_tokenObject



23
24
25
26
27
28
29
30
# File 'lib/spree/core/controller_helpers/auth.rb', line 23

def current_oauth_token
  get_last_access_token = ->(user) { Spree::OauthAccessToken.active_for(user).where(expires_in: nil).last }
  create_access_token = ->(user) { Spree::OauthAccessToken.create!(resource_owner: user) }
  user = try_spree_current_user
  return unless user

  @current_oauth_token ||= get_last_access_token.call(user) || create_access_token.call(user)
end

#store_location(location = nil) ⇒ Object

this will work for devise out of the box for other auth systems you will need to override this method



34
35
36
37
38
39
40
41
# File 'lib/spree/core/controller_helpers/auth.rb', line 34

def store_location(location = nil)
  return if try_spree_current_user

  location ||= request.fullpath
  session_key = store_location_session_key

  session[session_key] = location
end

#store_location_session_keyObject



43
44
45
# File 'lib/spree/core/controller_helpers/auth.rb', line 43

def store_location_session_key
  "#{Spree.user_class.model_name.singular_route_key.to_sym}_return_to"
end

#try_spree_current_userObject

proxy method to possible spree_current_user method Authentication extensions (such as spree_auth_devise) are meant to provide spree_current_user



49
50
51
52
53
54
55
56
57
58
# File 'lib/spree/core/controller_helpers/auth.rb', line 49

def try_spree_current_user
  # This one will be defined by apps looking to hook into Spree
  # As per authentication_helpers.rb
  if respond_to?(:spree_current_user)
    spree_current_user
  # This one will be defined by Devise
  elsif respond_to?(:current_spree_user)
    current_spree_user
  end
end