5

Ryan Boland の優れた Rails マルチテナンシー チュートリアルに従っていますが、devise_invitable で問題が発生しました。私は使っている...

Rails 4.1.5 
devise 3.3.0  
devise_invitable 1.3.6
Postgresql

選択したサブドメイン (mysubdomain.lvh.me:3000) で新しいアカウントとユーザー/アカウント所有者を作成し、そこからユーザーの招待状を問題なく送信できます。シークレット Chrome セッションで招待リンクを開き、ログインしていないか、現在のセッションがないことを確認します。招待リンクをクリックすると、サインイン ページ (mysubdomain.lvh.me:3000/users/sign_in) にリダイレクトされ、「提供された招待トークンは無効です!」というフラッシュ通知が表示されます。

私は非常に単純なメーラー ビュー (app/views/devise/mailer/invitation_instructions.html.erb) を使用しています...

<%= link_to 'Accept invitation', accept_invitation_url(@resource, :invitation_token => @token) %>

ご覧のとおり、こちらで説明されているように、@token の使用を確認しました。

招待の作成時に、招待トークンがデータベースに保存されていることを確認しました (この場合、hey@test.com - d1801fd8df78bd8cd125d5d8091fdc6a72c8f8faf4136cb282d497ec612195e9)。これが、承認リクエスト時の招待ルックアップのトークンと一致することを確認しました (以下のトレースを参照)。それでも、サインアップを完了するのではなく、ユーザーのサインイン ページにリダイレクトし、トレース ログに「フィルター チェーンが :resource_from_invitation_token レンダリングまたはリダイレクトされたため停止しました」と表示します。ユーザーは、このトランザクションの後、最終的に未確認のままになります。

ここで私にとって何がうまくいかないのかについてのアイデアはありますか? ログ、アプリケーションコントローラー、およびデバイス構成を以下に含めています...

招待状作成のトレース ログは次のとおりです。

Started POST "/users/invitation" for 127.0.0.1 at 2014-09-07 01:28:33 +0800
Processing by Devise::InvitationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"BiIQ95wwdQz3CJ0+OoLOE9xHHvxhloHsRHrxsqf1D2Q=", "user"=>{"email"=>"hey@test.com"}, "commit"=>"Invite User"}
  User Load (4.1ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 1  ORDER BY "users"."id" ASC LIMIT 1
  Account Load (0.4ms)  SELECT  "public"."accounts".* FROM "public"."accounts"  WHERE "public"."accounts"."subdomain" = 'mysubdomain' LIMIT 1
  User Load (0.7ms)  SELECT  "users".* FROM "users"  WHERE "users"."email" = 'hey@test.com'  ORDER BY "users"."id" ASC LIMIT 1
  User Load (0.7ms)  SELECT  "users".* FROM "users"  WHERE "users"."invitation_token" = 'd1801fd8df78bd8cd125d5d8091fdc6a72c8f8faf4136cb282d497ec612195e9'  ORDER BY "users"."id" ASC LIMIT 1
   (0.1ms)  BEGIN
  SQL (0.5ms)  INSERT INTO "users" ("created_at", "email", "invitation_created_at", "invitation_sent_at", "invitation_token", "invited_by_id", "invited_by_type", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id"  [["created_at", "2014-09-06 17:28:34.296123"], ["email", "hey@test.com"], ["invitation_created_at", "2014-09-06 17:28:34.294987"], ["invitation_sent_at", "2014-09-06 17:28:34.294987"], ["invitation_token", "d1801fd8df78bd8cd125d5d8091fdc6a72c8f8faf4136cb282d497ec612195e9"], ["invited_by_id", 1], ["invited_by_type", "User"], ["updated_at", "2014-09-06 17:28:34.296123"]]
   (2.2ms)  COMMIT
  Rendered devise/mailer/invitation_instructions.html.erb (1.3ms)

Devise::Mailer#invitation_instructions: processed outbound mail in 23.5ms

Sent mail to hey@test.com (26.0ms)
Date: Sun, 07 Sep 2014 01:28:34 +0800
From: please-change-me-at-config-initializers-devise@example.com
Reply-To: please-change-me-at-config-initializers-devise@example.com
To: hey@test.com
Message-ID: <...>
Subject: Invitation instructions
Mime-Version: 1.0
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

<a href="http://mysubdomain.lvh.me:3000/users/invitation/accept?invitation_token=3GXDmi7NntDRdhvo57q5">Accept invitation</a>
Redirected to http://mysubdomain.lvh.me:3000/users
Completed 302 Found in 888ms (ActiveRecord: 10.0ms)

招待リンクをたどったときのトレースは次のとおりです...

Started GET "/users/invitation/accept?invitation_token=3GXDmi7NntDRdhvo57q5" for 127.0.0.1 at 2014-09-07 01:28:38 +0800
Processing by Devise::InvitationsController#edit as HTML
  Parameters: {"invitation_token"=>"3GXDmi7NntDRdhvo57q5"}
  User Load (0.6ms)  SELECT  "users".* FROM "users"  WHERE "users"."invitation_token" = 'd1801fd8df78bd8cd125d5d8091fdc6a72c8f8faf4136cb282d497ec612195e9'  ORDER BY "users"."id" ASC LIMIT 1
Redirected to http://mysubdomain.lvh.me:3000/users/sign_in
Filter chain halted as :resource_from_invitation_token rendered or redirected
Completed 302 Found in 5ms (ActiveRecord: 0.6ms)


Started GET "/users/sign_in" for 127.0.0.1 at 2014-09-07 01:28:38 +0800
Processing by Devise::SessionsController#new as HTML
  Account Load (0.4ms)  SELECT  "public"."accounts".* FROM "public"."accounts"  WHERE "public"."accounts"."subdomain" = 'mysubdomain' LIMIT 1
  Rendered devise/shared/_links.erb (0.7ms)
  Rendered devise/sessions/new.html.erb within layouts/application (4.4ms)
Completed 200 OK in 21ms (Views: 16.6ms | ActiveRecord: 1.3ms)

これが私のapplication_controllerです...

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  before_filter :load_schema, :authenticate_user!, :set_mailer_host
  before_filter :configure_permitted_parameters, if: :devise_controller?


  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:first_name, :last_name, :company, :email, :password, :password_confirmation, :remember_me, :image, :image_cache)}
    devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:first_name, :last_name, :company, :email, :password_confirmation, :current_password, :image, :image_cache)}
  end

