Class: Clearance::Session
- Inherits:
-
Object
- Object
- Clearance::Session
- Defined in:
- lib/clearance/session.rb
Overview
Represents a clearance session, ultimately persisted in
request.env[:clearance]
by RackSession.
Instance Method Summary collapse
-
#add_cookie_to_headers ⇒ void
Called by RackSession to add the Clearance session cookie to a response.
-
#authentication_successful? ⇒ Boolean
True if a successful authentication has been performed.
-
#current_user ⇒ User?
The current user represented by this session.
-
#initialize(env) ⇒ Session
constructor
A new instance of Session.
-
#sign_in(user) {|status| ... } ⇒ void
Sign the provided user in, if approved by the configured sign in guards.
-
#sign_out ⇒ void
Invalidates the users remember token and removes the remember token cookie from the store.
-
#signed_in? ⇒ Boolean
True if #current_user is set.
-
#signed_out? ⇒ Boolean
True if #current_user is not set.
Constructor Details
#initialize(env) ⇒ Session
Returns a new instance of Session.
8 9 10 11 12 |
# File 'lib/clearance/session.rb', line 8 def initialize(env) @env = env @current_user = nil @cookies = nil end |
Instance Method Details
#add_cookie_to_headers ⇒ void
This method returns an undefined value.
Called by RackSession to add the Clearance session cookie to a response.
17 18 19 20 21 |
# File 'lib/clearance/session.rb', line 17 def if signed_in_with_remember_token? set_remember_token(current_user.remember_token) end end |
#authentication_successful? ⇒ Boolean
True if a successful authentication has been performed
98 99 100 |
# File 'lib/clearance/session.rb', line 98 def authentication_successful? !!@current_user end |
#current_user ⇒ User?
The current user represented by this session.
26 27 28 29 30 31 32 |
# File 'lib/clearance/session.rb', line 26 def current_user if remember_token.present? @current_user ||= user_from_remember_token(remember_token) end @current_user end |
#sign_in(user) {|status| ... } ⇒ void
This method returns an undefined value.
Sign the provided user in, if approved by the configured sign in guards. If the sign in guard stack returns Clearance::SuccessStatus, the #current_user will be set and then remember token cookie will be set to the user's remember token. If the stack returns FailureStatus, #current_user will be nil.
In either event, the resulting status will be yielded to a provided block, if provided. See Clearance::SessionsController#create for an example of how this can be used.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/clearance/session.rb', line 48 def sign_in(user, &block) @current_user = user status = run_sign_in_stack if status.success? # Sign in succeeded, and when {RackSession} is run and calls # {#add_cookie_to_headers} it will set the cookie with the # remember_token for the current_user else @current_user = nil end if block_given? block.call(status) end end |
#sign_out ⇒ void
This method returns an undefined value.
Invalidates the users remember token and removes the remember token cookie from the store. The invalidation of the remember token causes any other sessions that are signed in from other locations to also be invalidated on their next request. This is because all Clearance sessions for a given user share a remember token.
72 73 74 75 76 77 78 79 |
# File 'lib/clearance/session.rb', line 72 def sign_out if signed_in? current_user.reset_remember_token! end @current_user = nil .delete , end |
#signed_in? ⇒ Boolean
True if #current_user is set.
84 85 86 |
# File 'lib/clearance/session.rb', line 84 def signed_in? current_user.present? end |
#signed_out? ⇒ Boolean
True if #current_user is not set
91 92 93 |
# File 'lib/clearance/session.rb', line 91 def signed_out? ! signed_in? end |