7

私の Rails アプリケーションは、devise を使用して登録、認証などを処理します。確認可能なモジュールを使用しています。バグはこれです – ユーザーがメールで登録すると、Devise は異なる確認リンクを含む 2 つの確認メールを送信します。1 つのリンクは機能し、もう 1 つのリンクはユーザーをエラー ページに誘導します。

Devise は、「確認トークンが無効です」というエラーに関連するメッセージを吐き出し、ユーザーを確認メールの再送信ページに誘導します。

私はherokuでホスティングしており、sendgridを使用してメールを送信しています。更新:このバグは localhost でも発生します。

このバグの根源がどこにあるのかわかりません。これは、必要なものよりも多くのコードである可能性があります:


models/user.rb

...

devise :database_authenticatable, :registerable, :omniauthable,
     :recoverable, :rememberable, :trackable, :validatable, 
     :confirmable, :authentication_keys => [:login]

...

## callbacks
after_create :account_created

# called after the account is first created
def account_created

  # check if this activiy has already been created
  if !self.activities.where(:kind => "created_account").blank?
    puts "WARNING: user ##{self.id} already has a created account activity!"
    return
  end

  # update points
  self.points += 50
  self.save

  # create activity
  act = self.activities.new
  act.kind = "created_account"
  act.created_at = self.created_at
  act.save

end

...

def confirmation_required?
  super && (self.standard_account? || self.email_changed)
end

...



コントローラー/登録_コントローラー.rb

class RegistrationsController < Devise::RegistrationsController
  def update
    unless @user.last_sign_in_at.nil?

      puts "--------------double checking whether password confirmation is required--"
      ## if the user has not signed in yet, we don't want to do this.

      @user = User.find(current_user.id)
      # uncomment if you want to require password for email change
      email_changed = @user.email != params[:user][:email]
      password_changed = !params[:user][:password].empty?

      # uncomment if you want to require password for email change
      # successfully_updated = if email_changed or password_changed

      successfully_updated = if password_changed
        params[:user].delete(:current_password) if params[:user][:current_password].blank?
        @user.update_with_password(params[:user])
      else
        params[:user].delete(:current_password)
        @user.update_without_password(params[:user])
      end

      if successfully_updated
        # Sign in the user bypassing validation in case his password changed
        sign_in @user, :bypass => true
        if email_changed
          flash[:blue] = "Your account has been updated! Check your email to confirm your new address. Until then, your email will remain unchanged."
        else
          flash[:blue] = "Account info has been updated!"
        end
        redirect_to edit_user_registration_path
      else
        render "edit"
      end
    end
  end
end



コントローラー/omniauth_callbacks_controller

class OmniauthCallbacksController < Devise::OmniauthCallbacksController

  skip_before_filter :verify_authenticity_token

    def facebook
        user = User.from_omniauth(request.env["omniauth.auth"])
    if user.persisted?
      flash.notice = "Signed in!"

      # if the oauth_token is expired or nil, update it...
      if (DateTime.now > (user.oauth_expires_at || 99.years.ago) )
        user.update_oauth_token(request.env["omniauth.auth"])
      end

      sign_in_and_redirect user
    else
      session["devise.user_attributes"] = user.attributes
      redirect_to new_user_registration_url
    end
    end
end



config/routes.rb

...

devise_for :users, controllers: {omniauth_callbacks: "omniauth_callbacks", 
                                :registrations => "registrations"}

...

必要に応じて、さらに情報を提供させていただきます。また、デバイスメーラーの動作をカスタマイズ/オーバーライドすることにもオープンですが、それについてどうすればよいかわかりません。

どうもありがとう!

4

3 に答える 3

14

解決しました!

Devise::Mailer をオーバーライドし、スタック トレースを強制して、メールの重複の原因を正確に突き止めることができました。Devise::Mailer#confirmation_instructions が 2 回呼び出されており、以下に示す :after_create コールバックに問題があることがわかりました。


models/user.rb 内...

after_create :account_created

# called after the account is first created
def account_created

...

  # update points
  self.points += 50
  self.save

...

end

self.save を呼び出すと、どういうわけかメーラーが再びトリガーされました。ポイントが追加されるタイミングを変更することで問題を解決しました。after_create 呼び出しを取り除き、確認を上書きしました! このように見えるようにデバイス内のメソッド:

def confirm!
  super
  account_created
end

そのため、ユーザー レコードは確認後まで変更 (ポイントの追加) されません。メールの重複はもうありません!

于 2012-10-25T05:51:24.663 に答える
8

私はもともとトーマス・クレムの答えに行きましたが、何が起こっているのかを理解するための時間があったときに、これを見て戻ってきました。

「問題」を追跡したところ、デバイス (ユーザー) モデルで :confirmable が設定され、デバイス初期化子で再確認可能が有効になっている場合にのみ発生することに気付きました。メールアドレスを変更していませんが、ユーザーモデル - アカウントがまだ確認されていないため、Devise がこれを行う可能性があると思いますが、いずれにせよ、self.skip_reconfirmation を呼び出すだけで 2 番目のメールを簡単に停止できます! after_create メソッドで。

正しい動作を確認するためだけに、いくつかのテストを含むサンプル Rails プロジェクトを作成しました。以下は主要な抜粋です。時間がありすぎる場合は、ここでプロジェクトを確認できます: https://github.com/richhollis/devise-reconfirmable-test

アプリ/モデル/User.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me

  after_create :add_attribute

  private

  def add_attribute
    self.skip_reconfirmation!
    self.update_attributes({ :status => 200 }, :without_protection => true)
  end
end

初期化子/devise.rb

# Use this hook to configure devise mailer, warden hooks and so forth.
# Many of these configuration options can be set straight in your model.
Devise.setup do |config|

  ..
  ..

  # If true, requires any email changes to be confirmed (exactly the same way as
  # initial account confirmation) to be applied. Requires additional unconfirmed_email
  # db field (see migrations). Until confirmed new email is stored in
  # unconfirmed email column, and copied to email column on successful confirmation.
  config.reconfirmable = true

  ..
  ..

end

仕様/モデル/user_spec.rb

require 'spec_helper'

describe User do

  subject(:user) { User.create(:email => 'nobody@nobody.com', :password => 'abcdefghijk') }

  it "should only send one email during creation" do
    expect {
      user
    }.to change(ActionMailer::Base.deliveries, :count).by(1)
  end

  it "should set attribute in after_create as expected" do
    user.status.should eq(200)
  end

end

rspec テストを実行して、メールが 1 つだけ送信されることを確認すると、動作が確認されます。

..

0.87571 秒で終了 2 例、0 失敗

于 2013-03-11T18:09:29.940 に答える
2

素晴らしい解決策をありがとう、スティーブン!私はそれを試しましたが、confirm!メソッドに接続するのに完全に機能します。ただし、この場合、ユーザーが受信した電子メールの確認リンクをクリックすると、関数が呼び出されます(名前が示すとおり)。

別の方法は、メソッドにフックすることgenerate_confirmation_tokenです。これにより、確認トークンが作成されて電子メールが送信されるときに、メソッドが直接呼び出されます。

# app/models/user.rb
def generate_confirmation_token
  make_owner_an_account_member
  super # includes a call to save(validate: false), 
        # so be sure to call whatever you like beforehand
end

def make_owner_an_account_member
  self.account = owned_account if owned_account?
end

確認モジュールの関連ソース

于 2013-02-15T19:57:25.183 に答える