Class: Clearance::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/clearance/session.rb

Overview

Represents a clearance session, ultimately persisted in request.env[:clearane] by RackSession.

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ 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

This method returns an undefined value.

Called by RackSession to add the Clearance session cookie to a response.


17
18
19
20
21
22
23
24
25
# File 'lib/clearance/session.rb', line 17

def add_cookie_to_headers(headers)
  if cookie_value[:value].present?
    Rack::Utils.set_cookie_header!(
      headers,
      remember_token_cookie,
      cookie_value
    )
  end
end

#current_userUser?

The current user represented by this session.


30
31
32
33
34
35
36
# File 'lib/clearance/session.rb', line 30

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.

Yield Parameters:


52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/clearance/session.rb', line 52

def (user, &block)
  @current_user = user
  status = 

  if status.success?
    cookies[remember_token_cookie] = user && user.remember_token
  else
    @current_user = nil
  end

  if block_given?
    block.call(status)
  end
end

#sign_outvoid

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.


74
75
76
77
78
79
80
81
# File 'lib/clearance/session.rb', line 74

def sign_out
  if signed_in?
    current_user.reset_remember_token!
  end

  @current_user = nil
  cookies.delete remember_token_cookie
end

#signed_in?Boolean

True if #current_user is set.


86
87
88
# File 'lib/clearance/session.rb', line 86

def signed_in?
  current_user.present?
end

#signed_out?Boolean

True if #current_user is not set


93
94
95
# File 'lib/clearance/session.rb', line 93

def signed_out?
  ! signed_in?
end