Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-26T17:39:09.643Z
Answers.Text: Except that you just redefined what 'user' is, and thus will be saving 'undefined' — I changed it to '$existingUser'.
Answers.UserDisplayName: Douglas McDonald
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-26T18:09:18.437Z
Answers.Text: That's what I get for answering before coffee.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-26T16:42:27.117Z
Answers.LastActivityDate: 2015-02-26T17:42:04.380Z
Answers.LastEditDate: 2015-02-26T17:42:04.380Z
Answers.UserDisplayName: Brad Bell
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-02-26T14:05:17.743Z
Comments.Text: 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?
Comments.UserDisplayName: Mats Mikkel Rummelhoff
Tags: <controller><services>
Title: How to tell if user exists
ThreadText: # 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.  
  
  ---
  
  