Last active
February 4, 2016 05:55
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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