<p>Something like this should work:</p>

<pre><code>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);
    }
}
</code></pre>

CC BY-SA 3.0
2015-02-26T17:39:09.643Z
Except that you just redefined what 'user' is, and thus will be saving 'undefined' — I changed it to '$existingUser'.
Douglas McDonald
CC BY-SA 3.0
2015-02-26T18:09:18.437Z
That's what I get for answering before coffee.
Brad Bell
CC BY-SA 3.0
2015-02-26T16:42:27.117Z
2015-02-26T17:42:04.380Z
2015-02-26T17:42:04.380Z
Brad Bell
<p>I am automatically creating users in a service, is there a way to check if user exists?</p>

<p>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:</p>

<pre><code>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']);   
}
</code></pre>

CC BY-SA 3.0
2015-02-26T14:05:17.743Z
Well, `craft()->users->getUserByUsernameOrEmail()` will return `NULL` if no users match the username or email address given – can't you just do a call to that method and check the return value before attempting to create a new user?
Mats Mikkel Rummelhoff
<controller><services>
How to tell if user exists
# How to tell if user exists

(Asked by: username_0 on 2015-02-26)

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()->users->saveUser($user);

    $user = craft()->users->getUserByUsernameOrEmail($username);
    craft()->userSession->loginByUserId($user->attributes['id']);   
}

**Tags:** controller, services
> **Comments:**
> **username_1**: Well, `craft()->users->getUserByUsernameOrEmail()` will return `NULL` if no users match the username or email address given – can't you just do a call to that method and check the return value before attempting to create a new user?  

---

## 1 answers

### Answer by username_2 on 2015-02-26. Score: 6 | Accepted answer

Something like this should work:
public function add($user)
{
    $existingUser = craft()->users->getUserByUsernameOrEmail($user->username);

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

> **Comments:**
> **username_3**: Except that you just redefined what 'user' is, and thus will be saving 'undefined' — I changed it to '$existingUser'.  
> **username_2**: That's what I get for answering before coffee.  

---

