最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

oauth - How to properly catch Laravel 12 Passport Exceptions - Stack Overflow

programmeradmin6浏览0评论

I have a laravel 12 app that works with Oauth via Passport. All is fine but when i make bogus Auth requests with non valid tokens i get huge stacktraces. I have managed to use withExceptions in app.php but this only works for the OAuth error on the access token (9). The Error on the refresh token (8) still dumps a huge stacktrace. Where am I going wrong? Additionally returning anything but false on (9) also gives me the stacktrace.

return Application::configure(basePath: dirname(__DIR__))
...
->withExceptions(function (Exceptions $exceptions) {

    // $exceptions->dontReportDuplicates();

    // Handle Laravel Passport authentication errors
    $exceptions->report(function (OAuthServerException $e) {
        // this works
        if ($e->getCode() == 9) {
            Log::info('->withExceptions: Error within OAuthServerException (Token probably expired / not found)', []);
            // this supresses Passports own Logging and Stacktrace in the log:
            // I have not found a way to suppress that AND return a custom json response here
            return false;
        }

        // this does not work
        if ($e->getCode() == 8) {
            Log::info('->withExceptions: Error within OAuthServerException (Refresh-Token probably expired / not found)', []);
            return false;
        }


        return response()->json([
            'message' => 'Authentication error',
            'error' => $e->getMessage()
        ], 401);
    });
...

Thanks

发布评论

评论列表(0)

  1. 暂无评论