PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, September 4, 2022

[FIXED] How to Prevent Multiple Login Of Same Credentials in Laravel 8?

 September 04, 2022     authentication, laravel-8, logout, session     No comments   

Issue

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


Solution

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



Answered By - MShaba007
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing