Skip to content

Instantly share code, notes, and snippets.

@hassox
Created July 19, 2010 06:21
Show Gist options
  • Save hassox/481065 to your computer and use it in GitHub Desktop.
Save hassox/481065 to your computer and use it in GitHub Desktop.
class Guard < Padrino::Application
register Padrino::Helpers
require 'sass/plugin/rack'
Warden::Manager.serialize_into_session{|user| user.id }
Warden::Manager.serialize_from_session{|id| User.get(id) }
Warden::Manager.before_failure do |env,opts|
# Sinatra is very sensitive to the request method
# since authentication could fail on any type of method, we need
# to set it for the failure app so it is routed to the correct block
puts "============== #{opts.inspect}"
env['REQUEST_METHOD'] = "POST"
end
Warden::Strategies.add(:basic) do
def valid?
username && params["password"]
end
def username
params['email'] || params['login']
end
def authenticate!
u = User.authenticate(username, params["password"])
u.nil? ? fail! : success!(u)
end
end
Sass::Plugin.options[:template_location] = File.join(Padrino.root, "guard/app/sass")
Sass::Plugin.options[:css_location] = File.join(Padrino.root, "public/css")
configure do
use Sass::Plugin::Rack
use Rack::Session::DataMapper
use Rack::Flash
use Warden::Manager do |config|
config.default_scope :default
config.scope_defaults :default,
:strategies => [:basic],
:action => "sessions/unauthenticated"
config.failure_app = Guard
end
end
end
include CoreApp::Guard
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment