Created
April 27, 2011 22:52
-
-
Save jhowarth/945412 to your computer and use it in GitHub Desktop.
A session controller for Devise that allows ajax logins.
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 SessionsController < Devise::SessionsController | |
def create | |
resource = warden.authenticate!(:scope => resource_name, :recall => :failure) | |
return sign_in_and_redirect(resource_name, resource) | |
end | |
def sign_in_and_redirect(resource_or_scope, resource=nil) | |
scope = Devise::Mapping.find_scope!(resource_or_scope) | |
resource ||= resource_or_scope | |
sign_in(scope, resource) unless warden.user(scope) == resource | |
return render :json => {:success => true, :redirect => stored_location_for(scope) || after_sign_in_path_for(resource)} | |
end | |
def failure | |
return render:json => {:success => false, :errors => ["Login failed."]} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does your failure method is working?
I'm trying to do the same thing here, but when credentials are wrong, the default devise response is being sent instead.
See heartcombo/devise#3142 (comment)