Hi i recently inherited a CakePHP codebase which was in the process of an upgrade from CakePHP 3 to CakePHP 4.
On this project we use a reactjs frontend and we use CakePHP as a backend for API requests.
My problem right now is that when I use the CakePHP Authentication plugin I don’t know how to respond with a 401 HTTP error or a custom json response when a request is unauthenticated.
I can make a redirect to the login page, (as described here Quick Start - 2.x) by configuring the AuthenticationService
with a unauthenticatedRedirect
parameter. If I don’t set the unauthenticatedRedirect
parameter CakePHP responds with a Internal server error (500) and an UnauthenticatedException
is thrown: [Authentication\Authenticator\UnauthenticatedException] Authentication is required to continue in <filename and line number>
Is it possible having CakePHP return a custom json response if the request is unauthenticated? Or if that is not possible, just return a 401 Error?
Edit: I’m using CakePHP 4.2.5 and authentication 2.6.0
Thank you!