Module: Clowk::Authenticable

Extended by:
ActiveSupport::Concern
Included in:
BaseController
Defined in:
lib/clowk/authenticable.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.install_dynamic_methods(base) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/clowk/authenticable.rb', line 9

def self.install_dynamic_methods(base)
  scope = Clowk.config.prefix_by.to_s
  current_method = :"current_#{scope}"
  authenticate_method = :"authenticate_#{scope}!"
  signed_in_method = :"#{scope}_signed_in?"

  enforce_session_method = :"#{scope}_enforce_session!"

  base.class_eval do
    unless current_method == :clowk_current_resource
      define_method(current_method) do
        clowk_current_resource
      end
    end

    unless authenticate_method == :clowk_authenticate!
      define_method(authenticate_method) do
        clowk_authenticate!
      end
    end

    unless signed_in_method == :clowk_signed_in?
      define_method(signed_in_method) do
        clowk_signed_in?
      end
    end

    unless enforce_session_method == :clowk_enforce_session!
      define_method(enforce_session_method) do
        clowk_enforce_session!
      end
    end

    helper_method current_method, authenticate_method, signed_in_method, :current_token if respond_to?(:helper_method)
  end
end

Instance Method Details

#clowk_authenticate!Object



88
89
90
91
92
# File 'lib/clowk/authenticable.rb', line 88

def clowk_authenticate!
  return clowk_current_resource if clowk_signed_in?

  clowk_handle_unauthenticated
end

#clowk_current_resourceObject



50
51
52
53
54
55
# File 'lib/clowk/authenticable.rb', line 50

def clowk_current_resource
  @clowk_current_resource ||= begin
    payload = stored_user_payload || verified_request_payload
    payload ? Current.new(payload) : nil
  end
end

#clowk_enforce_session!Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/clowk/authenticable.rb', line 73

def clowk_enforce_session!
  return if clowk_session_active?

  session_info = clowk_session_status
  callback = Clowk.config.on_session_expired

  if callback.respond_to?(:call)
    callback.call(self, session_info)

    return
  end

  clowk_handle_expired_session(session_info)
end

#clowk_session_active?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/clowk/authenticable.rb', line 69

def clowk_session_active?
  clowk_session_status&.dig(:status) == "active"
end

#clowk_session_statusObject



65
66
67
# File 'lib/clowk/authenticable.rb', line 65

def clowk_session_status
  @clowk_session_status ||= resolve_session_status
end

#clowk_sign_out!Object



94
95
96
97
98
99
# File 'lib/clowk/authenticable.rb', line 94

def clowk_sign_out!
  session.delete(Clowk.config.session_key)
  cookies.delete(Clowk.config.cookie_key)

  @clowk_current_resource = nil
end

#clowk_signed_in?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/clowk/authenticable.rb', line 61

def clowk_signed_in?
  clowk_current_resource.present?
end

#current_tokenObject



57
58
59
# File 'lib/clowk/authenticable.rb', line 57

def current_token
  stored_session&.dig("token") || extracted_token
end