title: Slim v4 tuupola / slim-jwt-auth send custom token to middleware
body: <p>Here is my problem. I'm trying to set up a Slim 4 framework with slim-jwt-auth. This works like a charm with one small problem that I cannot seem to figure out. I want to be able to bypass the slim-jwt-auth middleware in certain circumstances, eg when the client has a certain IP or the server host IP is localhost.</p>
  <p>I created the OptionalAuth middleware that tests the conditions and generates a token on the fly if the conditions are met. But I cannot seem to get this token to the slim-jwt-auth middleware or somehow skip the slim-jwt-auth middleware from the OptionalAuth middleware.</p>
  <p><strong>The code I have is:</strong></p>
  <pre><code>// middleware.php
  $app-&gt;add(JwtAuthentication::class);
  $app-&gt;add(OptionalAuth::class);
  
  // container.php
      JwtAuthentication::class =&gt; function(ContainerInterface $container) {
          $settings = $container-&gt;get('settings')['jwt'];
          $settings['logger'] = $container-&gt;get(LoggerFactory::class)-&gt;createInstance('jwt');
          return new JwtAuthentication($settings);
      },
  
      OptionalAuth::class =&gt; function(ContainerInterface $container) {
          return new OptionalAuth($container);
      },
  
  // settings.php
  $settings['jwt'] = [
      &quot;path&quot; =&gt; [&quot;/api&quot;],
      &quot;ignore&quot; =&gt; [&quot;/api/token&quot;],
      &quot;secure&quot; =&gt; false,
      &quot;header&quot; =&gt; &quot;token&quot;,
      &quot;regexp&quot; =&gt; &quot;/(.*)/&quot;,
      &quot;secret&quot; =&gt; &quot;notpostedtostackoverflow&quot;,
      &quot;algorithm&quot; =&gt; [&quot;HS512&quot;],
      'validHours' =&gt; 1,
      &quot;attribute&quot; =&gt; &quot;jwt&quot; 
  ];
  
  // OptionalAuth.php
  &lt;?php
  
  // use and namespace removed for readability
  
  class OptionalAuth
  {
      private $container;
      protected $auth;
      public function __construct(ContainerInterface $container, Auth $auth) {
          $this-&gt;container = $container;
          $this-&gt;auth = $auth;
      }
      public function __invoke(Request $request, RequestHandler $handler) : Response {
          $condition = true;
          if ($condition) {
              // full token snipped for readability, 
              //actual token will be generated here with the Auth class.
              $token = 'eyEXA';
          }
          return $handler-&gt;handle($request);
      }
  }
  </code></pre>
  <p>For me the solution will be one of either things:</p>
  <ul>
  <li>Temporary disable the next (jwt) middleware if $condition === true</li>
  <li>Change the headers on the $request to add the generated $token</li>
  <li>Somehow pass a token to JwtAuthentication($settings);</li>
  </ul>
  <p>If someone can push me in the right direction here, that would be greatly appreciated!</p>
  <p>Ties.</p>