Module: DeviseToken::Controllers::Helpers

Extended by:
ActiveSupport::Concern
Included in:
DeviseToken::Concerns::AuthenticateToken
Defined in:
lib/devise_token/controllers/helpers.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Class Method Details

.define_helpers(mapping) ⇒ Object

Define authentication filters and accessor helpers based on mappings. These filters should be used inside the controllers as before_actions, so you can control the scope of the user who should be signed in to access that specific controller/action. Example:

Roles:
  User
  Admin

Generated methods:
  authenticate_user!                   # Signs user in or 401
  authenticate_admin!                  # Signs admin in or 401
  user_signed_in?                      # Checks whether there is a user signed in or not
  admin_signed_in?                     # Checks whether there is an admin signed in or not
  current_user                         # Current signed in user
  current_admin                        # Current signed in admin
  render_authenticate_error            # Render error unless user or admin is signed in

Use:
  before_action :authenticate_user!  # Tell devise to use :user map
  before_action :authenticate_admin! # Tell devise to use :admin map


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/devise_token/controllers/helpers.rb', line 37

def self.define_helpers(mapping) #:nodoc:
  mapping = mapping.name

  class_eval <<-METHODS, __FILE__, __LINE__ + 1
    def authenticate_#{mapping}!(opts={})
      unless current_#{mapping}
        render_authenticate_error
      end
    end

    def #{mapping}_signed_in?
      !!current_#{mapping}
    end

    def current_#{mapping}
      @current_#{mapping} ||= authenticate_token(:#{mapping})
    end

    def render_authenticate_error
      return render json: {
        errors: [I18n.t('devise.failure.unauthenticated')]
      }, status: 401
    end
  METHODS

  ActiveSupport.on_load(:action_controller) do
    if respond_to?(:helper_method)
      helper_method "current_#{mapping}", "#{mapping}_signed_in?", "render_authenticate_error"
    end
  end
end