Sha256: 94e3a98f068d91214a7ef2a651591913877b338323f0f9d37d68e6ad0c15aa66

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

module SinatraMore
  module WardenHelpers
    # Returns the current authenticated user
    def current_user
      warden_handler.user
    end

    # Login the user through the specified warden strategy
    def authenticate_user!
      warden_handler.authenticate!
    end

    # Signs out the user
    def logout_user!
      warden_handler.logout
    end

    # Returns true if the user has authenticated
    def logged_in?
      warden_handler.authenticated?
    end

    # If a block is given, only yields the content if the user is authenticated
    # If no block is given, returns true if the user is logged in
    def authenticated?(&block)
      if block_given?
        authenticated_content = capture_haml(&block)
        logged_in? ? haml_concat(authenticated_content) : ''
      else
        return logged_in?
      end
    end

    # Forces a user to return to a fail path unless they are authorized
    # Used to require a user be authenticated before routing to an action
    def must_be_authorized!(failure_path=nil)
      redirect_to(failure_path ? failure_path : '/') unless authenticated?
    end

    # Returns the raw warden authentication handler
    def warden_handler
      request.env['warden']
    end
  end

  module WardenPlugin
    def self.registered(app)
      app.helpers SinatraMore::WardenHelpers

      # TODO Improve serializing
      Warden::Manager.serialize_into_session{ |user| user.nil? ? nil : user.id }
      Warden::Manager.serialize_from_session{ |id|   id.nil? ? nil : User.find(id) }

      Warden::Strategies.add(:password) do
        def valid?
          params['username'] || params['password']
        end

        def authenticate!
          u = User.authenticate(params['username'], params['password'])
          u.nil? ? fail!("Could not log in") : success!(u)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sinatra_more-0.0.4 lib/sinatra_more/warden_helpers.rb