Implement API throttling, and ensure output is JSON

This commit is contained in:
David Bomba 2019-04-19 17:59:48 +10:00
parent 72dee9bfb6
commit 5b8f56593e
2 changed files with 18 additions and 3 deletions

View File

@ -3,9 +3,10 @@
namespace App\Exceptions;
use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Database\Eloquent\ModelNotFoundException as ModelNotFoundException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Http\Exceptions\ThrottleRequestsException;
class Handler extends ExceptionHandler
{
@ -56,7 +57,11 @@ class Handler extends ExceptionHandler
if ($exception instanceof ModelNotFoundException)
{
return response()->json(['error'=>'Record not found'],400);
return response()->json(['message'=>'Record not found'],400);
}
else if($exception instanceof ThrottleRequestsException)
{
return response()->json(['message'=>'Too many requests'],429);
}
return parent::render($request, $exception);

View File

@ -67,10 +67,20 @@ class LoginController extends BaseController
{
$this->validateLogin($request);
if ($this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
return response()->json(['message' => 'Too many login attempts, you are being throttled']);
}
if ($this->attemptLogin($request))
return $this->itemResponse($this->guard()->user());
else
else {
$this->incrementLoginAttempts($request);
return response()->json(['message' => ctrans('texts.invalid_credentials')]);
}
}