Created
August 3, 2009 11:57
-
-
Save nesquena/160506 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
- form_tag '/login' do | |
.flash= flash[:notice] | |
%p | |
= label_tag :username | |
= text_field_tag :username, :value => params[:username] | |
%p | |
= label_tag :password | |
= text_field_tag :password, :value => params[:password] | |
%p | |
= submit_tag "Login" |
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
describe "Some Controller Test" do | |
it "allow a logged in user" do | |
fred = Factory(:user) | |
# I want to authenticate fred as a logged in user here | |
get '/dashboard' | |
last_response.should.be.ok | |
end | |
it "reject a non-logged in user" do | |
get '/dashboard' | |
last_response.should.redirect_to '/login' | |
end | |
end |
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
configure do | |
use Rack::Session::Cookie | |
use Warden::Manager do |manager| | |
manager.default_strategies :password | |
manager.failure_app = WardenHandler | |
end | |
use Rack::Flash | |
end | |
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 |
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
class WardenHandler < Sinatra::Application | |
every '/unauthenticated/?' do | |
flash[:notice] = "That username and password are not correct!" | |
status 401 | |
haml_template 'warden/new' | |
end | |
get '/login/?' do | |
haml_template 'warden/new' | |
end | |
post '/login/?' do | |
env['warden'].authenticate! | |
redirect "/" | |
end | |
get '/logout/?' do | |
warden_handler.logout | |
redirect '/login' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment