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::Dependencies.ability_class.constantize.new(try_spree_current_user, { store: current_store })
end

#current_oauth_tokenObject



38
39
40
41
42
43
44
45
# File 'lib/spree/core/controller_helpers/auth.rb', line 38

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

#redirect_back_or_default(default) ⇒ Object



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

def redirect_back_or_default(default)
  Spree::Deprecation.warn('redirect_back_or_default is deprecated and will be removed in Spree 5.2. Please use redirect_back(fallback_location: default) instead.')
  redirect_back(fallback_location: default)
end

#set_tokenObject



28
29
30
31
32
33
34
35
36
# File 'lib/spree/core/controller_helpers/auth.rb', line 28

def set_token
  Spree::Deprecation.warn('set_token is deprecated and will be removed in Spree 5.2. Please use create_token_cookie(token) instead.')
  cookies.permanent.signed[:token] ||= cookies.signed[:guest_token]
  cookies.permanent.signed[:token] ||= {
    value: generate_token,
    httponly: true
  }
  cookies.permanent.signed[:guest_token] ||= cookies.permanent.signed[:token]
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



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

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



58
59
60
# File 'lib/spree/core/controller_helpers/auth.rb', line 58

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



64
65
66
67
68
69
70
71
72
73
# File 'lib/spree/core/controller_helpers/auth.rb', line 64

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