[英]Devise how to change reset_password_token error
我試圖完全覆蓋Devise的錯誤消息'reset_password_token is invalid'
。 我想讀它"password reset link has already been used."
我怎樣才能做到這一點? 在devise.en.yml
中沒有看到這個字段或關鍵字。
Reset password token is invalid
消息是更新密碼時拋出的驗證錯誤,並且不是特定於設備的錯誤(對於該消息存儲在devise.en.yml中 )。
此驗證發生在devise / passwords_controller#update方法中。 代碼包括:
# 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
self.resource = resource_class.reset_password_by_token(resource_params)
行設置resource.errors
對象,其中與reset_password_token相關的錯誤消息無效。
在此行之后檢查resource.errors
的值將顯示以... @messages={:reset_password_token=>["is invalid"]}
結尾的大哈希值... @messages={:reset_password_token=>["is invalid"]}
devise_error_messages
方法將其重新格式化為“重置密碼令牌無效”。
要更改此消息,應自定義密碼控制器,並將update
方法更改為具有不同的錯誤消息哈希。
步驟如下:
1)自定義密碼控制器的路由
# config/routes.rb
devise_for :users, :controllers => { :passwords => "passwords" }
2)創建自定義密碼控制器
# app/controllers/passwords_controller.rb
class PasswordsController < Devise::PasswordsController
end
3)自定義更新方法以更改錯誤消息:
# 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
有關自定義Devise控制器的更多信息
比覆蓋passwords_controller更簡單的解決方案就是修改視圖:
在app / views / devise / passwords / edit.html.haml(或等效的erb)中,只需將此條件放在表單中:
- 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
你可能想要刪除這兩行:
= f.error_notification
= f.full_error :reset_password_token
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.