Sha256: a30cde54887e09b6e35495b2bdbe6665faf57f63c92f354731fdbb86c5d540e2

Contents?: true

Size: 1.89 KB

Versions: 2

Compression:

Stored size: 1.89 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_back_or_default destination
    end
    
    def onboard_user(message = 'Welcome to Vowch!', destination='/')
      flash[:notice] = message
      redirect_back_or_default destination
    end

    def current_user
      @current_user ||= 
        if session[:user_id]
          User.find_by_id(session[:user_id])
        elsif cookies[:remember_token]
          begin
            User.from_remember_token(cookies[:remember_token])
          rescue
            cookies.delete(:remember_token)
            false   
          end
        else
          false
        end
    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!
      session[:user_id] = nil
      @current_user = nil
      cookies.delete(:remember_token)
    end
  end
end

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
twitter-auth-with-mongo-mapper-0.1.0 lib/twitter_auth/controller_extensions.rb
twitter-auth-with-mongo-mapper-0.0.9 lib/twitter_auth/controller_extensions.rb