tuupola / slim-jwt-auth

PSR-7 and PSR-15 JWT Authentication Middleware
https://appelsiini.net/projects/slim-jwt-auth
MIT License
828 stars 144 forks source link

error callback lambda gets incorrect FQCN for Response #197

Closed benmajor closed 4 years ago

benmajor commented 4 years ago

I have been using the JWT middleware for several years, and it's helped me quickly develop some great APIs, so thanks for the hard work!

I've just started a new project which uses Slim 3, but also uses Mailgun's PHP SDK which requires a PSR-7 implementation (see here: https://github.com/mailgun/mailgun-php). However, when I install the nyholm\psr7 bundle, the error callback lambda incorrectly receives an object of type Nyholm\Psr7\Response instead of \Psr\Http\Message\ResponseInterface. For example:

# Set up JWT middleare:
$this->kernel->add(new JwtAuthentication([
  'rules' => $ignoreRules,
  'secret' => $_ENV['JWT_SECRET'],
  'error' => function($response, $args) {
    return $response->withJson([
      'message' => $args['message'],
      'timestamp' => date('c')
    ], 401);
  }
]));

This throws an exception as follows:

Call to undefined method Nyholm\Psr7\Response::withJson()

Everything else inside of Slim works as expected, but the use of the Nyholm PSR-7 seems to break the middleware. Any suggestions on how we can get the two working?

benmajor commented 4 years ago

Never mind, reordering the route definition seems to have done the trick.