Skip to content

Commit

Permalink
Memorize current_#{group_name} to avoid error (lynndylanhurley#722)
Browse files Browse the repository at this point in the history
Currently current_#{group_name} (ex. current_member) is not memorized as current_#{mapping} (ex. current_user) is.
Instead, it calls set_user_by_token every time, and this could cause error if current_member is called after token is changed (maybe by some other request) and return nil
  • Loading branch information
Charlie-Hua authored Jun 20, 2020
1 parent 849c244 commit ae3547c
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions lib/devise_token_auth/controllers/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,21 @@ def devise_token_auth_group(group_name, opts = {})
class_eval <<-METHODS, __FILE__, __LINE__ + 1
def authenticate_#{group_name}!(favourite=nil, opts={})
unless #{group_name}_signed_in?
mappings = #{mappings}
mappings.unshift mappings.delete(favourite.to_sym) if favourite
mappings.each do |mapping|
set_user_by_token(mapping)
end
unless current_#{group_name}
render_authenticate_error
end
end
end
def #{group_name}_signed_in?
#{mappings}.any? do |mapping|
set_user_by_token(mapping)
end
!!current_#{group_name}
end
def current_#{group_name}(favourite=nil)
@current_#{group_name} ||= set_group_user_by_token(favourite)
end
def set_group_user_by_token(favourite)
mappings = #{mappings}
mappings.unshift mappings.delete(favourite.to_sym) if favourite
mappings.each do |mapping|
Expand Down

0 comments on commit ae3547c

Please sign in to comment.