Created
July 13, 2012 02:17
-
-
Save mikeebert/3102301 to your computer and use it in GitHub Desktop.
Warden and Sinatra Example 2 (still not complete)
This file contains hidden or 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 'sinatra' | |
| require 'warden' | |
| class YourApp < Sinatra::Application | |
| use Rack::Session::Cookie | |
| use Warden::Manager do |manager| | |
| manager.default_strategies :password | |
| manager.failure_app = YourApp | |
| manager.serialize_into_session {|user| user.id} | |
| manager.serialize_from_session {|id| Datastore.for(:user).find_by_id(id)} | |
| end | |
| Warden::Manager.before_failure do |env,opts| | |
| env['REQUEST_METHOD'] = 'POST' | |
| end | |
| Warden::Strategies.add(:password) do | |
| def valid? | |
| params["email"] || params["password"] | |
| end | |
| def authenticate! | |
| user = Datastore.for(:user).find_by_email(params["email"]) | |
| if user && user.authenticate(params["password"]) | |
| success!(user) | |
| else | |
| fail!("Could not log in") | |
| end | |
| end | |
| end | |
| get "/" do | |
| erb 'index'.to_sym | |
| end | |
| get "/protected_pages" do | |
| erb 'admin_only_page'.to_sym | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment