1 頁 (共 1 頁)

Laravel: Only allowing one session per user at a time

發表於 : 2017-01-14 23:50:08
yehlu
http://stackoverflow.com/questions/2793 ... -at-a-time


1
down vote
For laravel 5.2 after executing the make:auth command you can create the method:public function authenticated(Request $request,User $user) in the AuthController.php and in that method you can do what you want after login. For a single session per user we add to the AuthController at top:

代碼: 選擇全部

use Illuminate\Http\Request;
use Auth;
And add the function:

代碼: 選擇全部

public function authenticated(Request $request,User $user){
    $previous_session = $user->session_id;

    if ($previous_session) {
        \Session::getHandler()->destroy($previous_session);
    }

    Auth::user()->session_id = \Session::getId();
    Auth::user()->save();
    return redirect()->intended($this->redirectPath());
}
Remember to add the migration for altering your users table with the session_id column.