Your simple missing a else in the condition,
if (empty($u)) {
return redirect()->route('unexpected');
}
else
{
Auth::login($u);
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Greetings, I have a listener, which receives an email address, checks if there is a user with that email address, and logins the user if exists. I am sure the handle function of the listener is called. But Auth::login does nothing there:
public function handle(LoginEvent $event)
{
$email = $event->getEmail();
$u = User::where('email', $email )->first();
if (empty($u)) {
return redirect()->route('unexpected');
}
Auth::login($u);
}
If I run the same code in an ordinary web guard route, it logins the user. All I change is giving an existing user's email address:
$u = User::where('email', '[email protected]' )->first();
So what I am missing in the listener, which prevents Auth::login to run successfully?
thanks
Test route
Route::get('/test', function () {
event(new App\Events\BodgeMeIn());
return redirect('/');
});
Event (is untouched)
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class BodgeMeIn
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}
Listener
<?php
namespace App\Listeners;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\User;
use Auth;
class listenBodgeMeIn
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param object $event
* @return void
*/
public function handle($event)
{
dump('event listened to');
$u = User::find(1);
Auth::login($u);
dump(Auth::user());
}
}
Please or to participate in this conversation.