2016-10-23 11:41:52 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
|
|
|
|
use App;
|
|
|
|
use Cookie;
|
|
|
|
use Session;
|
|
|
|
use App\Models\User;
|
|
|
|
|
|
|
|
class CheckSessionUserValid
|
|
|
|
{
|
|
|
|
public function handle($request, \Closure $next)
|
|
|
|
{
|
|
|
|
// load session from cookie
|
|
|
|
if ($request->cookie('uid') && $request->cookie('token')) {
|
|
|
|
Session::put('uid' , $request->cookie('uid'));
|
|
|
|
Session::put('token', $request->cookie('token'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Session::has('uid')) {
|
|
|
|
$user = User::find(session('uid'));
|
|
|
|
|
|
|
|
if ($user && $user->getToken() == session('token')) {
|
|
|
|
// push user instance to repository
|
2016-10-25 21:35:09 +08:00
|
|
|
app('users')->set($user->uid, $user);
|
2016-12-28 12:53:13 +08:00
|
|
|
// bind current user to container
|
|
|
|
app()->instance('user.current', $user);
|
2016-10-23 11:41:52 +08:00
|
|
|
} else {
|
|
|
|
// remove sessions & cookies
|
|
|
|
delete_sessions();
|
|
|
|
delete_cookies();
|
2017-01-08 14:15:55 +08:00
|
|
|
|
|
|
|
return redirect('auth/login')->with('msg', trans('auth.check.token'));
|
2016-10-23 11:41:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
}
|