Sha256: c70959bec334ff300a19bde9473e41561e886344b18c1b9076d9ebb5a7aaa78b

Contents?: true

Size: 1.49 KB

Versions: 22

Compression:

Stored size: 1.49 KB

Contents

module TwitterAuth
  # These methods borrow HEAVILY from Rick Olsen's
  # Restful Authentication. All cleverness props
  # go to him, not me.
  module ControllerExtensions
    def self.included(base)
      base.send :helper_method, :current_user, :logged_in?, :authorized?
    end

    protected

    def authentication_failed(message, destination='/')
      flash[:error] = message
      redirect_to destination
    end

    def authentication_succeeded(message = 'You have logged in successfully.', destination = '/')
      flash[:notice] = message
      redirect_to destination
    end

    def current_user
      @current_user ||= User.find_by_id(session[:user_id]) || User.from_remember_token(cookies[:remember_token])
    end

    def current_user=(new_user)
      session[:user_id] = new_user.id
      @current_user = new_user
    end

    def authorized?
      !!current_user
    end

    def login_required
      authorized? || access_denied 
    end

    def access_denied
      store_location
      redirect_to login_path
    end

    def store_location
      session[:return_to] = request.request_uri
    end

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

    def logged_in?
      !!current_user
    end

    def logout_keeping_session!
      @current_user = nil
      session[:user_id] = nil
      cookies.delete(:remember_token)
    end
  end
end

ActionController::Base.send(:include, TwitterAuth::ControllerExtensions)

Version data entries

22 entries across 22 versions & 3 rubygems

Version Path
mbleigh-twitter-auth-0.1.10 lib/twitter_auth/controller_extensions.rb
mbleigh-twitter-auth-0.1.11 lib/twitter_auth/controller_extensions.rb
mbleigh-twitter-auth-0.1.12 lib/twitter_auth/controller_extensions.rb
mbleigh-twitter-auth-0.1.13 lib/twitter_auth/controller_extensions.rb
mbleigh-twitter-auth-0.1.14 lib/twitter_auth/controller_extensions.rb
mbleigh-twitter-auth-0.1.15 lib/twitter_auth/controller_extensions.rb
mbleigh-twitter-auth-0.1.16 lib/twitter_auth/controller_extensions.rb
mbleigh-twitter-auth-0.1.18 lib/twitter_auth/controller_extensions.rb
mbleigh-twitter-auth-0.1.20 lib/twitter_auth/controller_extensions.rb
mbleigh-twitter-auth-0.1.21 lib/twitter_auth/controller_extensions.rb
millsb-twitter-auth-0.1.16 lib/twitter_auth/controller_extensions.rb
twitter-auth-0.1.10 lib/twitter_auth/controller_extensions.rb
twitter-auth-0.1.11 lib/twitter_auth/controller_extensions.rb
twitter-auth-0.1.13 lib/twitter_auth/controller_extensions.rb
twitter-auth-0.1.12 lib/twitter_auth/controller_extensions.rb
twitter-auth-0.1.15 lib/twitter_auth/controller_extensions.rb
twitter-auth-0.1.16 lib/twitter_auth/controller_extensions.rb
twitter-auth-0.1.14 lib/twitter_auth/controller_extensions.rb
twitter-auth-0.1.17 lib/twitter_auth/controller_extensions.rb
twitter-auth-0.1.21 lib/twitter_auth/controller_extensions.rb