ruby-on-railsdevisedevise-invitable

Check if user is valid for authentication only if uses database authenticable


Users can sign into our application by OmniAuth or by email and password (but only if they were invited).

We have a method, which checks if the user can sign in by email and password as follow:

def active_for_authentication?
  super && valid_for_authentication
end

private
# Check wheter user was invited or not   
def valid_for_authentication
  User.invitation_accepted.find_by(id: id).present?
end

This works fine, but when I want to log in by OmniAuth, this method will block me. Can I specify to bypass this method if OmniAuth authentication is used?


Solution

  • You can check below method before executing active_for_authentication? lines

    The below lines will If the returned some res data if login using omniauth add logic unless res.present?

    def logged_using_omniauth? request
      res = nil
      omniauth = request.env["omniauth.auth"]
      res = Authentication.find_by_provider_and_uid 
      (omniauth['provider'], omniauth['uid']) if omniauth
      res  
    end