Warden custom_failure

I am using an API authentication program. I rewrote the SessionController:create method as follows:

 class Users::SessionsController < Devise::SessionsController def create resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new") set_flash_message(:notice, :signed_in) if is_navigational_format? val = sign_in(resource_name, resource) current_user.reset_authentication_token! respond_to do |format| format.html do respond_with resource, :location => redirect_location(resource_name, resource) end format.json do render :json => { :status => OK_OK, :auth_token => current_user.authentication_token }.to_json, :status => :ok end end end end 

As you can see, I am responding with a status code when authenticating with JSON. When authentication fails, I get the following response:

 {"error":"Invalid email or password."} 

How do I change this message? I do not know where to replace this warden.authenticate! method warden.authenticate! .

+4
source share
2 answers

You must implement your own application for rejection. You can look and / or inherit from devuse default here https://github.com/plataformatec/devise/blob/master/lib/devise/failure_app.rb

To configure it, in the config / initializers / devise.rb configuration file:

 Devise.setup do |config| config.warden do |manager| manager.failure_app = CustomFailureApp end end 
+2
source

In your locale file (the file in the config / locales / directory), depending on your i18n configuration (for English en.yml ) add this:

 en: devise: invalid: 'My custom message for invalid login' 

Of course, replace My custom message for invalid login with the desired message.

+1
source

Source: https://habr.com/ru/post/1386842/


All Articles