Users - Auto-login after registered?

emergingdzns - 6 months ago

I have two clients that both want to have it so that when the user registers, they are automatically and immediately logged in. Is this possible at all with the system already or am I going to have to hijack the registration process and custom build it myself?

Answer

huglester - 6 months ago

Hello,

edit app/streams.php > singletons with: '\Anomaly\UsersModule\User\Register\RegisterFormBuilder' => '\App\Extensions\User\RegisterFormBuilder', content like so:

<?php

namespace App\Extensions\User;

use Anomaly\UsersModule\User\Register\RegisterFormBuilder as BaseRegisterFormBuilder;

class RegisterFormBuilder extends BaseRegisterFormBuilder
{

    protected $handler = RegisterFormHandler::class;

}

And the RegisterFormHandler.php:

<?php

namespace App\Extensions\User;

use Anomaly\CustomersModule\Customer\CustomerModel;
use Anomaly\Streams\Platform\Traits\Transmitter;
use Anomaly\UsersModule\User\Contract\UserInterface;
use Anomaly\UsersModule\User\Notification\UserHasRegistered;
use Anomaly\UsersModule\User\Register\Command\HandleEmailRegistration;
use Anomaly\UsersModule\User\Register\Command\HandleManualRegistration;
use Anomaly\UsersModule\User\UserActivator;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Foundation\Bus\DispatchesJobs;

class RegisterFormHandler
{

    use Transmitter;
    use DispatchesJobs;

    public function handle(Repository $config, RegisterFormBuilder $builder, UserActivator $activator)
    {
        if (! $builder->canSave()) {
            return;
        }

        auth()->login($user);
    }
}

// maybe you will need to copy of some login from Default Handler... but you should get the idea :)

ryanthompson - 6 months ago

This was actually pushed and done this last weekend. Already like that now ^_^

emergingdzns - 6 months ago

Thanks @ryanthompson. I just ran composer update and tested this but it still didn't log the user in after registering. I've got the Activation Mode set to "Automatically activate the user after they register."

Is there something I'm doing wrong?

ryanthompson - 6 months ago

No let me double check that mode. Write up a quick issue cause Ima be AFK for a while.

emergingdzns - 6 months ago

Done. Thanks bro.

emergingdzns - 6 months ago

I can see that it was added to the event handler for the user activation, which is done when the user gets an email to activate. https://github.com/anomalylabs/users-module/blob/2.2/src/User/Register/Command/HandleActivateRequest.php#L44

But when the activation is automatic, it's not there: Unless I'm missing something, we need to add the UserAuthenticator to the function and then add the login line correct?

emergingdzns - 5 months ago

Thank you sir!