chess/app/Http/Middleware/SessionExpired.php

43 lines
1.2 KiB
PHP
Raw Permalink Normal View History

2024-08-29 03:56:32 +00:00
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Session\Store;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
use Session;
class SessionExpired
{
protected $session;
protected $timeout = 1200;
public function __construct(Store $session){
$this->session = $session;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$isLoggedIn = $request->path() != 'login';
if(! session('lastActivityTime'))
$this->session->put('lastActivityTime', time());
elseif(time() - $this->session->get('lastActivityTime') > $this->timeout){
$this->session->forget('lastActivityTime');
$cookie = cookie('intend', $isLoggedIn ? url()->current() : 'home');
// auth()->logout();
Auth::logout();
return Redirect::route('login');
}
$isLoggedIn ? $this->session->put('lastActivityTime', time()) : $this->session->forget('lastActivityTime');
return $next($request);
}
}