Kaydet (Commit) 4409b932 authored tarafından Baran Sekin's avatar Baran Sekin

Added SessionTimeout Middleware

üst 8714668d
......@@ -16,7 +16,7 @@ DB_PATH=/liman/database/liman.sqlite
BROADCAST_DRIVER=log
CACHE_DRIVER=array
SESSION_DRIVER=file
SESSION_LIFETIME=120
SESSION_LIFETIME=1
QUEUE_DRIVER=database
......
......@@ -13,6 +13,7 @@ class Kernel extends HttpKernel
Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
// \App\Http\Middleware\TrustProxies::class,
\App\Http\Middleware\SessionTimeout::class,
];
protected $middlewareGroups = [
......
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Session\Store;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;
class SessionTimeout {
/**
* Instance of Session Store
* @var session
*/
protected $session;
/**
* Time for user to remain active, set to 900secs( 15minutes )
* @var timeout
*/
protected $timeout = 900;
public function __construct(Store $session){
$this->session = $session;
$this->redirectUrl = 'auth/login';
$this->sessionLabel = 'warning';
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if(! $this->session->has('lastActivityTime'))
{
$this->session->put('lastActivityTime', time());
}
else if( time() - $this->session->get('lastActivityTime') > $this->getTimeOut())
{
$this->session->forget('lastActivityTime');
Auth::logout();
return redirect($this->getRedirectUrl())->with([ $this->getSessionLabel() => 'You have been inactive for '. $this->timeout/60 .' minutes ago.']);
}
$this->session->put('lastActivityTime',time());
return $next($request);
}
/**
* Get timeout from laravel default's session lifetime, if it's not set/empty, set timeout to 15 minutes
* @return int
*/
private function getTimeOut()
{
return ($this->lifetime) ?: $this->timeout;
}
/**
* Get redirect url from env file
* @return string
*/
private function getRedirectUrl()
{
return (env('SESSION_TIMEOUT_REDIRECTURL')) ?: $this->redirectUrl;
}
/**
* Get Session label from env file
* @return string
*/
private function getSessionLabel()
{
return (env('SESSION_LABEL')) ?: $this->sessionLabel;
}
}
\ No newline at end of file
......@@ -33,7 +33,7 @@ return [
'lifetime' => env('SESSION_LIFETIME', 120),
'expire_on_close' => false,
'expire_on_close' => true,
/*
|--------------------------------------------------------------------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment