<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>

CC BY-SA 4.0
2018-11-08T14:14:39.700Z
Why do you initiate a new instance of the component rather than using the existing one? Do you want to avoid events?
Robin Schambach
CC BY-SA 4.0
2018-12-11T01:40:02.813Z
Robin,  how would you use the existing one?
David A McInnis
CC BY-SA 4.0
2019-08-01T15:48:59.370Z
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?
Rick Kukiela
CC BY-SA 4.0
2019-08-01T16:51:26.663Z
n/m i totally forgot they changed object arguments are passed by reference by default after php5.
Rick Kukiela
CC BY-SA 4.0
2019-08-24T14:52:37.383Z
@DavidAaronMcInnis I believe you should be able to call the `Craft::$app->getUser()->login($user)` method.
Chris
CC BY-SA 4.0
2018-11-08T14:06:58.867Z
2018-11-08T14:06:58.867Z
Matt Shearing
<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>

<plugin-development><craft3><login><registration>
Save and login user in a Plugin?
# 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:
$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**: Robin,  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.  

---

