Commit 621071ca authored by Grzegorz Bizon's avatar Grzegorz Bizon

Refactor and simplify token authenticatable concern

parent 3ee8e019
...@@ -9,21 +9,18 @@ module TokenAuthenticatable ...@@ -9,21 +9,18 @@ module TokenAuthenticatable
private # rubocop:disable Lint/UselessAccessModifier private # rubocop:disable Lint/UselessAccessModifier
def add_authentication_token_field(token_field, options = {}) def add_authentication_token_field(token_field, options = {})
@token_fields ||= [] if token_authenticatable_fields.include?(token_field)
unique = options.fetch(:unique, true)
if @token_fields.include?(token_field)
raise ArgumentError.new("#{token_field} already configured via add_authentication_token_field") raise ArgumentError.new("#{token_field} already configured via add_authentication_token_field")
end end
@token_fields << token_field token_authenticatable_fields.push(token_field)
attr_accessor :cleartext_tokens attr_accessor :cleartext_tokens
strategy = TokenAuthenticatableStrategies::Base strategy = TokenAuthenticatableStrategies::Base
.fabricate(self, token_field, options) .fabricate(self, token_field, options)
if unique if options.fetch(:unique, true)
define_singleton_method("find_by_#{token_field}") do |token| define_singleton_method("find_by_#{token_field}") do |token|
strategy.find_token_authenticatable(token) strategy.find_token_authenticatable(token)
end end
...@@ -51,5 +48,9 @@ module TokenAuthenticatable ...@@ -51,5 +48,9 @@ module TokenAuthenticatable
strategy.reset_token!(self) strategy.reset_token!(self)
end end
end end
def token_authenticatable_fields
@token_authenticatable_fields ||= []
end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment