28
29

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Deviseでセッション作成時にwardenの認証が失敗した場合の遷移を変える

Posted at
app/controllers/users/sessions_controller.rb
class Users::SessionsController < Devise::SessionsController
  # POST /resource/sign_in
  def create
    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

  # 認証が失敗した場合に呼び出されるアクション
  def failed
    # warden で出力されたエラーを保存する
    flash[:notice] = flash[:notice].to_a.concat [flash[:alert]]
    redirect_to root_path
  end

  protected
  def auth_options
    # 失敗時に recall に設定したパスのアクションが呼び出されるので変更
    # { scope: resource_name, recall: "#{controller_path}#new" } # デフォルト
    { scope: resource_name, recall: "#{controller_path}#failed" }
  end
end

失敗時に sessions/new ではなく root にリダイレクトしたかったんだけど Sessions#new をオーバーライドしたほうがいいのかも。
普通に作ってれば全然必要ない気がするし設計に問題があるかもっておうおう

28
29
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
28
29

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?