Sha256: ddec63e2194d80de86076ed8335c53f9d3d3004cd0c5e7b510a7ec61844b669d

Contents?: true

Size: 1.3 KB

Versions: 10

Compression:

Stored size: 1.3 KB

Contents

# This module is included in your application controller which makes
# several methods available to all controllers and views. Here's a
# common example you might add to your application layout file.
#
#   <% if logged_in? %>
#     Welcome <%= current_user.username %>! Not you?
#     <%= link_to "Log out", logout_path %>
#   <% else %>
#     <%= link_to "Sign up", signup_path %> or
#     <%= link_to "log in", login_path %>.
#   <% end %>
#
# You can also restrict unregistered users from accessing a controller using
# a before filter. For example.
#
#   before_filter :login_required, :except => [:index, :show]
#

module Authentication
  def self.included(controller)
    controller.send :helper_method, :current_user, :logged_in?, :redirect_to_target_or_default
  end

  def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
  end

  def logged_in?
    current_user
  end

  def login_required
    unless logged_in?
      flash[:error] = "You must first log in or sign up before accessing this page."
      store_target_location
      redirect_to login_url
    end
  end

  def redirect_to_target_or_default(default)
    redirect_to(session[:return_to] || default)
    session[:return_to] = nil
  end

  private

  def store_target_location
    session[:return_to] = request.fullpath
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
i0n_rails3_generators-0.2.19 lib/generators/i0n/authentication/templates/lib/authentication.rb
i0n_rails3_generators-0.2.18 lib/generators/i0n/authentication/templates/lib/authentication.rb
i0n_rails3_generators-0.2.17 lib/generators/i0n/authentication/templates/lib/authentication.rb
i0n_rails3_generators-0.2.16 lib/generators/i0n/authentication/templates/lib/authentication.rb
i0n_rails3_generators-0.2.15 lib/generators/i0n/authentication/templates/lib/authentication.rb
i0n_rails3_generators-0.2.14 lib/generators/i0n/authentication/templates/lib/authentication.rb
i0n_rails3_generators-0.2.13 lib/generators/i0n/authentication/templates/lib/authentication.rb
i0n_rails3_generators-0.2.12 lib/generators/i0n/authentication/templates/lib/authentication.rb
i0n_rails3_generators-0.2.11 lib/generators/i0n/authentication/templates/lib/authentication.rb
i0n_rails3_generators-0.2.10 lib/generators/i0n/authentication/templates/lib/authentication.rb