Reputation: 379
I need to redirect the user to a simple informative view when throttling is detected during login.
I have a view called suspended.blade.php
I have set a route
Route::get('/suspended', function(){
return view('suspended');
});
I'm using Cartalyst/Sentinel.
In my login controller I have something like this:
function LoginUser(Request $request){
// some validation stuff...
try {
$user = Sentinel::authenticate($request->all());
} catch (ThrottlingException $e) {
// user inserted too many times a wrong password
return redirect('/suspended');
} catch (NotActivatedgException $e) {
return redirect()->back()->with( ['error' => "Account not active yet."] );
}
// some other stuff...
}
If I emulate trottling I only get an error page, instead of my view.
Why is that?
Thanks
EDIT Following the hints of @PsyLogic I modified my function like that:
function LoginUser(Request $request){
// some validation stuff...
try {
$user = Sentinel::authenticate($request->all());
}
/* remove this part to use the default behaviour described in app\Excpetions\Handler.php */
// catch (ThrottlingException $e) {
// return redirect('/suspended');
// }
catch (NotActivatedgException $e) {
return redirect()->back()->with( ['error' => "Account not active yet."]
);
}
// some other stuff...
}
Still does not work, and shows the Laravel Error Page with all the debug code.
Upvotes: 0
Views: 670
Reputation: 1193
Laravel already has throttle middleware you can just extend it and update the handle()
method
namespace App\Http\Middleware;
use Illuminate\Routing\Middleware\ThrottleRequests;
class CustomThrottleMiddleware extends ThrottleRequests
{
//...
}
and update the new middleware in your Handle.php file
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
// ...
'throttle' =>App\Http\Middleware\CustomThrottleMiddleware::class,
]
or you may keep the original index throttle and add your s
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
// ...
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'custom_throttle' =>App\Http\Middleware\CustomThrottleMiddleware::class,
]
Updated (easy way)
Event those changes won't affect your package, but let's do it with easy way, you can update the render()
function inside App\Exceptions\Handler::class
and make a test
public function render($request, Throwable $exception)
{
if ($exception instanceof ThrottleRequestsException) {
return redirect()->route('suspended');
}
return parent::render($request, $exception);
}
Upvotes: 0