Module: Clearance::Authentication

Extended by:
ActiveSupport::Concern
Defined in:
lib/clearance/authentication.rb

Instance Method Summary collapse

Instance Method Details

#authenticate(params) ⇒ Object

Find the user by the given params or return nil. By default, uses email and password. Redefine this method and User.authenticate for other mechanisms such as username and password.

Examples:

@user = authenticate(params)


74
75
76
77
# File 'lib/clearance/authentication.rb', line 74

def authenticate(params)
  ::User.authenticate(params[:session][:email],
                      params[:session][:password])
end

#authorizeObject

Deny the user access if they are signed out.

Examples:

before_filter :authorize


83
84
85
# File 'lib/clearance/authentication.rb', line 83

def authorize
  deny_access unless signed_in?
end

#current_userUser?

User in the current cookie

Returns:



16
17
18
# File 'lib/clearance/authentication.rb', line 16

def current_user
  @_current_user ||= user_from_cookie
end

#current_user=(user) ⇒ Object

Set the current user

Parameters:



23
24
25
# File 'lib/clearance/authentication.rb', line 23

def current_user=(user)
  @_current_user = user
end

#deny_access(flash_message = nil) ⇒ Object

Store the current location and redirect to sign in. Display a failure flash message if included.

Parameters:

  • optional (String)

    flash message to display to denied user



91
92
93
94
95
96
97
98
99
# File 'lib/clearance/authentication.rb', line 91

def deny_access(flash_message = nil)
  store_location
  flash[:notice] = flash_message if flash_message
  if signed_in?
    redirect_to(url_after_denied_access_when_signed_in)
  else
    redirect_to(url_after_denied_access_when_signed_out)
  end
end

#handle_unverified_requestObject



103
104
105
106
# File 'lib/clearance/authentication.rb', line 103

def handle_unverified_request
  super
  sign_out
end

#sign_in(user) ⇒ Object

Sign user in to cookie.

Examples:

(@user)

Parameters:



47
48
49
50
51
52
53
54
55
# File 'lib/clearance/authentication.rb', line 47

def (user)
  if user
    cookies[:remember_token] = {
      :value   => user.remember_token,
      :expires => Clearance.configuration.cookie_expiration.call
    }
    self.current_user = user
  end
end

#sign_outObject

Sign user out of cookie.

Examples:

sign_out


61
62
63
64
65
# File 'lib/clearance/authentication.rb', line 61

def sign_out
  current_user.reset_remember_token! if current_user
  cookies.delete(:remember_token)
  self.current_user = nil
end

#signed_in?true, false

Is the current user signed in?

Returns:

  • (true, false)


30
31
32
# File 'lib/clearance/authentication.rb', line 30

def signed_in?
  ! current_user.nil?
end

#signed_out?true, false

Is the current user signed out?

Returns:

  • (true, false)


37
38
39
# File 'lib/clearance/authentication.rb', line 37

def signed_out?
  current_user.nil?
end