Answers for "laravel return json when no authorized"

PHP
1

laravel response json if not authorized

>>> add this function in app/Exceptions/Handler.php

use Illuminate\Auth\AuthenticationException;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

/**
 * Convert an authentication exception into an unauthenticated response.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Illuminate\Auth\AuthenticationException  $exception
 * @return \Illuminate\Http\Response
 */
protected function unauthenticated($request, AuthenticationException $exception)
{
    return response()->json(['error' => 'Unauthenticated!'], 401);
}
Posted by: Guest on May-25-2021

Browse Popular Code Answers by Language