Laravel Login Event Listener Example Tutorial

Laravel event listener example; In this tutorial we will show you how to use login event listener in laravel application. Sometimes we need to display welcome back message to user or some specific use after successfully login. In laravel we can do anything easily the login, register, logout etc event or listener.

Here we use the default login event and use it via custom created listener via eventServiceProvider. Sometime we need Fire an action when user is logged in or verified with Laravel, there are two ways of handle this actions, Here we are showing success message after user login successfully.

Follow the following steps to add login event listeners in laravel app;

Step 1: Create a Listener

First you will need to create a logini listener. To make a listener run the following artisan command:

php artisan make:listener LoginSuccessful --event=Illuminate\Auth\Events\Login

This command specify the name of the login event on creating the listener. We can create a listener without specify an event.

Next, go to app/Listeners directory and open LoginSuccessful.php file. Inside the handle() method we add our code whats we need to display or anything after login by user.

app\Listeners\LoginSuccessful.php

<?php

namespace App\Listeners;

use Illuminate\Auth\Events\Login;
use Session;

class LoginSuccessful
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param Login $event
     * @return void
     */
    public function handle(Login $event)
    {
        Session::flash('login-success', 'Hello ' . $event->user->name . ', welcome back!');
    }
}

Step 2: Register Listener in Provider

Now open app/Providers/EventServiceProvider.php file and add Login default event and listener to $listen property:

    protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],
        'Illuminate\Auth\Events\Login' => [
            'App\Listeners\LoginSuccessful'
        ],
    ];

Step 3: Display Success Message

Here we display the message using the blade template. In my case after login the user we redirect them in home.blade.php file, You can add the below success message where your user are redirected. Now open your resources/views/home.blade.php file and put the success message on it just like we have added.

@if(session('login-success'))
    <div class="alert alert-success" role="alert">
        {{ session('login-success') }}
    </div>
@endif

resources\views\home.blade.php

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">Dashboard</div>

                <div class="card-body">
                    @if(session('login-success'))
                        <div class="alert alert-success" role="alert">
                            {{ session('login-success') }}
                        </div>
                    @endif
                    @if (session('status'))
                        <div class="alert alert-success" role="alert">
                            {{ session('status') }}
                        </div>
                    @endif

                    You are logged in!
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

Now, you can check when a user is logged in then the success message will be show. I hope you enjoy Laravel login event listener, for user logged-in and successully login message showing in blade file.

Leave a Comment