Hi,
Fairly new to middleware concept and start to feel confortable with delegation MiddlewareInterface::process()
, but what’s the recommended way to handle error responses (unauthorized, redirect response, content-types…) ?
For example:
class JwtAuthMiddleware implements ServerMiddlewareInterface
{
public function process(ServerRequestInterface $request, DelegateInterface $delegate): ResponseInterface
{
$auth = $this->authenticate($jwtToken=?, $login=?, $password=?);
if ($auth->isValid()) {
$response = $delegate->process($request->withAttribute('xxxx', $auth));
$response->writeTokenInSecureToken();
return $response;
}
// Not authenticated - my question is about this:
switch($type) {
case 'json':
return new JsonResponse(['message' => 'unauthorized'], 401);
break;
// case 'xml': ...
default:
return new RedirectResponse('/login');
}
}
Is there a recommended approach for doing this ? (ErrorResponse, UnauthorizedResponse factory ?)
PS: an early WIP example code: https://github.com/belgattitude/soluble-wallit/blob/99b91f50694b5f165ea7c60b0bf451991a20f399/src/Soluble/Wallit/JwtAuthMiddleware.php#L79