Single Session Login in Laravel

后端 未结 6 1589
囚心锁ツ
囚心锁ツ 2020-12-03 05:35

I\'m trying to implement a user policy whereby only one user can login at a time. I\'m trying to build this on top of Laravel\'s Auth driver.

I\'ve thought of using

6条回答
  •  臣服心动
    2020-12-03 05:52

    Here’s how you can accomplish this. First of all, you need to uncomment the line \Illuminate\Session\Middleware\AuthenticateSession::class from $middlewareGroups property in app/Http/Kernel.php file because this is the middleware that manages the user sessions in Laravel.

    2- add this line in your login function just after login attempt successfully and before redirection: \Auth::logoutOtherDevices(request('password'));

    use Illuminate\Support\Facades\Auth;
    Auth::logoutOtherDevices($password);
    
       if(Auth::guard('student')->attempt([],$request->rememberme))
        {
    
            Auth::guard('student')->logoutOtherDevices(request('password'));
    
            return redirect()->intended('/');  //route('homepage');
        }
    

    https://www.amitmerchant.com/logout-from-everywhere-except-current-device-laravel/

    demo https://xpredo.com

提交回复
热议问题