Skip to content

Instantly share code, notes, and snippets.

@asaelx
Last active February 4, 2016 05:55
Show Gist options
  • Save asaelx/212c2165b7eec473216c to your computer and use it in GitHub Desktop.
Save asaelx/212c2165b7eec473216c to your computer and use it in GitHub Desktop.
This Middleware made it super easy to satisfy a requirement to log the user out after 15 minutes of in activity.
<?php namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
use Illuminate\Session\Store;
class SessionTimeout {
protected $session;
protected $timeout=900;
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)
{
if(!$this->session->has('lastActivityTime'))
$this->session->put('lastActivityTime',time());
elseif(time() - $this->session->get('lastActivityTime') > $this->getTimeOut()){
$this->session->forget('lastActivityTime');
Auth::logout();
return redirect('auth/login')->withErrors(['You had not activity in 15 minutes']);
}
$this->session->put('lastActivityTime',time());
return $next($request);
}
protected function getTimeOut()
{
return (env('TIMEOUT')) ?: $this->timeout;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment