jsmestad (owner)

Revisions

  • a4b5ed lukesutton Thu May 07 00:17:28 -0700 2009
gist: 216353 Download_button fork
public
Public Clone URL: git://gist.github.com/216353.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
  env['REQUEST_METHOD'] = "POST"
end
 
Warden::Strategies.add(:password) do
  def valid?
    params["email"] || params["password"]
  end
 
  def authenticate!
    u = User.authenticate(params["email"], params["password"])
    u.nil? ? fail!("Could not log in") : success!(u)
  end
end
Ruby #
1
2
3
4
5
6
7
8
9
require 'login_management'
 
use Rack::Session::Cookie
use Warden::Manager do |manager|
  manager.default_strategies :password
  manager.failure_app = LoginManager
end
 
run LoginManager
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class LoginManager < Sinatra::Base
  get "/" do
    haml :welcome
  end
 
  post '/unauthenticated/?' do
    status 401
    haml :login
  end
 
  get '/login/?' do
    haml :login
  end
  
  post '/login/?' do
    env['warden'].authenticate!
    redirect "/"
  end
  
  get '/logout/?' do
    env['warden'].logout
    redirect '/'
  end
end