How to change reset_password_token error

I am trying to completely override the error message 'reset_password_token is invalid' . I would like him to read "password reset link has already been used." How can i do this? No field or keyword was found in the devise.en.yml field for this.

+5
source share
2 answers

Reset password token is invalid message is a verification error that occurs when updating the password, and is not a special development error (for which messages are stored in devise.en.yml ).

This check is performed in the devise / passwords_controller # update method. Code below:

 # PUT /resource/password def update self.resource = resource_class.reset_password_by_token(resource_params) yield resource if block_given? if resource.errors.empty? resource.unlock_access! if unlockable?(resource) flash_message = resource.active_for_authentication? ? :updated : :updated_not_active set_flash_message(:notice, flash_message) if is_flashing_format? sign_in(resource_name, resource) respond_with resource, location: after_resetting_password_path_for(resource) else respond_with resource end end 

The line self.resource = resource_class.reset_password_by_token(resource_params) sets the resource.errors object, and the error message associated with reset_password_token is not valid.

Checking the value of resource.errors after this line shows a large hash ending in ... @messages={:reset_password_token=>["is invalid"]}

The devise_error_messages method devise_error_messages reformat this to say: "Reset Password icon is invalid."

To change this message, the password controller must be configured, and the update method changed to have a different hash of the error message.

The steps would be as follows:

1) Configure routes for the password controller

 # config/routes.rb devise_for :users, :controllers => { :passwords => "passwords" } 

2) Create a customized password controller

 # app/controllers/passwords_controller.rb class PasswordsController < Devise::PasswordsController end 

3) Configure the update method to change the error message:

 # app/controllers/passwords_controller.rb # Include the update method as it is fully, with the following changes in the else block: def update ... if resource.errors.empty? ... else if resource.errors.messages[:reset_password_token] resource.errors.messages.delete(:reset_password_token) resource.errors.messages[:password_reset_link] = ["has already been used"] end respond_with resource end 

Read more about Configuring development controllers

+2
source

A simpler solution than rewriting passwords_controller is to simply change the view:

In app / views / devise / passwords / edit.html.haml (or your erb equivalent), Just put this condition inside the form:

  - if resource.errors[:reset_password_token].present? .alert.alert-danger This password reset URL has expired. You may have requested to reset your password more than once. Follow the link in the most recent email or = link_to 'request to reset your password again.', new_user_password_path 

And you can remove these two lines:

 = f.error_notification = f.full_error :reset_password_token 
+1
source

All Articles