Telescope Not Logging After JWT Authorization and Gate/Policy Implementation
I recently implemented JWT authentication in my Laravel application using the tymon/jwt-auth package, alongside Gates and Policies for role-based access control. I also use a modular approach with Nwidart.
Here's the issue: Telescope no longer logs requests for APIs that require authorization. However, 404 errors are still being logged. Strangely, Telescope works fine for APIs that don't require authorization, and the main APIs (without authorization) log as expected.
I'm trying to understand why Telescope doesn't log requests after authorization is applied. Could it be related to how authorization is processed, or is something missing in my setup?
Any insights or advice would be greatly appreciated!
Some info may help:
- env is local
- I'm using multi guard
- laravel/framework:
^7.30.6 - telescope:
3.5.1
class TelescopeServiceProvider extends TelescopeApplicationServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
Telescope::night();
$this->hideSensitiveRequestDetails();
Telescope::filter(function (IncomingEntry $entry) {
if ($this->app->environment('local')) {
return true;
}
return $entry->isReportableException() ||
$entry->isFailedRequest() ||
$entry->isFailedJob() ||
$entry->isScheduledTask() ||
$entry->hasMonitoredTag();
});
}
/**
* Prevent sensitive request details from being logged by Telescope.
*
* @return void
*/
protected function hideSensitiveRequestDetails()
{
if ($this->app->environment('local')) {
return;
}
Telescope::hideRequestParameters(['_token']);
Telescope::hideRequestHeaders([
'cookie',
'x-csrf-token',
'x-xsrf-token',
]);
}
/**
* Register the Telescope gate.
*
* This gate determines who can access Telescope in non-local environments.
*
* @return void
*/
protected function gate()
{
Gate::define('viewTelescope', function ($user) {
return in_array($user->email, [
//
]);
});
}
}
class Jwt
{
/**
* Handle an incoming request.
* I used this middleware to thrown exceptions
*
* @param Request $request
* @param Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
try {
$guardsToCheck = [
'customer-api',
'supplier-api',
'employee-api',
];
foreach ($guardsToCheck as $guard) {
if (auth()->guard($guard)->check()) {
$user = auth()->guard($guard)->user();
break;
}
}
if (!$user) {
return api_response(false, 'authenticate', 'User not found', [], 404);
}
if ($user && $user->locked_at) {
return api_response(false, 'authenticate', 'User is locked.', [], 403);
}
} catch (TokenExpiredException $exception) {
return api_response(false, 'authenticate', $exception->getMessage(), [], 422);
} catch (TokenInvalidException $exception) {
return api_response(false, 'authenticate', $exception->getMessage(), 422);
} catch (JWTException $exception) {
return api_response(false, 'authenticate', 'Missing token!', [], 422);
} catch (Throwable $th) {
return api_response(false, 'authenticate', 'unAuthenticate', [], 401);
}//end try
return $next($request);
}//end handle()
}//end class
Please or to participate in this conversation.