Module: Knocknock::Authenticatable

Defined in:
lib/knocknock/authenticatable.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (private)



18
19
20
21
22
23
24
25
26
# File 'lib/knocknock/authenticatable.rb', line 18

def method_missing(method, *args)
  prefix, *parts = method.to_s.split('_')
  if prefix == 'authenticate'
    resource_class = constant_from_parts(parts)
    send(:authenticate_for, resource_class)
  else
    super
  end
end

Instance Method Details

#authenticate_for(resource_class) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/knocknock/authenticatable.rb', line 3

def authenticate_for resource_class
  token = token_from_request_headers
  return head(:unauthorized) if token.nil?

  begin
    @resource = Knocknock::AuthToken.new(token: token).resource(resource_class)
    return head(:unauthorized) unless @resource
    define_current_resource_getter(resource_class)
  rescue
    return head(:unauthorized)
  end
end