Log in User (Craft 3) How can I log in a user in Craft 3 and redirect to a url?

This doesn't work:

$username = $user-&gt;username;
$userCode = $user-&gt;teamCode;
Craft::$app-&gt;user-&gt;login($username, $userCode);


Thanks!
Documentation for this can be found in Craft's class reference.

# 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();


Or, if you're in a controller:

return $this-&gt;redirect($url);