2012-05-18 23 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