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.
9 10 11 12 13 |
# File 'lib/authentication/authenticator.rb', line 9 def initialize() @session = .fetch :session @session_key = .fetch :session_key @finder = .fetch :finder end |
Instance Method Details
#current_client ⇒ Object Also known as: current_user
Return current_client. If it does not exist, returns nil.
28 29 30 |
# File 'lib/authentication/authenticator.rb', line 28 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.
37 38 39 |
# File 'lib/authentication/authenticator.rb', line 37 def current_client_id @session[@session_key] end |
#logged_in? ⇒ Boolean
Return current_client exists or not.
45 46 47 |
# File 'lib/authentication/authenticator.rb', line 45 def logged_in? not current_client.nil? end |
#login!(client) ⇒ Object
Set current_client.
18 19 20 21 22 |
# File 'lib/authentication/authenticator.rb', line 18 def login!(client) raise Unauthenticated unless client @current_client = client @session[@session_key] = client.id end |
#logout! ⇒ Object
Delete current_client from database and session.
51 52 53 54 |
# File 'lib/authentication/authenticator.rb', line 51 def logout! return unless current_client @current_client = @session[@session_key] = nil end |