1

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!

2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.