Module: Authenticate::Controller

Extended by:
ActiveSupport::Concern
Includes:
Debug
Defined in:
lib/authenticate/controller.rb

Instance Method Summary collapse

Methods included from Debug

#debug

Instance Method Details

#authenticate(params) ⇒ Object

Validate a user’s identity with (typically) email/ID & password, and return the User if valid, or nil. After calling this, call login(user) to complete the process.



14
15
16
17
18
19
# File 'lib/authenticate/controller.rb', line 14

def authenticate(params)
  # todo: get params from User model
  credentials = Authenticate.configuration.user_model_class.credentials(params)
  debug "Controller::credentials: #{credentials.inspect}"
  Authenticate.configuration.user_model_class.authenticate(credentials)
end

#authenticate_controller?Boolean

Return true if it’s an Authenticate controller. Useful if you want to apply a before filter to all controllers, except the ones in Authenticate, e.g.

before_action :my_filter, unless: :authenticate_controller?

Returns:

  • (Boolean)


97
98
99
# File 'lib/authenticate/controller.rb', line 97

def authenticate_controller?
  is_a?(Authenticate::AuthenticateController)
end

#authenticated?Boolean

Has the user been logged in? Exposed as a helper, can be called from views.

<% if authenticated? %>
  <%= link_to logout_path, "Sign out" %>
<% else %>
  <%= link_to login_path, "Sign in" %>
<% end %>

Returns:

  • (Boolean)


78
79
80
# File 'lib/authenticate/controller.rb', line 78

def authenticated?
  authenticate_session.authenticated?
end

#current_userObject

Get the current user from the current Authenticate session. Exposed as a helper , can be called from controllers, views, and other helpers.

<p>Your email address: <%= current_user.email %></p>


88
89
90
# File 'lib/authenticate/controller.rb', line 88

def current_user
  authenticate_session.current_user
end

#login(user, &block) ⇒ Object

Complete the user’s sign in process: after calling authenticate, or after user creates account. Runs all valid callbacks and sends the user a session token.



24
25
26
# File 'lib/authenticate/controller.rb', line 24

def (user, &block)
  authenticate_session. user, &block
end

#logoutObject

Log the user out. Typically used in session controller.

class SessionsController < ActionController::Base

include Authenticate::Controller

def destroy
  logout
  redirect_to '/', notice: 'You logged out successfully'
end


38
39
40
# File 'lib/authenticate/controller.rb', line 38

def logout
  authenticate_session.deauthenticate
end

#require_authenticationObject

Use this filter as a before_action to restrict controller actions to authenticated users. Consider using in application_controller to restrict access to all controllers.

Example:

class ApplicationController < ActionController::Base
  before_action :require_authentication

  def index
    # ...
  end
end


56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/authenticate/controller.rb', line 56

def require_authentication
  debug 'Controller::require_authentication'
  unless authenticated?
    unauthorized
  end

  message = catch(:failure) do
    current_user = authenticate_session.current_user
    Authenticate.lifecycle.run_callbacks(:after_set_user, current_user, authenticate_session, {event: :set_user })
  end
  unauthorized(message) if message
end