LoginSignup
2
4

More than 5 years have passed since last update.

論理削除実装時のdevise対応

Posted at

deviseによる認証を行っているrailsアプリで、kakurenbo-putiなんかで論理削除を実装しても、そのままではログインができてしまう。

論理削除したユーザーがログインしている場合は強制ログアウトし、ログインできないように、該当のモデルに下記のメソッドをオーバーライドすることで対応できる。
deleted_accountに、認証時のメッセージを登録する。

YourModel.ruby
  # ensure user account is active
  def active_for_authentication?
    super && !soft_destroyed_at
  end

  # provide a custom message for a deleted account
  def inactive_message
    !soft_destroyed_at ? super : :deleted_account
  end
config/locales/devise.ja.yml
ja:
  devise:
    failure:
      deleted_account: 'このアカウントは退会されています。'
2
4
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
2
4