@@ -15,10 +15,11 @@ module ClassMethods
15
15
# devise_group :blogger, contains: [:user, :admin]
16
16
#
17
17
# Generated methods:
18
- # authenticate_blogger! # Redirects unless user or admin are signed in
19
- # blogger_signed_in? # Checks whether there is either a user or an admin signed in
20
- # current_blogger # Currently signed in user or admin
21
- # current_bloggers # Currently signed in user and admin
18
+ # authenticate_blogger! # Redirects unless user or admin are signed in
19
+ # blogger_signed_in? # Checks whether there is either a user or an admin signed in
20
+ # current_blogger # Currently signed in user or admin
21
+ # current_bloggers # Currently signed in user and admin
22
+ # render_authenticate_error # Render error unless user or admin are signed in
22
23
#
23
24
# Use:
24
25
# before_action :authenticate_blogger! # Redirects unless either a user or an admin are authenticated
@@ -38,9 +39,7 @@ def authenticate_#{group_name}!(favourite=nil, opts={})
38
39
end
39
40
40
41
unless current_#{ group_name }
41
- return render json: {
42
- errors: [I18n.t('devise.failure.unauthenticated')]
43
- }, status: 401
42
+ render_authenticate_error
44
43
end
45
44
end
46
45
end
@@ -67,8 +66,14 @@ def current_#{group_name.to_s.pluralize}
67
66
end.compact
68
67
end
69
68
69
+ def render_authenticate_error
70
+ return render json: {
71
+ errors: [I18n.t('devise.failure.unauthenticated')]
72
+ }, status: 401
73
+ end
74
+
70
75
if respond_to?(:helper_method)
71
- helper_method "current_#{ group_name } ", "current_#{ group_name . to_s . pluralize } ", "#{ group_name } _signed_in?"
76
+ helper_method "current_#{ group_name } ", "current_#{ group_name . to_s . pluralize } ", "#{ group_name } _signed_in?", "render_authenticate_error"
72
77
end
73
78
METHODS
74
79
end
@@ -90,14 +95,15 @@ def log_process_action(payload)
90
95
# Admin
91
96
#
92
97
# Generated methods:
93
- # authenticate_user! # Signs user in or 401
94
- # authenticate_admin! # Signs admin in or 401
95
- # user_signed_in? # Checks whether there is a user signed in or not
96
- # admin_signed_in? # Checks whether there is an admin signed in or not
97
- # current_user # Current signed in user
98
- # current_admin # Current signed in admin
99
- # user_session # Session data available only to the user scope
100
- # admin_session # Session data available only to the admin scope
98
+ # authenticate_user! # Signs user in or 401
99
+ # authenticate_admin! # Signs admin in or 401
100
+ # user_signed_in? # Checks whether there is a user signed in or not
101
+ # admin_signed_in? # Checks whether there is an admin signed in or not
102
+ # current_user # Current signed in user
103
+ # current_admin # Current signed in admin
104
+ # user_session # Session data available only to the user scope
105
+ # admin_session # Session data available only to the admin scope
106
+ # render_authenticate_error # Render error unless user or admin is signed in
101
107
#
102
108
# Use:
103
109
# before_action :authenticate_user! # Tell devise to use :user map
@@ -109,9 +115,7 @@ def self.define_helpers(mapping) #:nodoc:
109
115
class_eval <<-METHODS , __FILE__ , __LINE__ + 1
110
116
def authenticate_#{ mapping } !
111
117
unless current_#{ mapping }
112
- return render json: {
113
- errors: [I18n.t('devise.failure.unauthenticated')]
114
- }, status: 401
118
+ render_authenticate_error
115
119
end
116
120
end
117
121
@@ -126,11 +130,17 @@ def current_#{mapping}
126
130
def #{ mapping } _session
127
131
current_#{ mapping } && warden.session(:#{ mapping } )
128
132
end
133
+
134
+ def render_authenticate_error
135
+ return render json: {
136
+ errors: [I18n.t('devise.failure.unauthenticated')]
137
+ }, status: 401
138
+ end
129
139
METHODS
130
140
131
141
ActiveSupport . on_load ( :action_controller ) do
132
142
if respond_to? ( :helper_method )
133
- helper_method "current_#{ mapping } " , "#{ mapping } _signed_in?" , "#{ mapping } _session"
143
+ helper_method "current_#{ mapping } " , "#{ mapping } _signed_in?" , "#{ mapping } _session" , "render_authenticate_error"
134
144
end
135
145
end
136
146
end
0 commit comments