Stpn
Stpn

Reputation: 6394

Custom devise sessions controller is not redirecting on error (:recall => not happening)

I am overriding Device sessions controller... It works (I can log in with json), but I cannot get it to redirect to another action on error:

class Users::SessionsController < Devise::SessionsController

  def create
    resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#no")
    return invalid_login_attempt unless resource
    sign_in(resource_name, resource)
    #respond_with resource, :location => redirect_location(resource_name, resource)
    respond_to do |format|
      format.json { render :status => 200, :json => { :success => true, :auth_token => resource.authentication_token, :user => resource } }
    end
  end

  def no
    puts "NO!"
    return render:json => {:success => false, :errors => alert}
  end


end

I also tried:

 if resource.nil?
   puts "NIL!"
 end

I always get the same default Devise json error in response to wrong sign_in credentials:

{error: "You need to sign in or sign up before continuing."}

What am I doing wrong?

Upvotes: 4

Views: 1953

Answers (2)

mrodrigues
mrodrigues

Reputation: 1082

According to this similar thread (Extending Devise SessionsController to authenticate using JSON), all you need to do is to include the following code:

# config/initializers/devise.rb
config.navigational_formats = [:"*/*", "*/*", :html, :json]

Upvotes: 1

Matt
Matt

Reputation: 14048

Keep in mind that if warden doesn't get a user from it's authenticate! action it throws a symbol to the failure app. Anything after that line is therefore irrelevant in the case of failure.

Change authenticate! to authenticate and it will return the user object for you to handle as you see fit.

Check the proxy.rb file in the warden gem for further details. Documentation

Upvotes: 2

Related Questions