AJAX リクエストを処理するために Devise (3.1.1) サインイン/サインアップ メソッドを拡張しようとしていますが、確認可能なロジックで行き詰っています。通常、ユーザーがアカウントを確認する前に Devise にサインインすると、「続行する前にアカウントを確認する必要があります」というフラッシュ メッセージとともにログイン画面にリダイレクトされます。Devise がどこで確認を確認し、リダイレクトの決定を行っているかを把握することはできません。
これが拡張session_controllerコードです。成功したログイン試行と失敗したログイン試行に対して正常に機能します。
# CUSTOM (Mix of actual devise controller method and ajax customization from http://natashatherobot.com/devise-rails-sign-in/):
def create
# (NOTE: If user is not confirmed, execution will never get this far...)
respond_to do |format|
format.html do
# Copied from original create method:
self.resource = warden.authenticate!(auth_options)
set_flash_message(:notice, :signed_in) if is_navigational_format?
sign_in(resource_name, resource)
respond_with resource, :location => after_sign_in_path_for(resource)
end
format.js do
# Derived from Natasha AJAX recipe:
self.resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#failure")
sign_in(resource_name, resource)
return render :json => {:success => true, :token => form_authenticity_token() }, content_type: "application/json" # Need to explicitely set content type to JSON, otherwise it gets set as application/javascript and success handler never gets hit.
end
end
end
def failure
return render :json => {:success => false, :errors => ["Login failed."]}
end
問題は、ユーザーが未確認の場合、create
メソッドがヒットしないことです。リダイレクトは以前のどこかで発生します。つまり、JS に適した方法で処理することはできません。しかし、ソースを調べてみると、確認可能なチェックを行う前のフィルターが見つかりません。確認可能なチェックはどこで行われ、どのように傍受できますか?