Skip to content

Instantly share code, notes, and snippets.

@jhowarth
Created April 27, 2011 22:52
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jhowarth/945412 to your computer and use it in GitHub Desktop.
Save jhowarth/945412 to your computer and use it in GitHub Desktop.
A session controller for Devise that allows ajax logins.
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
@lcguida
Copy link

lcguida commented Aug 18, 2014

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment