<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def authenticate(self, username=None, password=None, **kwargs):
 """Allow users to log in with their email address or username."""

        try:
           # Try to fetch the user by searching the username or email field
            user = get_user_model().objects.filter(Q(username=username)|Q(email=username))[0]
            if check_password(password, user.password):
                return user
            else:
                return None
        except Exception as e:
            # No user was found, return None - triggers default login failed
            return None