How to stop soft deleted user's login with Devise

前端 未结 3 1871
小鲜肉
小鲜肉 2021-01-12 16:54

I currently use Devise for user registration/authentication in a Rails project. When a user wants to cancel their account, the user object is soft deleted in a way like the

3条回答
  •  梦谈多话
    2021-01-12 17:08

    To stop a user that has been 'soft deleted', the best way is to overwrite the find_for_authentication class method on the user model. Such as:

    Class User < ActiveRecord::Base
      def self.find_for_authentication(conditions)
        super(conditions.merge(:deleted_flag => false))
      end
    

    This will generate a invalid email or password flash message by devise (because it cannot find the user to authenticate)

    As far as your second question though, you'll need some for of method in your controller to add a particular flash message. However, in my opinion you should treat users that are 'soft' deleted the same as if they didn't exist in the database at all. Thus if they tried to log in, they should just get an valid email or password message.

提交回复
热议问题