Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a custom message with the 419 CSRF error & Remove $dontReport as … #65

Merged
merged 1 commit into from
Apr 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,12 @@

use Illuminate\Auth\AuthenticationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Session\TokenMismatchException;
use Illuminate\Support\Facades\Auth;
use Throwable;

class Handler extends ExceptionHandler
{
/**
* A list of the exception types that should not be reported.
*
* @var array
*/
protected $dontReport = [
\Illuminate\Auth\AuthenticationException::class,
\Illuminate\Auth\Access\AuthorizationException::class,
\Symfony\Component\HttpKernel\Exception\HttpException::class,
\Illuminate\Database\Eloquent\ModelNotFoundException::class,
\Illuminate\Session\TokenMismatchException::class,
\Illuminate\Validation\ValidationException::class
];

/**
* A list of the inputs that are never flashed for validation exceptions.
Expand All @@ -46,6 +34,25 @@ public function report(Throwable $exception)
parent::report($exception);
}

/**
* Register the exception handling callbacks for the application.
*
* @return void
*/
public function register()
{
$this->reportable(function (Throwable $e) {
//
});

// Manage 419 csrf token expiration error
$this->renderable(function (\Exception $e) {
if ($e->getPrevious() instanceof TokenMismatchException) {
return back()->with('danger', 'You made too much time to validate the form ! Time to take a coffee !');
};
});
}

/**
* Render an exception into an HTTP response.
*
Expand Down Expand Up @@ -73,7 +80,7 @@ public function render($request, Throwable $exception)
}

/**
* Convert an authentication exception into an unauthenticated response.
* Convert an authentication exception into a response.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Auth\AuthenticationException $exception
Expand All @@ -82,8 +89,8 @@ public function render($request, Throwable $exception)
*/
protected function unauthenticated($request, AuthenticationException $exception)
{
return $request->expectsJson()
? response()->json(['error' => 'Unauthenticated.'], 401)
return $this->shouldReturnJson($request, $exception)
? response()->json(['message' => $exception->getMessage()], 401)
: redirect()
->guest($exception->redirectTo() ?? route('users.auth.login'))
->with('danger', 'You don\'t have the permission to view this page.');
Expand Down