ricardoper / slim3-skeleton

Slim Framework v3.5 Skeleton With Twig Views, Flash Messages, Logger, VarDumper, Environment Variables And A Folder Structure With Sense
MIT License
45 stars 7 forks source link

Middleware: JWT Auth #1

Closed flowdee closed 6 years ago

flowdee commented 6 years ago

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?

ricardoper commented 6 years ago

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.

flowdee commented 6 years ago

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 !