LoginSignup
4
0

More than 3 years have passed since last update.

Deviseを使ってController側からログアウトさせる

Posted at

1. 経緯

userテーブルにflgを持たせて、0以外の人はログインさせたくないなぁ・・・
ということで調べてみたところ以下の内容でできました:raised_hands:

2. 結果

appllication_controller.rb
  before_action :sign_out_user,  if: :user_signed_in?
  protected
    # ログイン後のパス
    def after_sign_in_path_for(resource)
      root_path
    end
    # flgが0以外はログアウトさせる
    def sign_out_user
      sign_out_and_redirect(current_user) if current_user.flg == 0
    end

3. 詳しい内容

4. ネットの記事

ネットの記事見てると以下の内容とかもあったんですが、サインアウトはHTTPリクエストからしか
受け付けないみたいで以下の書き方をしてもダメでした・・・。

redirect_to :controller=>"user/sessions", :action=>"destroy"

4. 最後に

いいね!もらえると励みになるのでよろしくお願いします!:v:

4
0
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
4
0