2

I'm building a mobile app and when a CSRF token isn't present I want to return an error in JSON instead of it returning the "TokenMismatchException" Html page.

Is there anyway to do this easily without adjusting the library code?

user1157885
  • 1,999
  • 3
  • 23
  • 38

1 Answers1

3

You can create your own custom responses and make one for TokenMismatchException

So you do that in the Exceptions/Handler.php file. Something like;

public function render($request, Exception $e)
{
    if($e instanceof TokenMismatchException)
    {
        return json(......
    }

I think you might also need to include in the use statement;

use Illuminate\Session\TokenMismatchException as TokenMismatchException;

mikelovelyuk
  • 4,042
  • 9
  • 49
  • 95