Skip to content

recall method is not being called in warden.authenticate! #3142

@lcguida

Description

@lcguida

Hi,

I searched around, posted in stackoverflow, but I got no solution so far.

I'm trying to give a custom json response in case of failed login. I want the json to be in this format:

{success: false, errors: [<error_message>]}

So I overrode the SessionsContoller, as following:

class SessionsController < Devise::SessionsController
  respond_to :json

  def create 
    self.resource = warden.authenticate!(auth_options)
    sign_in(resource_name, resource)
    render json: { :success => true }, status: 200
  end

  def failure   
    warden.custom_failure!
    render json: { success: false, errors: ["Login Credentials Failed"] }, status: 401
  end

  protected
  def auth_options
    {:scope => resource_name, :recall => "#{controller_path}#failure"}
  end
end

routes.rb:

devise_for :users, controllers: {sessions: 'sessions'}

devise.rb:

config.navigational_formats = ["*/*", :html, :json]

Login is working just fine, I'm just not being able to override the failure response. I'm just posting this here to check if it's not a bug, since this same code is presented in various blog posts in the internet.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions