Search code examples
ruby-on-railsrubydevise

Ruby on Rails: Edit Sessions controller create action to check if user is active


I need to alter the Sessions controller create method to first check if the user has a column filled out for deactivation_date.

If that column has a value, the user IS deactivated and the sign in authentication should fail in the same way as if the user doesn't exist.

class Users::SessionsController < Devise::SessionsController



    def create
        logger.debug('create')
        self.resource = warden.authenticate!(auth_options)
        if self.resource.deactivation_date?
            logger.debug('user has been deactivated')
            flash.now[:notice] = "Sorry, this account has been deactivated."
        else

            set_flash_message!(:notice, :signed_in)
            sign_in(resource_name, resource)
            respond_with resource, location: after_sign_in_path_for(resource)
        end
      end


end

I've tried overriding the sessions controller as well as the create method.

My code above is only preventing a redirect. Once I refresh the page, the user is authenticated no matter what.

Could someone help point me in the right direction?


Solution

  • The most simple way to do this is really just add a custom valid_for_authentication? method in the resource devise is using, so in Users.rb:

    def valid_for_authentication?
        super && !deactivation_date?
    end