Module: Touth::ActionControllerSupport::ClassMethods

Defined in:
lib/touth/action_controller_support.rb

Instance Method Summary collapse

Instance Method Details

#token_authentication_for(resource_name) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/touth/action_controller_support.rb', line 5

def token_authentication_for(resource_name)
  unless @_init_token_authenticator_hook
    prepend_before_action :set_token_authorized_resource!
    @_init_token_authenticator_hook = true
  end

  resource_name = Touth.get_resource_name resource_name
  callback_name = "authenticate_#{resource_name}!".to_sym

  unless method_defined? callback_name
    define_method "#{resource_name}_signed_in?" do
      !!Touth::Authenticator.current(resource_name)
    end

    define_method "current_#{resource_name}" do
      Touth::Authenticator.current resource_name
    end

    define_method callback_name do
      authenticate_token_for! resource_name
    end

    protected callback_name
    before_action callback_name
  end
end