Sha256: 9601607b1c1973134ff9a16c1525d1ca65822c1457fe2fdfdb5024bd17d883ab

Contents?: true

Size: 1.02 KB

Versions: 3

Compression:

Stored size: 1.02 KB

Contents

Dir[File.dirname(__FILE__) + '/warden_plugin/**/*.rb'].each {|file| load file }

module SinatraMore
  module WardenPlugin
    def self.registered(app)
      app.use Warden::Manager do |manager|
        manager.default_strategies :password
        manager.failure_app = app
      end
      app.helpers SinatraMore::WardenHelpers

      # TODO Improve serializing methods
      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?
          username || password
        end

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sinatra_more-0.0.14 lib/sinatra_more/warden_plugin.rb
sinatra_more-0.0.13 lib/sinatra_more/warden_plugin.rb
sinatra_more-0.0.11 lib/sinatra_more/warden_plugin.rb