<p>I'm trying to build a plugin wherein after a user is created via the normal means via a front end template, the user is immediately logged in.</p>

<p>My module is watching for the after save event on Elements for a User:</p>

<pre class="lang-php prettyprint-override"><code>Event::on(Elements::class, Elements::EVENT_AFTER_SAVE_ELEMENT, function(ElementEvent $event) {
    if ($event-&gt;element instanceof \craft\elements\User &amp;&amp; !ElementHelper::isDraftOrRevision($event-&gt;element)) {
        return Craft::$app-&gt;getModule('my-module')-&gt;users-&gt;handleCreateLogin($event);
    }
});
</code></pre>

<p>Then, <code>handleCreateLogin</code> looks like this:</p>

<pre class="lang-php prettyprint-override"><code>public function handleCreateLogin(ElementEvent $event)
{
    $user = $event-&gt;element;
    $isNewUser = $event-&gt;isNew;

    if ($user-&gt;can('Admin')) {
        // leave admins alone
        return;
    }

    if ($isNewUser &amp;&amp; Craft::$app-&gt;user-&gt;isGuest) {
        // This is a new user, so log in
        Craft::$app-&gt;user-&gt;loginByUserId($user-&gt;id);
    }
    return;
}
</code></pre>

<p>The event triggers fine, but after the user is created I'm greeted by this message:</p>

<pre><code>Call to a member function mergePreferences() on null
vendor/craftcms/cms/src/controllers/UsersController.php at line 1139
</code></pre>

<p>which is an error from the UserController's sanity check to see if the user is currently logged in.</p>

<p>According to the docs this is the accepted method, and it worked in a similar fashion in Craft 2. I'm guessing the save isn't complete when trying to redirect after logging in? Or some other bit of unfinished business.</p>

<p>Anyone have any tips or suggestions?</p>

CC BY-SA 4.0
2019-11-23T00:21:27.080Z
Would it be easier to use this? https://docs.craftcms.com/v3/config/config-settings.html#autologinafteraccountactivation
Brad Bell
CC BY-SA 4.0
2019-11-25T14:04:21.700Z
Thanks @BradBell, I did try that and it didn't seem to change anything, which made me wonder if there was something wonky with how the form was set up - but it's just a normal registration form pointing to the endpoint in the docs.
MikeJ
<plugin-development><craft3><modules><usermodel>
Save and log in a user from event callback
# Save and log in a user from event callback

(Asked by: username_0 on 2019-11-21)

I'm trying to build a plugin wherein after a user is created via the normal means via a front end template, the user is immediately logged in.
My module is watching for the after save event on Elements for a User:
Event::on(Elements::class, Elements::EVENT_AFTER_SAVE_ELEMENT, function(ElementEvent $event) {
    if ($event->element instanceof \craft\elements\User && !ElementHelper::isDraftOrRevision($event->element)) {
        return Craft::$app->getModule('my-module')->users->handleCreateLogin($event);
    }
});

Then, handleCreateLogin looks like this:
public function handleCreateLogin(ElementEvent $event)
{
    $user = $event->element;
    $isNewUser = $event->isNew;

    if ($user->can('Admin')) {
        // leave admins alone
        return;
    }

    if ($isNewUser && Craft::$app->user->isGuest) {
        // This is a new user, so log in
        Craft::$app->user->loginByUserId($user->id);
    }
    return;
}

The event triggers fine, but after the user is created I'm greeted by this message:
Call to a member function mergePreferences() on null
vendor/craftcms/cms/src/controllers/UsersController.php at line 1139

which is an error from the UserController's sanity check to see if the user is currently logged in.
According to the docs this is the accepted method, and it worked in a similar fashion in Craft 2. I'm guessing the save isn't complete when trying to redirect after logging in? Or some other bit of unfinished business.
Anyone have any tips or suggestions?

**Tags:** plugin-development, craft3, modules, usermodel
> **Comments:**
> **username_1**: Would it be easier to use this? https://docs.craftcms.com/v3/config/config-settings.html#autologinafteraccountactivation  
> **username_0**: Thanks @BradBell, I did try that and it didn't seem to change anything, which made me wonder if there was something wonky with how the form was set up - but it's just a normal registration form pointing to the endpoint in the docs.  

---

## 0 answers

