Skip to content

Instantly share code, notes, and snippets.

@RSpace
Created September 30, 2010 14:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RSpace/604661 to your computer and use it in GitHub Desktop.
Save RSpace/604661 to your computer and use it in GitHub Desktop.
class CustomFailure < Devise::FailureApp
# Never do http authentication through Devise
def http_auth?
false
end
def redirect_url
send(:"new_#{scope}_session_path", :format => (request.xhr? ? 'js' : nil ))
end
end
Devise.setup do |config|
...
# ==> Warden configuration
# If you want to use other strategies, that are not (yet) supported by Devise,
# you can configure them inside the config.warden block.
config.warden do |manager|
manager.failure_app = CustomFailure
end
end
MyApp::Application.routes.draw do
# Devise authentication
devise_for :users,
:controllers => { :sessions => '/sessions', :registrations => '/registrations' }
end
...
end
class SessionsController < Devise::SessionsController
def sign_in_and_redirect(resource_or_scope, resource=nil)
scope = Devise::Mapping.find_scope!(resource_or_scope)
resource ||= resource_or_scope
redirect_url = stored_location_for(scope)
respond_to do |format|
format.js do
sign_in(scope, resource) unless warden.user(scope) == resource
if redirect_url.present?
redirect_url = "#{redirect_url}.js" unless redirect_url[-3..-1] == '.js'
redirect_url += redirect_url.match(/\?/) ? '&' : '?'
redirect_url += "after_authentication=true"
redirect_to redirect_url
else
render(:update) do |page|
page << render('/shared/after_authentication')
end
end
end
format.html { super }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment