2012-05-18 22 views
2

在Rails中設計認證gem。在更改密碼後設計認證日誌

如何通過「忘記密碼」鏈接防止密碼更改後自動登錄?

理想情況下,最好顯示帶有「已保存新密碼」消息的頁面。

回答

4

您需要重寫Devise的passwords_controller,您可以看到here的默認方法。首先,創建自己的控制器,它會從設計控制器繼承:

class User::PasswordsController < Devise::PasswordsController

一旦你有你的控制器準備好,加入所有的,你不希望覆蓋等方法,而只是內部調用super其中。這將是neweditcreate方法。另外不要忘記添加受保護的after_sending_reset_password_instructions_path_for(resource_name)方法。

您關心覆蓋的方法是update操作。

def update 
    self.resource = resource_class.reset_password_by_token(resource_params) 

    if resource.errors.empty? 
    flash_message = resource.active_for_authentication? ? :updated : :updated_not_active 
    set_flash_message(:notice, "Your flash message here") 
    redirect_to new_user_session_path 
    else 
    respond_with resource 
    end 
end 

所有我們在這裏改變的是刪除與重定向到登錄頁面,用戶簽署線,然後將我們自定義的提示信息。

最後,你必須告訴設計使用新的控制器,所以在routes.rb變化devise_for :users到:

devise_for :users, :controllers => { :passwords => 'users/passwords' } 

而且應該這樣做。

0

上述答案是正確的,但事情是根據設計版本而有所不同。我遵循上面說的,我無法得到它的工作,一段時間後,我發現我使用的設計版本不支持resource_params方法,然後我嘗試了不同的版本,並得到它的工作。

2

下面是根據色器件3.1.1更新

class Users::PasswordsController < Devise::PasswordsController 

def new 
    super 
end 

def edit 
    super 
end 

def create 
    super 
end 

#override this so user isn't signed in after resetting password 
def update 
    self.resource = resource_class.reset_password_by_token(resource_params) 

    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? 

     respond_with resource, :location => after_resetting_password_path_for(resource) 
    else 
     respond_with resource 
    end 

    end 

protected 

    def after_resetting_password_path_for(resource) 
    new_session_path(resource) 
    end