Skip to content

Instantly share code, notes, and snippets.

@rfunduk
Created September 24, 2011 15:51
Show Gist options
  • Save rfunduk/1239474 to your computer and use it in GitHub Desktop.
Save rfunduk/1239474 to your computer and use it in GitHub Desktop.
namespace :admin do
mount Resque::Server, :at => '/resque'
# ...
end
gem 'warden'
gem 'rails_warden'
Rails.configuration.middleware.use RailsWarden::Manager do |manager|
manager.default_strategies :admin
manager.failure_app = Admin::SessionsController.action(:new)
end
Warden::Manager.serialize_into_session do |user|
user.id.to_s
end
Warden::Manager.serialize_from_session do |id|
Administrator.find( id )
end
Warden::Strategies.add( :admin ) do
def valid?
params[:email] || params[:password]
end
def authenticate!
begin
email = params[:email].downcase
admin = Administrator.where( email: email ).first
unless admin.authenticate( params[:password] )
raise StandardError
end
success! admin
rescue
admin = nil
fail!
end
end
end
def require_admin
# get admin id from session and look up
# logged in user by hand and assign to @admin
redirect_to new_admin_session_path unless @admin
end
def require_admin
warden.authenticate! :admin
@admin = warden.user
end
class MountedAdminAppAuth
def initialize( app )
@app = app
end
def call( env )
env['rack.session.options'] = {
key: 'YOUR_SESSION_KEY',
secret: YourApp::Application.config.secret_token
}
env['warden'].authenticate!
@app.call( env )
end
end
Route53::Web.use MountedAdminAppAuth
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment