44

Rails 4 と Devise 3.1.0 を Web アプリで使用しています。ユーザーのサインアップをテストする Cucumber テストを作成しました。電子メールから「アカウントの確認」リンクをクリックすると失敗します。

Scenario: User signs up with valid data                                                           # features/users/sign_up.feature:9
    When I sign up with valid user data                                                             # features/step_definitions/user_steps.rb:87
    Then I should receive an email                                                                  # features/step_definitions/email_steps.rb:51
    When I open the email                                                                           # features/step_definitions/email_steps.rb:76
    Then I should see the email delivered from "no-reply@mysite.com"                                # features/step_definitions/email_steps.rb:116
    And I should see "You can confirm your account email through the link below:" in the email body # features/step_definitions/email_steps.rb:108
    When I follow "Confirm my account" in the email                                                 # features/step_definitions/email_steps.rb:178
    Then I should be signed in                                                                      # features/step_definitions/user_steps.rb:142
      expected to find text "Logout" in "...Confirmation token is invalid..." (RSpec::Expectations::ExpectationNotMetError)
     ./features/step_definitions/user_steps.rb:143:in `/^I should be signed in$

このエラーは、Web サーバーから手動でサインアップした場合にも再現されるため、Cucumber の問題ではないようです。

をお願いします:

  • ユーザーは、この電子メールのリンクからワンクリックでアカウントを確認できます
  • アカウントを確認した後、ユーザーにサインインしたままにする

私はセットアップしました:

  • GitHub の最新の Devise コード (3.1.0、ref 041fcf90807df5efded5fdcd53ced80544e7430f)
  • 実装するUserクラスconfirmable
  • 「デフォルト」の確認コントローラーを使用する (独自のカスタム コントローラーを定義していません)。

私はこれらの投稿を読みました:

そして試しました:

  • 私のDeviseイニシャライザで設定config.allow_insecure_tokens_lookup = trueすると、起動時に「不明なメソッド」エラーがスローされます。さらに、これは一時的な修正にすぎないように思われるため、使用は避けたいと思います。
  • DB をパージし、ゼロから開始しました (したがって、古いトークンは存在しません)

アップデート:

登録後に に保存されている確認トークンを確認しますUser。email トークンは DBs トークンと一致します。上記の投稿によると、Devise の新しい動作は想定されておらず、代わりに電子メールのトークンに基づいて 2 番目のトークンを生成する必要があると述べています。これは疑わしい。実行User.confirm_by_token('[EMAIL_CONFIRMATION_TOKEN]')すると、エラー セット "@messages={:confirmation_token=>["is invalid"]}" を持つユーザーが返されます。これが問題の原因であると思われます。

トークンの不一致が問題の核心であるようです。コンソールで次のコードを実行して、ユーザーの Confirmation_token を手動で変更すると、確認が成功します。

new_token = Devise.token_generator.digest(User, :confirmation_token, '[EMAIL_TOKEN]')
u = User.first
u.confirmation_token = new_token
u.save
User.confirm_by_token('[EMAIL_TOKEN]') # Succeeds

では、そもそも間違った確認トークンを DB に保存するのはなぜでしょうか? 私はカスタム登録コントローラを使用しています...何かが間違って設定されている可能性がありますか?

ルート.rb

  devise_for  :users,
          :path => '',
          :path_names => {
            :sign_in => 'login',
            :sign_out => 'logout',
            :sign_up => 'register'
            },
          :controllers => {
            :registrations => "users/registrations",
            :sessions => "users/sessions"
          }

users/registrations_controller.rb :

class Users::RegistrationsController < Devise::RegistrationsController

  def create
    # Custom code to fix DateTime issue
    Utils::convert_params_date_select params[:user][:profile_attributes], :birthday, nil, true

    super
  end

  def sign_up_params
    # TODO: Still need to fix this. Strong parameters with nested attributes not working.
    #       Permitting all is a security hazard.
    params.require(:user).permit!
    #params.require(:user).permit(:email, :password, :password_confirmation, :profile_attributes)
  end
  private :sign_up_params
end
4

3 に答える 3

97

そのため、Devise 3.1.0 にアップグレードすると、しばらく触れていなかったビューに「残骸」が残りました。

このブログ投稿によると、Devise メーラーを@token古いものではなく使用するように変更する必要があります@resource.confirmation_token

これを見つけて、app/views/<user>/mailer/confirmation_instructions.html.erb次のように変更します。

<p>Welcome <%= @resource.email %>!</p>
<p>You can confirm your account email through the link below:</p>
<p><%= link_to 'Confirm my account', confirmation_url(@resource, :confirmation_token => @token) %></p>

これにより、発生しているトークンベースの確認の問題が修正されます。これにより、ロック解除またはパスワード トークンのリセットの問題も解決される可能性があります。

于 2013-09-07T15:57:29.023 に答える