Skip to content

Instantly share code, notes, and snippets.

@marttosc
Created May 25, 2016 17:36
Show Gist options
  • Save marttosc/48fafe057f7ea6697e9ba756a67febbf to your computer and use it in GitHub Desktop.
Save marttosc/48fafe057f7ea6697e9ba756a67febbf to your computer and use it in GitHub Desktop.
Using middleware to change response
<?php
namespace App\Http\Middleware;
use Closure;
class PasswordTemporary
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$user = app('auth')->user();
$passwords = app('db')->table('temporary_passwords')->orderBy('default', 'desc')->get();
$isTemporary = false;
foreach ($passwords as $password) {
if (app('hash')->check($password->password, $user->password)) {
$isTemporary = true;
break;
}
}
if (!$isTemporary && $request->route()->getName() === 'pass.temp') {
return redirect()->home();
}
if ($isTemporary && $request->route()->getName() !== 'pass.temp') {
return redirect()->route('pass.temp');
}
return $next($request);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment