Skip to content

Instantly share code, notes, and snippets.

@df-jablan
Last active January 26, 2016 21:50
Show Gist options
  • Save df-jablan/fe7799e654c595faa51c to your computer and use it in GitHub Desktop.
Save df-jablan/fe7799e654c595faa51c to your computer and use it in GitHub Desktop.
Laravel 5.1 handle Token Mismatch error from expired an session
// app/Http/Middleware/Authenticate.php
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @return mixed
*/
public function handle($request, Closure $next)
{
if ($this->auth->guest()) {
return $request->ajax() ? response()->json('Unauthorized.', 401) : \Redirect::guest('auth/login');
}
return $next($request);
}
// app/Http/Middleware/VerifyCsrfToken.php
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @return mixed
*/
public function handle($request, \Closure $next)
{
try {
return parent::handle($request, $next);
} catch (TokenMismatchException $_ex) {
// Catch expired sessions
return \Redirect::guest('auth/login')->withErrors([
'Session Expired' => \Lang::get('session-expired',
'Your session has expired or is otherwise not valid.'),
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment