Module: ReviseAuth::Authentication
- Extended by:
- ActiveSupport::Concern
- Included in:
- Api::BaseController, RouteConstraint, ReviseAuthController
- Defined in:
- lib/revise_auth/authentication.rb
Instance Method Summary collapse
-
#authenticate_user ⇒ Object
Authenticates the current user - from session cookie - (future) from Authorization header.
-
#authenticate_user! ⇒ Object
Authenticates a user or redirects to the login page.
-
#authenticated_user_from_session ⇒ Object
Returns a user from session cookie.
-
#current_user ⇒ Object
Authenticates the user if not already authenticated Returns a User or nil.
-
#login(user) ⇒ Object
Logs in the user - Set Current.user for the current request - Save a session cookie so the next request is authenticated.
- #logout ⇒ Object
- #reset_session ⇒ Object
-
#user_signed_in? ⇒ Boolean
Returns a boolean whether the user is signed in or not.
Instance Method Details
#authenticate_user ⇒ Object
Authenticates the current user
- from session cookie
- (future) from Authorization header
31 32 33 |
# File 'lib/revise_auth/authentication.rb', line 31 def authenticate_user Current.user = authenticated_user_from_session end |
#authenticate_user! ⇒ Object
Authenticates a user or redirects to the login page
24 25 26 |
# File 'lib/revise_auth/authentication.rb', line 24 def authenticate_user! redirect_to login_path unless user_signed_in? end |
#authenticated_user_from_session ⇒ Object
Returns a user from session cookie
36 37 38 39 40 |
# File 'lib/revise_auth/authentication.rb', line 36 def authenticated_user_from_session user_id = session[:user_id] return unless user_id User.find_by(id: user_id) end |
#current_user ⇒ Object
Authenticates the user if not already authenticated Returns a User or nil
19 20 21 |
# File 'lib/revise_auth/authentication.rb', line 19 def current_user Current.user ||= authenticate_user end |
#login(user) ⇒ Object
Logs in the user
- Set Current.user for the current request
- Save a session cookie so the next request is authenticated
45 46 47 48 49 |
# File 'lib/revise_auth/authentication.rb', line 45 def login(user) Current.user = user reset_session session[:user_id] = user.id end |
#logout ⇒ Object
51 52 53 54 |
# File 'lib/revise_auth/authentication.rb', line 51 def logout Current.user = nil reset_session end |
#reset_session ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/revise_auth/authentication.rb', line 56 def reset_session if session&.respond_to?(:destroy) session.destroy else self.session = {} end end |
#user_signed_in? ⇒ Boolean
Returns a boolean whether the user is signed in or not
13 14 15 |
# File 'lib/revise_auth/authentication.rb', line 13 def user_signed_in? !!current_user end |