Created
December 30, 2010 01:35
-
-
Save delonnewman/759331 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'sinatra' | |
require 'sinatra_warden' | |
require 'warden' | |
require 'rack/flash' | |
require 'haml' | |
User = Struct.new(:id, :name, :email) | |
Warden::Strategies.add(:password) do | |
def valid? | |
p "Validating..." | |
p params | |
params['username'] && params['password'] | |
end | |
def authenticate! | |
p "Authenticating..." | |
p params | |
r = params["username"] == 'test' && params["password"] == 'testing' | |
u = User.new(1, "Peter Parker", "[email protected]") | |
r ? success!(u) : fail!("Couldn't log in") | |
end | |
end | |
module Test | |
class App < Sinatra::Base | |
use Rack::Session::Cookie | |
use Warden::Manager do |m| | |
m.default_strategies :password | |
m.failure_app = Test::App | |
m.serialize_into_session { |u| u.id } | |
m.serialize_from_session do |id| | |
User.new(id, "Peter Parker", "[email protected]") | |
end | |
end # Warden::Manager | |
use Rack::Flash | |
register Sinatra::Warden | |
set :sessions, true | |
set :auth_failure_path, '/fail' | |
set :auth_success_path, '/' | |
get '/?' do | |
authorize!('/login') | |
return <<-RESULT | |
#{flash[:success]} | |
We're in #{user.name}! your id is #{user.id}. | |
<a href="/logout">Click Here</a> to logout. | |
<pre>Your params where: #{params.inspect}</pre>. | |
RESULT | |
end | |
get '/fail' do | |
"Sorry, you didn't get in." | |
flash[:error] | |
end | |
end | |
def self.app | |
Test::App | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment