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: 'このアカウントは退会されています。'