Class: Clearance::Session

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

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Session

Returns a new instance of Session.

Parameters:

  • env

    The current rack environment



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
# File 'lib/clearance/session.rb', line 17

def add_cookie_to_headers
  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

Returns:

  • (Boolean)


98
99
100
# File 'lib/clearance/session.rb', line 98

def authentication_successful?
  !!@current_user
end

#current_userUser?

The current user represented by this session.

Returns:



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.

Parameters:

Yield Parameters:



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/clearance/session.rb', line 48

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

  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_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.



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
  cookies.delete remember_token_cookie, delete_cookie_options
end

#signed_in?Boolean

True if #current_user is set.

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


91
92
93
# File 'lib/clearance/session.rb', line 91

def signed_out?
  ! signed_in?
end