【问题标题】:Authlogic sends the wrong activation codeAuthlogic 发送错误的激活码
【发布时间】:2010-10-11 13:09:45
【问题描述】:

更新:此问题已得到解答(见下文)。我会保留它,以防将来有人可以受益。


我正在尝试使用 Rails 3 在 Authlogic 上获得电子邮件确认。http://github.com/matthooks/authlogic-activation-tutorial

身份验证正在运行,并且正在生成和发送激活电子邮件,每封电子邮件都包含一个易腐标记,但易腐标记不正确,因为它们与保存在用户的记录。

按照电子邮件中的令牌,我得到:ActivationsController#create 中的异常

注意:当我从表中手动将正确的令牌输入到 URL 中时,它会按预期进行验证和重定向。因此,唯一的问题是生成的易腐令牌与保存的令牌不同。

# UserMailer
class UserMailer < ActionMailer::Base  
  default :from => "notifications@myapp.com"

  def registration_confirmation(user)
    @user = user
    mail(:to => "#{user.login} <#{user.email}>", :subject => "Registered")
  end

  def activation_instructions(user)
    subject       "Activate Your Account"
    from          "noreply@myapp.com"
    recipients    user.email
    sent_on       Time.now
    body          :account_activation_url => activate_url(user.perishable_token)
  end

  def welcome(user)
    subject       "Welcome to the site!"
    from          "noreply@myapp.com"
    recipients    user.email
    sent_on       Time.now
    body          :root_url => root_url
  end
end

# E-mail itself:  
To activate, click here: <%= @account_activation_url %>

错误发生在第 5 行,系统尝试通过令牌查找用户但失败:

class ActivationsController < ApplicationController
  before_filter :require_no_user

  def create
    @user = User.find_by_perishable_token(params[:activation_code], 1.week) || (raise Exception)
    raise Exception if @user.active?

    if @user.activate!
      flash[:notice] = "Your account has been activated!"
      UserSession.create(@user, false) # Log user in manually
      @user.deliver_welcome!
      redirect_to home_url
    else
      render :controller => "welcome", :action => "linklogin"
    end
  end    
end

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 authlogic


    【解决方案1】:

    这很有趣 - 有时提出问题的过程本身就会揭示答案。

    在我的 users#create 中,有不同的用户类型,该操作会在初始验证保存后设置几个值,然后再次保存简单的更改而不进行验证。

    我的电子邮件是在第一次和第二次保存之间发送的,所以当用户点击激活电子邮件时,perishable_token 已经被重置。

    我将邮件移到第二次保存后,现在激活电子邮件可以正常工作了。

    非常感谢您花时间考虑这个问题。 :) 卷云

    【讨论】:

      猜你喜欢
      • 2011-07-13
      • 1970-01-01
      • 2015-04-02
      • 1970-01-01
      • 2016-12-12
      • 2014-02-16
      • 1970-01-01
      • 1970-01-01
      • 2013-04-17
      相关资源
      最近更新 更多