How to tell if user exists I am automatically creating users in a service, is there a way to check if user exists?

I want to log in id user exist or save a new user if it doesn't, I have the following so far which will add a user:

public function add($user)
{
    craft()-&gt;users-&gt;saveUser($user);

    $user = craft()-&gt;users-&gt;getUserByUsernameOrEmail($username);
    craft()-&gt;userSession-&gt;loginByUserId($user-&gt;attributes['id']);   
}
Something like this should work:

public function add($user)
{
    $existingUser = craft()-&gt;users-&gt;getUserByUsernameOrEmail($user-&gt;username);

    if ($existingUser)
    {
        // They exist... log them in.
        craft()-&gt;userSession-&gt;loginByUserId($existingUser-&gt;id);  
    }
    else
    {
        // They don't exist... create them.
        craft()-&gt;users-&gt;saveUser($user);
    }
}