lampuotomatis/app/Exceptions/Handler.php

66 lines
1.7 KiB
PHP

<?php
namespace App\Exceptions;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that are not reported.
*
* @var array<int, class-string<Throwable>>
*/
protected $dontReport = [
//
];
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array<int, string>
*/
protected $dontFlash = [
'current_password',
'password',
'password_confirmation',
];
/**
* Register the exception handling callbacks for the application.
*
* @return void
*/
protected function unauthenticated($request, AuthenticationException $exception)
{
// Selalu kembalikan JSON untuk semua request API/mobile
if ($request->expectsJson() || $request->is('api/*') || $request->is('mobile/*')) {
return response()->json([
'errors' => 'Token tidak valid atau telah kedaluwarsa',
], 401);
}
// Untuk request web, redirect ke login
return redirect()->guest(route('login'));
}
public function register()
{
$this->reportable(function (Throwable $e) {
//
});
// Tambahkan handler untuk respons JSON universal
$this->renderable(function (AuthenticationException $e, $request) {
if ($request->expectsJson() || $request->is('api/*') || $request->is('mobile/*')) {
return response()->json([
'errors' => 'Token tidak valid atau telah kedaluwarsa',
], 401);
}
});
}
}