<p>Documentation for this can be found in <a href="https://docs.craftcms.com/api/v3/craft-web-user.html#method-loginbyuserid" rel="nofollow noreferrer">Craft's class reference</a>.</p>

<pre><code># Login as user, by id
Craft::$app-&gt;getUser()-&gt;loginByUserId($user-&gt;id);

# Redirect
return Craft::$app-&gt;getResponse()-&gt;redirect($url)-&gt;send();
</code></pre>

<p>Or, if you're in a controller:</p>

<pre><code>return $this-&gt;redirect($url);
</code></pre>

CC BY-SA 4.0
2018-11-07T12:14:45.137Z
2018-11-07T12:47:43.167Z
2018-11-07T12:47:43.167Z
Paul
<p>How can I log in a user in Craft 3 and redirect to a url?</p>

<p>This doesn't work:</p>

<pre><code>$username = $user-&gt;username;
$userCode = $user-&gt;teamCode;
Craft::$app-&gt;user-&gt;login($username, $userCode);
</code></pre>

<p>Thanks!</p>

<plugin-development><craft3><users><login>
Log in User (Craft 3)
# Log in User (Craft 3)

(Asked by: username_0 on 2018-11-07)

How can I log in a user in Craft 3 and redirect to a url?
This doesn't work:
$username = $user->username;
$userCode = $user->teamCode;
Craft::$app->user->login($username, $userCode);

Thanks!

**Tags:** plugin-development, craft3, users, login

---

## 1 answers

### Answer by username_1 on 2018-11-07. Score: 3 | Accepted answer

Documentation for this can be found in Craft's class reference.
# Login as user, by id
Craft::$app->getUser()->loginByUserId($user->id);

# Redirect
return Craft::$app->getResponse()->redirect($url)->send();

Or, if you're in a controller:
return $this->redirect($url);


---

