Home > Enterprise >  How to Prevent Multiple Login Of Same Credentials in Laravel 8?
How to Prevent Multiple Login Of Same Credentials in Laravel 8?

Time:06-25

I have a Laravel web app where multiple users login but I need to prevent login if the user has already logged.

I know that Laravel has a method (logoutOtherDevices) to logout the user when a second user (same credentials) is login.

Uncommented \Illuminate\Session\Middleware\AuthenticateSession::class, in app/Http/Kernel.php.

But I have doubt below mentioned code where to include?

use Illuminate\Support\Facades\Auth;

Auth::logoutOtherDevices($currentPassword);

Answer

 public function authenticated(Request $request)
    {
        Auth::logoutOtherDevices($request->password);
        if (Auth::check() && Auth::user()->role_id == 1) {
            return redirect('/admin/dashboard');
        }
        $config = DB::table('config')->get();
        /*starts*/
        $id = Session::get('selected_plan_id');

        if(empty($id))
        {
            return redirect('/user/dashboard');
        }
        else
        {
            $selected_plan = Plan::where('plan_id', $id)->where('status', 1)->first();
    
            if ($selected_plan == null) {
                alert()->error('Your current plan is not available. Choose another plan.');
                return redirect('/user/plans');
            } else {

                if ($selected_plan == null) {
                    return view('errors.404');
                } else {

                    return redirect('/user/rediretoCheckout');
                }
            }
        }   

    }

you need to include Auth::logoutOtherDevices($request->password); win authencication code

CodePudding user response:

You may use the logoutOtherDevices method provided by the Auth facade. This method requires the user to confirm their current password, which your application should accept through an input form: Add in Controller-> Login Mehthod

use Illuminate\Support\Facades\Auth;

Auth::logoutOtherDevices($currentPassword);

Here you will get more details

  • Related