Help with User Module Login Redirect: Need to redirect users to particular urls depending on the user role when using standard /login
Created 6 years ago by b1gw0rm

I have two particular user roles setup where I would like to redirect users in each group to a different landing url when they login via the standard Pyro /login route. For sake of discussion, the landing url can be /group1 and /group2. Is Pyro setup to handle that with configuration? Or do I need to extend/customize the default handler? Any advice or insight is appreciated.

emergingdzns  —  6 years ago

@b1gw0rm you can add a listener to your module's service provider:

protected $listeners = [
    'Anomaly\UsersModule\User\Event\UserWasLoggedIn' => [
        // replace namespace, modulename, stream name and class with your own and create the Listener folder in your stream's folder
        'Namespace\ModuleName\StreamName\Listener\ClassYouWantToCall',
    ],
];

Then have your listener look something like:

<?php namespace Namespace\ModuleName\StreamName\Listener;

use Session;
use Anomaly\UsersModule\User\Event\UserWasLoggedIn;

class ClassYouWantToCall
{

    /**
     * Handle the event.
     *
     * @param UserWasLoggedIn $event
     */
    public function handle(UserWasLoggedIn $event)
    {
        if (Auth::user()->hasRole('special-role-1')) {
            return redirect()->to('/whatever-role-1-route-is');
        } else {
            return redirect()->to('/whatever-role-2-route-is');
        }
        return;
    }
}

The above is pretty rudimentary, but you should be able to figure it out from that example.