views:

9

answers:

1

Update: this question has been answered (see below). I'll leave it up in case anyone can benefit in the future.


I am trying to get e-mail confirmations working on Authlogic using Rails 3. http://github.com/matthooks/authlogic-activation-tutorial

Authentication is working and the activation e-mails are being generated and sent, each containing a perishable token, but the perishable tokens are incorrect, in that they do not match the one saved in the user's record.

Upon following the token in the e-mail, I get: Exception in ActivationsController#create

Note: When I manually enter the correct token from the table into the URL, it validates and redirects as it is supposed to. Therefore, the only issue is that the perishable token being generated is not the same as the one being saved.

# UserMailer
class UserMailer < ActionMailer::Base  
  default :from => "[email protected]"

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

  def activation_instructions(user)
    subject       "Activate Your Account"
    from          "[email protected]"
    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          "[email protected]"
    recipients    user.email
    sent_on       Time.now
    body          :root_url => root_url
  end
end

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

The error is occurring on line 5 where the system tries and fails to find User by token:

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
A: 

It's funny - sometimes the process of asking the question itself reveals the answer.

In my users#create, there are different user types, and the action sets a couple of values after the initial validated save and saves the simple changes again without validation.

My e-mail was being sent in between the first and second saves, so of course by the time the user clicks on the activation e-mail, the perishable_token has already been reset.

I moved the mailing down to after the second save, and now the activation e-mail works perfectly.

Thank you very much for any time you've spent considering this problem. :) Cirrus

sscirrus