【问题标题】:Rails - Authlogic persistence_token questionRails - Authlogic persistence_token 问题
【发布时间】:2011-04-18 02:29:23
【问题描述】:

我正在尝试使用http://www.binarylogic.com/2008/11/16/tutorial-reset-passwords-with-authlogic/ 的教程实现重置密码功能 并面临困难。问题似乎来自persistence_token 与数据库中的内容不匹配。

persistence_token 何时设置/更新?

我的密码重置代码与教程一模一样,这里是:

class PasswordResetsController < ApplicationController
  before_filter :require_no_user
  before_filter :load_user_using_perishable_token, :only => [ :edit, :update ]

  def create
    @user = User.find_by_email_address(params[:email])
    if @user
      @user.deliver_password_reset_instructions!
      flash[:notice] = "Instructions to reset your password have been emailed to you"
      redirect_to login_path
    else
      flash.now[:error] = "No user was found with email address #{params[:email]}"
      render :action => :new
    end
  end

def update
  @user.password = params[:password]
  if @user.save
    flash[:success] = "Your password was successfully updated"
    redirect_to @user
  else
    render :action => :edit
  end
end

private

def load_user_using_perishable_token
  @user = User.find_using_perishable_token(params[:id])
  unless @user
    flash[:error] = "We're sorry, but we could not locate your account"
    redirect_to new_password_reset_path
  end
end

方法load_user_using_perishable_token 正在从数据库中正确加载值,但是,对@user.password = params[:password] 的调用正在将peristence_token 修改为不同的值。随后的调用@user.save 失败,因为它找不到修改后的persistence_token 的记录。

任何解决这个问题的想法都会很有帮助。

编辑澄清:

1) 没有错误,@user.save 返回 false。 @user.save 触发以下 SQL 查询:

SELECT `users`.id FROM `users` WHERE (`users`.`persistence_token` = 
  BINARY '53a6fd82a1da50c299230afd2b7911e7774f6fd62a714909194d5d84e2094444d808a71992bb8b11bc572fd865e9956681dea61bf46e3f65d9d41c53a6b0ec90' AND `users`.id <> 7) LIMIT 1 

查询中使用的 persistence_token 值与数据库值不匹配。

2) 我不确定这是否是一个线索,但在调试器中我注意到无论何时 @user.password = params[:password] 被设置,@user 对象中的 persistence_token 值改变。

【问题讨论】:

  • 失败时的错误信息是什么?我怀疑它失败是因为您没有设置@user.password_confirmation,因此验证失败。
  • 你是对的,原来它需要密码确认。即使我的用户模型没有密码确认字段,也需要设置该值才能成功更新现有密码。感谢您为我指明正确的方向。

标签: ruby-on-rails authlogic


【解决方案1】:

htanata 说什么。您没有设置密码确认。你能把你收到的错误信息告诉我们吗?

首先您确实需要设置密码确认,否则您将收到验证错误。

【讨论】:

  • 顺便说一句,你正在混淆 perishable_token 和 persistence_token。 perishable_token 用于密码重置等,persistence_token 用于“记住我”功能。
  • James,我编辑了原始帖子并添加了说明。问题是用于查询密码更新的 persistence_token 值与数据库值不同。如何让它们同步?
  • password_confirmation 是缺失的部分。保存时没有错误,因此认为不需要。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-27
  • 2011-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-19
  • 1970-01-01
相关资源
最近更新 更多