Module: DeviseTokenAuth::Controllers::Helpers::ClassMethods

Defined in:
lib/devise_token_auth/controllers/helpers.rb

Instance Method Summary collapse

Instance Method Details

#devise_token_auth_group(group_name, opts = {}) ⇒ Object

Define authentication filters and accessor helpers for a group of mappings. These methods are useful when you are working with multiple mappings that share some functionality. They are pretty much the same as the ones defined for normal mappings.

Example:

inside BlogsController (or any other controller, it doesn't matter which):
  devise_group :blogger, contains: [:user, :admin]

Generated methods:
  authenticate_blogger!  # Redirects unless user or admin are signed in
  blogger_signed_in?     # Checks whether there is either a user or an admin signed in
  current_blogger        # Currently signed in user or admin
  current_bloggers       # Currently signed in user and admin

Use:
  before_action :authenticate_blogger!              # Redirects unless either a user or an admin are authenticated
  before_action ->{ authenticate_blogger! :admin }  # Redirects to the admin login page
  current_blogger :user                             # Preferably returns a User if one is signed in


28
29
30
31
32
33
34
35
36
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
68
69
70
71
72
73
74
# File 'lib/devise_token_auth/controllers/helpers.rb', line 28

def devise_token_auth_group(group_name, opts={})
  mappings = "[#{ opts[:contains].map { |m| ":#{m}" }.join(',') }]"

  class_eval "    def authenticate_\#{group_name}!(favourite=nil, opts={})\n      unless \#{group_name}_signed_in?\n        mappings = \#{mappings}\n        mappings.unshift mappings.delete(favourite.to_sym) if favourite\n        mappings.each do |mapping|\n          set_user_by_token(mapping)\n        end\n\n        unless current_\#{group_name}\n          return render json: {\n            errors: [\"Authorized users only.\"]\n          }, status: 401\n        end\n      end\n    end\n\n    def \#{group_name}_signed_in?\n      \#{mappings}.any? do |mapping|\n        set_user_by_token(mapping)\n      end\n    end\n\n    def current_\#{group_name}(favourite=nil)\n      mappings = \#{mappings}\n      mappings.unshift mappings.delete(favourite.to_sym) if favourite\n      mappings.each do |mapping|\n        current = set_user_by_token(mapping)\n        return current if current\n      end\n      nil\n    end\n\n    def current_\#{group_name.to_s.pluralize}\n      \#{mappings}.map do |mapping|\n        set_user_by_token(mapping)\n      end.compact\n    end\n\n    if respond_to?(:helper_method)\n      helper_method \"current_\#{group_name}\", \"current_\#{group_name.to_s.pluralize}\", \"\#{group_name}_signed_in?\"\n    end\n  METHODS\nend\n", __FILE__, __LINE__ + 1

#log_process_action(payload) ⇒ Object



76
77
78
79
# File 'lib/devise_token_auth/controllers/helpers.rb', line 76

def log_process_action(payload)
  payload[:status] ||= 401 unless payload[:exception]
  super
end