2016-12-16 114 views
1

我試圖更改用戶密碼併成功更改,但它不會讓我做任何事後因爲用戶未經授權...我可能錯過了一個重新驗證它們的部分。在設計中更改用戶密碼

這是我的代碼來更新自己的密碼

def password 

    if current_user.valid_password?(params[:current_password]) 

     current_user.password = params[:new_password] 

     if current_user.save 
     #sign them in 

     #tried doing this to sign them in again but didn't work 
      sign_in(:user, current_user) 
      response.headers['X-CSRF-Token'] = form_authenticity_token 
      response.headers['X-Pyne-Auth'] = current_user.authentication_token 

      render :json => {:success => true} and return 
     else 
      render :json => {:success => false, error: "Unexpected error while trying to save user. Please try again."} and return 
     end 

    else 
     render :json => {:success => false, error: "Your current password is incorrect. Please try again"} and return 
    end 

    end 

我可以更新密碼,但有麻煩再次訪問應用程序,因爲用戶成爲非法。

謝謝

回答

2

嘗試bypass_sign_in(@user)的建議in the Devise wiki

+0

我正準備和wiki一樣,如果'@ user.update(user_params)'......進入else @Brian – Walker

+0

聽起來像'update'失敗然後。控制檯上是否有任何內容顯示阻止更新的內容?或者,您可以查看else子句中的@ user.errors.full_messages,看看發生了什麼。 – Brian

+0

布賴恩,我在我的用戶控制器中這樣做,我只注意到我在我的路線'devise_for:users,:controllers => {:sessions =>「api/v1/sessions」,密碼:'api/v1 /密碼'},::path_prefix =>'api/v1''這是否意味着我應該在其中一個控制器或其他東西? – Walker