ricardoper/slim3-skeleton

Middleware: JWT Auth

flowdee opened this issue · 2 comments

Trying to implement the Slim JWT Auth as middleware but really struggling getting this work with your skeleton.

Creating a route for generating a token is fine. The only problem is adding the middleware correctly in order so validate route requests.

All Slim JWT auth tutorials and documentations are using a different approach of setting up middlewares. That’s why I have no idea how to solve this with your skeleton, especially the part inside the Authentication Middleware class: https://gist.github.com/flowdee/799203ac858e060826892541bc0110f5

Any idea how to get this working?

Hello Flowdee,

Sorry for the delay...

You need to call the JWT Class directly from the middlewares.php file.

Example:

--- slim3/config/middlewares.php ---
<?php

return [
    \App\Middlewares\CorsMiddleware::class,

    (new Tuupola\Middleware\JwtAuthentication([
        "secure" => false,
        "path" => "/api",
        "secret" => "supersecretkeyyoushouldnotcommittogithub",
    ])),
];

In this example, all the routes beginning with /api needs JWT Auth.

Please, try it and give some feedback.

Best regards,
Ricardo Pereira.

Works like charm!

Wasn't aware that I can add those third party middlewares like described. Thought it all has to take action inside separate class files, but of course it makes sense.

Thanks @ricardoper !