xtremeCODE
xtremeCODE

Reputation: 719

Do a specific Action before Logging out in Laravel

I am building a Laravel App that involves Limiting the numbers of session/device a User can login into

I have 3 types of User

  1. Basic
  2. Family
  3. Couple

In my users table, I have a column of no_of_logins to store the number of device a user login into. I perform increment function on this column at user login.

This is the way my controller looks like

public function authenticated(Request $request,User $user)
{

    if(Auth::check())
    $previous_session = $user->session_id;
    $login = $user->no_of_logins;
    { //check if the user is logged in or not
        $user = Auth::user();

        if ($user->isCouple()) 
        {
            if ($login > 2)
            {
                Auth::logout();    
                session()->flash('logout', "You are Logged in on other devices");
                return redirect('login');
            }

            else
            {
            Auth::user()->session_id = \Session::getId();
            Auth::user()->no_of_logins++;
            Auth::user()->save();
            }

            return redirect(route('welcome'));
        }

        elseif ($user->isFamily()) 
        {
            if ($login > 4)
            {
                Auth::logout();  
                session()->flash('logout', "You are Logged in on other devices");
                return redirect('login');
            }
            else 
            {
            Auth::user()->session_id = \Session::getId();
            Auth::user()->no_of_logins++;
            Auth::user()->save();
            }
            return redirect(route('welcome'));
        }

        elseif ($user->isBasic()) 
        {
            if ($login > 1)
            {
                Auth::logout();
                session()->flash('logout', "You are Logged in on other devices");
                return redirect('login');
            }
            else
            {
            // Auth::user()->session_id = \Session::getId();
            Auth::user()->no_of_logins++;
            Auth::user()->save();
            }
            return redirect(route('welcome'));
        }

        else 
        {
            return redirect(route('welcome'));
        }
    } 
}

This code work for me. I don't know if this is the perfect way to do this though.

But, I have a blocker. When the user logs out,

I want to be able to reduce no_of_logins when user logs out

I tried to create a method in my controller by doing this

public function logout(Request $request) 
{
    if(Auth::logout())
    { 
    //  $user = Auth::user();
    $login = $user->no_of_logins;
    Auth::user()->no_of_logins--;
    Auth::user()->save();
    }
    return redirect('/login');

}

This doesn't reduce the no_of_logins column

I know this won't work. Can anyone recommend how I can go about this. I read through some blogs andd article. I was reading using Laravel Event, but I don't understand how Laravel event work

Upvotes: 1

Views: 843

Answers (1)

lagbox
lagbox

Reputation: 50561

The event fired from the SessionGuard upon logging out is Illuminate\Auth\Events\Logout. You can setup a listener to listen for this event and do the action you need. This is after they have been logged out, not before.

Create a file at app/Listeners/LoggedOutListener.php:

<?php

namespace App\Listeners

use Illuminate\Auth\Events\Logout;

class LoggedOutListener
{
    public function handle(Logout $event)
    {
        $event->user->decrement('no_of_logins');
    }
}

You can register this in your App\Providers\EventServiceProvider:

use App\Listeners\LoggedOutListener;
use Illuminate\Auth\Events\Logout;

protected $listen = [
    ...
    Logout::class => [
        LoggedOutListener::class,
    ],
];

Upvotes: 6

Related Questions