我發現了一些關於可恢復功能的討論和主題,但無法解決我遇到的問題。設計(通過API)可恢復:reset_password_token不能爲空
我有一個使用軌道3.2後端(與設計2.2.4) 所有路由都運作良好,但前端API,當我嘗試更新它發送一個JSON resonse(在郵差)密碼:
"reset_password_token" : "can't be blank"
如果我理解的特徵:
1-該POST#POST /資源/密碼發送復位密碼的電子郵件(用作PARAM) 2-然後我執行GET /資源/密碼/編輯?reset_password_token =使用密碼輸入到我的FrontEnd頁面的abcdef。 3-當密碼更改時我啓動#PUT /資源/密碼
這是發生錯誤的地方。
這裏是password_controller.rb文件:
class Devise::PasswordsController < DeviseController
prepend_before_filter :require_no_authentication
# Render the #edit only if coming from a reset password email link
append_before_filter :assert_reset_token_passed, :only => [:edit, :update]
# GET /resource/password/new
def new
build_resource({})
end
# POST /resource/password
def create
self.resource = resource_class.send_reset_password_instructions(resource_params)
if successfully_sent?(resource)
#respond_with({}, :location => after_sending_reset_password_instructions_path_for(resource_name))
render json: { message: "mail sent"}, status: 200
else
#respond_with(resource)
render json: { :status }
end
end
# GET /resource/password/edit?reset_password_token=abcdef
def edit
self.resource = resource_class.new
resource.reset_password_token = params[:reset_password_token]
end
# PUT /resource/password
def update
self.resource = resource_class.reset_password_by_token(params[:reset_password_token])
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_navigational_format?
#sign_in(resource_name, resource)
#respond_with resource, :location => after_resetting_password_path_for(resource)
render json: { message: "password updated"}, status: 200
else
respond_with resource
end
end
protected
def after_resetting_password_path_for(resource)
after_sign_in_path_for(resource)
end
# The path used after sending reset password instructions
def after_sending_reset_password_instructions_path_for(resource_name)
new_session_path(resource_name) if is_navigational_format?
end
# Check if a reset_password_token is provided in the request
def assert_reset_token_passed
if params[:reset_password_token].blank?
#set_flash_message(:error, :no_token)
#redirect_to new_session_path(resource_name)
render json: { message: "reset password is not blank"}, status: 200
end
end
# Check if proper Lockable module methods are present & unlock strategy
# allows to unlock resource on password reset
def unlockable?(resource)
resource.respond_to?(:unlock_access!) &&
resource.respond_to?(:unlock_strategy_enabled?) &&
resource.unlock_strategy_enabled?(:email)
end
end
的recoverable.rb方法reset_password_by_token:
def reset_password_by_token(attributes={})
recoverable = find_or_initialize_with_error_by(:reset_password_token, attributes[:reset_password_token])
if recoverable.persisted?
if recoverable.reset_password_period_valid?
recoverable.reset_password!(attributes[:password], attributes[:password_confirmation])
else
recoverable.errors.add(:reset_password_token, :expired)
end
end
recoverable
end
Devise::Models.config(self, :reset_password_keys, :reset_password_within)
我嘗試添加的方法的一些看跌期權,他們沒有在拋出積壓。 這裏是我的軌道航海日誌:
Started PUT "https://stackoverflow.com/users/password" for 192.168.0.18 at 2016-04-06 09:35:11 +0200
Processing by Devise::PasswordsController#update as JSON
Parameters: {"reset_password_token"=>"[FILTERED]", "password"=>"[FILTERED]"}
, :options=>{:count=>1, :default=>["User"]}, :description=>"User"}, {:key=>"activerecord.attributes.user.confirmation_token", :locale=>:en, :options=>{:count=>1, :default=>[:"attributes.confirmation_token", "Confirmation token"]}, :description=>"Confirmation token"}, {:key=>"activerecord.attributes.user.unconfirmed_email", :locale=>:en, :options=>{:count=>1, :default=>[:"attributes.unconfirmed_email", "Unconfirmed email"]}, :description=>"Unconfirmed email"}, {:key=>"activerecord.attributes.user.email", :locale=>:en, :options=>{:count=>1, :default=>[:"attributes.email", "Email"]}, :description=>"Email"}, {:key=>"activerecord.attributes.user.reset_password_token", :locale=>:en, :options=>{:count=>1, :default=>[:"attributes.reset_password_token", "Reset password token"]}, :description=>"Reset password token"}]}
Completed 422 Unprocessable Entity in 2174ms (Views: 0.4ms | ActiveRecord: 1.6ms)
===================
編輯
===== ==============
要明白的地方請求來自我下面添加對角/前端部分PUT請求:
users.newPassword = function (token, password) {
var putData = {
user:{
reset_password_token: token,
password: password
}
};
return Restangular.withConfig(function(RestangularConfigurer) {
RestangularConfigurer.setFullResponse(true);
}).one('/users/password').customPUT(putData);
};
是的,我做了第一PARAMS一個錯誤決議:它不是''confirmation_token'but reset_password_token '我更新了這個帖子。現在我正在嘗試你的建議。 – user1713964
我用'resource_class.reset_password_by_token(params [:password],params [:reset_password_token])'更改了更新方法,但仍然有「錯誤不能爲空」的422錯誤。在服務器日誌中,我有2個參數:'參數:{「reset_password_token」=>「[FILTERED]」,「password」=>「[FILTERED]」}' – user1713964
事實是我試圖在所有這個控制器的方法現在在日誌上彈出。我認爲在調用更新方法之前還有其他的東西可以捕獲錯誤。但我不知道Devise是如何管理的。 – user1713964