0
0

devise 退会済みユーザー ログイン実行「すでにログインしています。」と出る件

Posted at

はじめに

ポートフォリオの最終チェックしていると、退会済みユーザーがログインできてしまうことが発覚😱
前まではちゃんとブロックできてたのに…
ということで解決したのでアウトプットしておきます✍️

現象

退会済みのユーザー情報でログインを実行するとこのようになります

スクリーンショット 2024-07-17 16.13.27.png

sessions_controllre.rb
# frozen_string_literal: true

class Public::SessionsController < Devise::SessionsController
  before_action :user_status, only: [:create]
:
  private

  # アクティブ判断メソッド
  def user_status
    return if params[:user].nil?
    # emailからアカウントを1件取得
    user = User.find_by(email: params[:user][:email])
    return if user.nil?
    return unless user.valid_password?(params[:user][:password])
    unless user.is_active?
      flash[:alert] = "退会済みです。別のメールアドレスをお使いください。"
      redirect_to new_user_session_path
    end
  end

ログはこんな感じ↓

Started POST "/users/sign_in" for 180.0.182.32 at 2024-07-17 06:59:45 +0000
Cannot render console from 180.0.182.32! Allowed networks: 127.0.0.0/127.255.255.255, ::1
Processing by Public::SessionsController#create as HTML
  Parameters: {"authenticity_token"=>"[FILTERED]", "user"=>{"email"=>"aaa@aaa.com", "password"=>"[FILTERED]"}, "commit"=>"ログイン"}
  User Load (0.1ms)  SELECT "users".* FROM "users" WHERE "users"."email" = ? ORDER BY "users"."id" ASC LIMIT ?  [["email", "aaa@aaa.com"], ["LIMIT", 1]]
  ↳ app/controllers/application_controller.rb:8:in set_unread_notifications_count'
   (0.3ms)  SELECT COUNT(*) FROM "notifications" WHERE "notifications"."user_id" = ? AND "notifications"."read" = ?  [["user_id", 25], ["read", 0]]
  ↳ app/controllers/application_controller.rb:8:in set_unread_notifications_count'
   (0.1ms)  SELECT "user_rooms"."room_id" FROM "user_rooms" WHERE "user_rooms"."user_id" = ?  [["user_id", 25]]
  ↳ app/controllers/application_controller.rb:14:in set_unread_chats_count'
   (0.1ms)  SELECT COUNT(*) FROM "chats" WHERE 1=0 AND "chats"."read" = ? AND "chats"."user_id" != ?  [["read", 0], ["user_id", 25]]
  ↳ app/controllers/application_controller.rb:16:in set_unread_chats_count'
  User Load (0.1ms)  SELECT "users".* FROM "users" WHERE "users"."email" = ? LIMIT ?  [["email", "aaa@aaa.com"], ["LIMIT", 1]]
  ↳ app/controllers/public/sessions_controller.rb:39:in user_status'
Redirected to https://47518c0581d54d4ba18e08e28092dd6e.vfs.cloud9.ap-northeast-1.amazonaws.com/users/sign_in
Filter chain halted as :user_status rendered or redirected
Completed 302 Found in 603ms (ActiveRecord: 0.7ms | Allocations: 4411)

むむむ…user_statusにはちゃんと行っているようですね🧐

解決編

結論から言うとbefore_actionをprepend_before_actionに変えます!

sessions_controller.rb
class Public::SessionsController < Devise::SessionsController
  prepend_before_action :user_status, only: [:create]
:
end

repend_before_actionとは 🌱
Railsのコントローラで使われるフィルタ(コールバック)の一種です
before_actionと同様に特定のアクションの前に実行されますが、
prepend_before_actionは他のフィルタよりも前に実行されます

今回はbefore_actionのuser_statusがDeviseのデフォルトのセッション処理よりも後に実行されてしまったためログインできてしまっていました
そのため、repend_before_actionを使用することでセッション処理よりも前にuser_statusを実行することができます!

無事ブロックできました!

スクリーンショット 2024-07-17 16.25.39.png

さいごに

検索しても全然同じ事象にヒットしなかったためまとめました
もし同じ方がいらっしゃれば参考になれば幸いです!

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