private
  def load_schema
    Apartment::Database.switch('public')
    return unless request.subdomain.present?

    if current_account
      Apartment::Database.switch(current_account.subdomain)
    else
      redirect_to root_url(subdomain: false)
    end
  end  

  def current_account
    @current_account ||= Account.find_by(subdomain: request.subdomain)
  end
  helper_method :current_account

  def set_mailer_host
    subdomain = current_account ? "#{current_account.subdomain}." : ""
    ActionMailer::Base.default_url_options[:host] = "#{subdomain}lvh.me:3000"
  end

  def after_sign_out_path_for(resource_or_scope)
    new_user_session_path
  end

  def after_invite_path_for(resource)
    users_path
  end

end

これが私のDeviseイニシャライザ(config/initializers/devise.rb)です。「config.allow_insecure_token_lookup = true」という行を追加して、これが役立つかどうかを確認しましたが、役に立たなかった...

Devise.setup do |config|

  config.mailer_sender = 'please-change-me-at-config-initializers-devise@example.com'

  require 'devise/orm/active_record'

  config.case_insensitive_keys = [ :email ]

  config.strip_whitespace_keys = [ :email ]

  config.skip_session_storage = [:http_auth]

  config.stretches = Rails.env.test? ? 1 : 10

  config.reconfirmable = true

  config.expire_all_remember_me_on_sign_out = true

  config.password_length = 8..128

  config.sign_out_via = :delete

  config.allow_insecure_token_lookup = true
end
4

1 に答える 1

1

コメントしたいのですが、36ポイントしかなく、許可されていないため、不完全な回答です:

これは、リクエストをリダイレクトしているdevise_invitable InvitationsControllerのコードです

def resource_from_invitation_token
  unless params[:invitation_token] && self.resource = resource_class.find_by_invitation_token(params[:invitation_token], true)
    set_flash_message(:alert, :invitation_token_invalid)
    redirect_to after_sign_out_path_for(resource_name)
  end
end

Railsコンソールで次を実行してみてください:

token = '3GXDmi7NntDRdhvo57q5' #the token sent in the invitation email
User.find_by_invitation_token(token, true)

それがあなたのユーザーを返すかどうかを確認してください。おそらくそうではありませんが、これにより答えに近づくことができます。そうだといい。

于 2014-10-09T03:54:46.760 に答える