Answers.Body: <p>Put this at the top of the file:</p>
  
  <pre><code>use craft\elements\User;
  </code></pre>
  
  <p>Within your function use the following code:</p>
  
  <pre><code>$user = new User();
  $user-&gt;pending = true;
  $user-&gt;username = $contact["login_email"];
  $user-&gt;firstName = $contact["first_name"];
  $user-&gt;lastName = $contact["last_name"];
  $user-&gt;email = $contact["login_email"];
  $user-&gt;passwordResetRequired = false;
  $user-&gt;validate(null, false);
  Craft::$app-&gt;getElements()-&gt;saveElement($user, false);
  Craft::$app-&gt;users-&gt;assignUserToGroups($user-&gt;id, [1]);
  Craft::$app-&gt;getUsers()-&gt;sendActivationEmail($user);
  </code></pre>
  
  <p>The sendActivationEmail is not a required step and can be removed but this code is working on a Craft 3 site I've been building and it creates user accounts.</p>
  
  <p>This assumes that a $contacts array has been set with all of the correct values, just swap these out for your own values or omit them if you don't have any. From here you should be able to customise it though for what you need once the user account is created.</p>
  
  <p>The final step for you to log in as that user would be to use something like this, this is untested however and may need some tweaking but has come from the <a href="https://docs.craftcms.com/api/v3/craft-web-user.html#method-loginbyuserid" rel="noreferrer">docs</a>:</p>
  
  <pre><code>$userLogin = new \Craft\web\User();
  $userLogin-&gt;loginByUserId($user-&gt;id);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-08T14:14:39.700Z
Answers.Text: Why do you initiate a new instance of the component rather than using the existing one? Do you want to avoid events?
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-11T01:40:02.813Z
Answers.Text: Robin,  how would you use the existing one?
Answers.UserDisplayName: David A McInnis
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-08-01T15:48:59.370Z
Answers.Text: I'm really confused how `$user->id` is set in your code. The `saveElement` method is not taking a `&$referenced` argument so the fact that it updates the ID in the model should mean nothing to the code calling it. What am I missing here?
Answers.UserDisplayName: Rick Kukiela
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-08-01T16:51:26.663Z
Answers.Text: n/m i totally forgot they changed object arguments are passed by reference by default after php5.
Answers.UserDisplayName: Rick Kukiela
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-08-24T14:52:37.383Z
Answers.Text: @DavidAaronMcInnis I believe you should be able to call the `Craft::$app->getUser()->login($user)` method.
Answers.UserDisplayName: Chris
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-08T14:06:58.867Z
Answers.LastActivityDate: 2018-11-08T14:06:58.867Z
Answers.UserDisplayName: Matt Shearing
Body: <p>How can I register and login a user in my Controller? I have tried something like this:</p>
  
  <pre><code>$newUser = new User($userName, $userPassword, $userEmail);
  Craft::$app-&gt;elements-&gt;saveUser($newUser);
  </code></pre>
  
Tags: <plugin-development><craft3><login><registration>
Title: Save and login user in a Plugin?
ThreadText: # Save and login user in a Plugin?
  
  (Asked by: username_0 on 2018-11-08)
  
  How can I register and login a user in my Controller? I have tried something like this:
  
  ```
  $newUser = new User($userName, $userPassword, $userEmail);
  Craft::$app->elements->saveUser($newUser);
  
  ```
  
  **Tags:** plugin-development, craft3, login, registration
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-11-08. Score: 5 | Accepted answer
  
  Put this at the top of the file:
  
  ```
  use craft\elements\User;
  
  ```
  
  Within your function use the following code:
  
  ```
  $user = new User();
  $user->pending = true;
  $user->username = $contact["login_email"];
  $user->firstName = $contact["first_name"];
  $user->lastName = $contact["last_name"];
  $user->email = $contact["login_email"];
  $user->passwordResetRequired = false;
  $user->validate(null, false);
  Craft::$app->getElements()->saveElement($user, false);
  Craft::$app->users->assignUserToGroups($user->id, [1]);
  Craft::$app->getUsers()->sendActivationEmail($user);
  
  ```
  
  The sendActivationEmail is not a required step and can be removed but this code is working on a Craft 3 site I've been building and it creates user accounts.
  
  This assumes that a $contacts array has been set with all of the correct values, just swap these out for your own values or omit them if you don't have any. From here you should be able to customise it though for what you need once the user account is created.
  
  The final step for you to log in as that user would be to use something like this, this is untested however and may need some tweaking but has come from the [docs](https://docs.craftcms.com/api/v3/craft-web-user.html#method-loginbyuserid):
  
  ```
  $userLogin = new \Craft\web\User();
  $userLogin->loginByUserId($user->id);
  
  ```
  
  > **Comments:**
  > **username_2**: Why do you initiate a new instance of the component rather than using the existing one? Do you want to avoid events?  
  > **username_3**: username_2,  how would you use the existing one?  
  > **username_4**: I'm really confused how `$user->id` is set in your code. The `saveElement` method is not taking a `&$referenced` argument so the fact that it updates the ID in the model should mean nothing to the code calling it. What am I missing here?  
  > **username_4**: n/m i totally forgot they changed object arguments are passed by reference by default after php5.  
  > **username_5**: @DavidAaronMcInnis I believe you should be able to call the `Craft::$app->getUser()->login($user)` method.  
  
  ---
  
  