Laravel: Only allowing one session per user at a time

http://laravel.com/

http://kejyun.github.io/Laravel-4-Docum ... roduction/
回覆文章
yehlu
Site Admin
文章: 3245
註冊時間: 2004-04-15 17:20:21
來自: CodeCharge Support Engineer

Laravel: Only allowing one session per user at a time

文章 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.
回覆文章

回到「laravel」