[users-module] UserWasLoggedIn event is fired without logged in?
Created 7 years ago by lckamal

I was adding a listener for event UserWasLoggedIn to redirect on different dashboard of logged in user. But on redirect, Unfortunately user is not logged in.

ryanthompson  —  7 years ago

Can you share some code?

ryanthompson  —  7 years ago

This is the class that handles the login form: https://github.com/anomalylabs/users-module/blob/master/src/User/Login/LoginFormHandler.php

Perhaps a little work is needed but a good way to approach this I think is to use the onPosted callback to set the option.

Depending on what you find there - maybe there are some modifications that will make this easier. I know I need to go through and add more callback points but check it out and keep me posted!

lckamal  —  7 years ago

I tried like this. on themeService provider:

        'Anomaly\UsersModule\User\Event\UserWasLoggedIn'                  => [
            'Skrollx\JobingTheme\Listener\RedirectToRole',
        ],
    ];```
and on RedirectToRole:

public function handle(UserWasLoggedIn $event) { $user = $event->getUser(); if($user->hasRole('admin')){ return redirect('admin')->send(); } if($user->hasRole('employer')){ return redirect('employer/dashboard')->send(); } if($user->hasRole('user')){ return redirect('user/dashboard')->send(); } }

lckamal  —  7 years ago

I have seen that you have mentioned using callback couple of times. Can you give a heads up on how to use the callback? I have used on formbuilder but loginFormBuilder is from core so, wanted a way to extend the callback.

ryanthompson  —  7 years ago

Sure! So let's assume you have a service provider somewhere - anywhere and let's use the boot method so that everything is registered:

PS I think you actually wanna use listen which is a callback that applies to all instances of the class: https://pyrocms.com/documentation/streams-platform/v1.1#services/callbacks/basic-usage/firescallbacks-listen

public function boot(LoginFormBuilder $builder)
{
    $builder->listen('posting', function() use ($builder) {
        // Do stuff ...
    }
}

Hope this helps!

lckamal  —  7 years ago

I made a new Provider:

<?php namespace Skrollx\JobingTheme\Providers;

use Anomaly\UsersModule\User\Login\LoginFormBuilder;
use Illuminate\Support\ServiceProvider;

class PostLoginServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot(LoginFormBuilder $builder)
    {
        $builder->listen('posted', function() use ($builder) {
            dd($builder);
        });
    }
}

Added this provider on theme serviceprivider:

protected $providers = [
    'Skrollx\JobingTheme\Providers\PostLoginServiceProvider'
];

I tried posted, onPost, post etc.. Nothing worked

ryanthompson  —  7 years ago

Is it running? The provider that is.

lckamal  —  7 years ago

Yes. Provider is running. When I add exit() inside boot. website exits.

ryanthompson  —  7 years ago

It should be getting hit here: https://github.com/anomalylabs/streams-platform/blob/master/src/Ui/Form/FormBuilder.php#L238

Maybe make sure it's in the callbacks at that time. The FiresCallbacks trait is responsible for it all.

lckamal  —  7 years ago

not working. any other way?