[users-module] UserWasLoggedIn event is fired without logged in?
Created 7 years ago by lckamalI 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.
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!
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(); } }
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!
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
Is it running? The provider that is.
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.
Can you share some code?