Single Session User Login in Laravel

  • 20-09-2022
  • 1691
  • Laravel 9
  • Haresh Chauhan

Watch Youtube Video (HINDI) :

Hello Developer!, in this tutorial we do Single Session User Login in the Laravel application using the default authentication system in the laravel application.

Laravel provides a single session authentication system for the default login. as you already know that single session means the user can only log in a single time at the same time. If the user already login with other devices then they will automatically log out once they login into another device.

To active single session login in your laravel application we just need to modify with the default laravel auth login system. as well as need to active a service provider in the kernel.php file in the middleware section.

Create a new fresh laravel project using below provided command in your terminal.

composer create-project laravel/laravel example-app

Install composer for the laravel UI using the below-provided command.

composer require laravel/ui

Create a default authentication system using the below-suggested command in your laravel application.

php artisan ui bootstrap --auth

We will install npm in your laravel application for the provided basic dependency in the application. This will create a node module folder in your laravel application.

npm install

Add a authenticated() method in the login controller. The controller is located in the auth folder inside the controller's root folder. or you can copy the controller and paste it into your application.

App\Http\Controllers\Auth\LoginController.php
<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller
{
     /*
     |--------------------------------------------------------------------------
     | Login Controller
     |--------------------------------------------------------------------------
     |
     | This controller handles authenticating users for the application and
     | redirecting them to your home screen. The controller uses a trait
     | to conveniently provide its functionality to your applications.
     |
     */

     use AuthenticatesUsers;

     /**
     * Where to redirect users after login.
     *
     * @var string
     */
     protected $redirectTo = RouteServiceProvider::HOME;

     /**
     * Create a new controller instance.
     *
     * @return void
     */
     public function __construct()
     {
          $this->middleware('guest')->except('logout');
     }

     protected function authenticated()
     {
          \Auth::logoutOtherDevices(request('password'));
     }
}

We will uncomment a line inside the kernel.php file. You for the authentication session middleware for the single session accept only. So just open the file and add a line.

App\Http\Kernel.php
protected $middlewareGroups = [
     'web' => [
                    // ADD OR UNCOMMENT 
          \Illuminate\Session\Middleware\AuthenticateSession::class,
     ],
];

Now all the setup is ready, just need to build an application using the below suggested command in the project terminal.

npm run dev

Start your development server using the given command.

php artisan serve

Open your browser and use This URL and register use and check also with a different browser with the same user login and refresh your browser and check single session login.

http://127.0.0.1/login

We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :