• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mgmodell / devise_token_auth_multi_email / #663

17 Mar 2026 01:12AM UTC coverage: 12.22% (-78.4%) from 90.649%
#663

push

mgmodell
switching back to mult-email

202 of 1653 relevant lines covered (12.22%)

0.39 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.47
/lib/devise_token_auth/controllers/helpers.rb
1
# frozen_string_literal: true
2

3
module DeviseTokenAuth
1✔
4
  module Controllers
1✔
5
    module Helpers
1✔
6
      extend ActiveSupport::Concern
1✔
7

8
      module ClassMethods
1✔
9
        # Define authentication filters and accessor helpers for a group of mappings.
10
        # These methods are useful when you are working with multiple mappings that
11
        # share some functionality. They are pretty much the same as the ones
12
        # defined for normal mappings.
13
        #
14
        # Example:
15
        #
16
        #   inside BlogsController (or any other controller, it doesn't matter which):
17
        #     devise_group :blogger, contains: [:user, :admin]
18
        #
19
        #   Generated methods:
20
        #     authenticate_blogger!             # Redirects unless user or admin are signed in
21
        #     blogger_signed_in?                # Checks whether there is either a user or an admin signed in
22
        #     current_blogger                   # Currently signed in user or admin
23
        #     current_bloggers                  # Currently signed in user and admin
24
        #     render_authenticate_error         # Render error unless user or admin are signed in
25
        #
26
        #   Use:
27
        #     before_action :authenticate_blogger!              # Redirects unless either a user or an admin are authenticated
28
        #     before_action ->{ authenticate_blogger! :admin }  # Redirects to the admin login page
29
        #     current_blogger :user                             # Preferably returns a User if one is signed in
30
        #
31
        def devise_token_auth_group(group_name, opts = {})
1✔
32
          mappings = "[#{opts[:contains].map { |m| ":#{m}" }.join(',')}]"
×
33

34
          class_eval <<-METHODS, __FILE__, __LINE__ + 1
×
35
            def authenticate_#{group_name}!(favourite=nil, opts={})
36
              unless #{group_name}_signed_in?
37
                unless current_#{group_name}
38
                  render_authenticate_error
39
                end
40
              end
41
            end
42

43
            def #{group_name}_signed_in?
44
              !!current_#{group_name}
45
            end
46

47
            def current_#{group_name}(favourite=nil)
48
              @current_#{group_name} ||= set_group_user_by_token(favourite)
49
            end
50
            
51
            def set_group_user_by_token(favourite)
52
              mappings = #{mappings}
53
              mappings.unshift mappings.delete(favourite.to_sym) if favourite
54
              mappings.each do |mapping|
55
                current = set_user_by_token(mapping)
56
                return current if current
57
              end
58
              nil
59
            end
60

61
            def current_#{group_name.to_s.pluralize}
62
              #{mappings}.map do |mapping|
63
                set_user_by_token(mapping)
64
              end.compact
65
            end
66

67
            def render_authenticate_error
68
              return render json: {
69
                errors: [I18n.t('devise.failure.unauthenticated')]
70
              }, status: 401
71
            end
72

73
            if respond_to?(:helper_method)
74
              helper_method(
75
                "current_#{group_name}",
76
                "current_#{group_name.to_s.pluralize}",
77
                "#{group_name}_signed_in?",
78
                "render_authenticate_error"
79
              )
80
            end
81
          METHODS
82
        end
83

84
        def log_process_action(payload)
1✔
85
          payload[:status] ||= 401 unless payload[:exception]
×
86
          super
×
87
        end
88
      end
89

90
      # Define authentication filters and accessor helpers based on mappings.
91
      # These filters should be used inside the controllers as before_actions,
92
      # so you can control the scope of the user who should be signed in to
93
      # access that specific controller/action.
94
      # Example:
95
      #
96
      #   Roles:
97
      #     User
98
      #     Admin
99
      #
100
      #   Generated methods:
101
      #     authenticate_user!                   # Signs user in or 401
102
      #     authenticate_admin!                  # Signs admin in or 401
103
      #     user_signed_in?                      # Checks whether there is a user signed in or not
104
      #     admin_signed_in?                     # Checks whether there is an admin signed in or not
105
      #     current_user                         # Current signed in user
106
      #     current_admin                        # Current signed in admin
107
      #     user_session                         # Session data available only to the user scope
108
      #     admin_session                        # Session data available only to the admin scope
109
      #     render_authenticate_error            # Render error unless user or admin is signed in
110
      #
111
      #   Use:
112
      #     before_action :authenticate_user!  # Tell devise to use :user map
113
      #     before_action :authenticate_admin! # Tell devise to use :admin map
114
      #
115
      def self.define_helpers(mapping) #:nodoc:
1✔
116
        mapping = mapping.name
9✔
117

118
        class_eval <<-METHODS, __FILE__, __LINE__ + 1
9✔
119
          def authenticate_#{mapping}!(opts={})
120
            unless current_#{mapping}
121
              render_authenticate_error
122
            end
123
          end
124

125
          def #{mapping}_signed_in?
126
            !!current_#{mapping}
127
          end
128

129
          def current_#{mapping}
130
            @current_#{mapping} ||= set_user_by_token(:#{mapping})
131
          end
132

133
          def #{mapping}_session
134
            current_#{mapping} && warden.session(:#{mapping})
135
          end
136

137
          def render_authenticate_error
138
            return render json: {
139
              errors: [I18n.t('devise.failure.unauthenticated')]
140
            }, status: 401
141
          end
142
        METHODS
143

144
        ActiveSupport.on_load(:action_controller) do
9✔
145
          if respond_to?(:helper_method)
9✔
146
            helper_method(
9✔
147
              "current_#{mapping}",
148
              "#{mapping}_signed_in?",
149
              "#{mapping}_session",
150
              'render_authenticate_error'
151
            )
152
          end
153
        end
154
      end
155
    end
156
  end
157
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc