Class: Authentication::Authenticator
- Inherits:
-
Object
- Object
- Authentication::Authenticator
- Defined in:
- lib/authentication/authenticator.rb
Overview
A object to authenticate client.
Instance Method Summary collapse
-
#current_client ⇒ Object
(also: #current_user)
Return current_client.
-
#current_client_id ⇒ Object
(also: #current_user_id)
Return id of given current_client.
-
#initialize(options) ⇒ Authenticator
constructor
A new instance of Authenticator.
-
#logged_in? ⇒ Boolean
Return current_client exists or not.
-
#login!(client) ⇒ Object
Set current_client.
-
#logout! ⇒ Object
Delete current_client from database and session.
Constructor Details
#initialize(options) ⇒ Authenticator
Returns a new instance of Authenticator.
11 12 13 14 15 16 17 |
# File 'lib/authentication/authenticator.rb', line 11 def initialize() @session = .fetch :session @session_key = .fetch :session_key @finder = .fetch :finder @after_login_callback = [:after_login_callback] @after_logout_callback = [:after_logout_callback] end |
Instance Method Details
#current_client ⇒ Object Also known as: current_user
Return current_client. If it does not exist, returns nil.
33 34 35 |
# File 'lib/authentication/authenticator.rb', line 33 def current_client @current_client ||= @finder.call end |
#current_client_id ⇒ Object Also known as: current_user_id
Return id of given current_client. If it does not exist, returns nil.
42 43 44 |
# File 'lib/authentication/authenticator.rb', line 42 def current_client_id @session[@session_key] end |
#logged_in? ⇒ Boolean
Return current_client exists or not.
50 51 52 |
# File 'lib/authentication/authenticator.rb', line 50 def logged_in? not current_client.nil? end |
#login!(client) ⇒ Object
Set current_client.
22 23 24 25 26 27 |
# File 'lib/authentication/authenticator.rb', line 22 def login!(client) raise Unauthenticated unless client @current_client = client @session[@session_key] = client.id invoke_after_login_callback! end |
#logout! ⇒ Object
Delete current_client from database and session.
56 57 58 59 60 |
# File 'lib/authentication/authenticator.rb', line 56 def logout! return unless current_client @current_client = @session[@session_key] = nil invoke_after_logout_callback! end |