I am using https://github.com/RobDWaller/psr-jwt library in my slim4 application
I want to validate the token before the API call. if the token is valid, API should call else an error should prompt. Below is my code
//routes.php
$app->group('/myapp/login', function (Group $group) {
$group->post('', LoginProcess::class);
});
$app->group('/myapp/register', function (Group $group) {
$group->post('', RegisterProcess::class);
});
$app->group('/,myapp', function (Group $group) {
$group->get('/list', ListUser::class);
});
//middlware.php
<?php
use App\Application\Middleware\AuthMiddleware;
use Slim\App;
return function (App $app) {
$app->add(\PsrJwt\Factory\JwtMiddleware::html('Secret123!456$', 'jwt', 'Authorisation Failed'));
};
Is it validate the token itself. are any other methods exist? my question is:
How to write custom errors?
How to exclude middleware to log in, register apis before authenticated & after authenticated.
I am new to the slim framework. please help me.
To invoke a middleware on routing level you can add that middleware to a specific route and/or a routing group.
Slim 4 example:
use Slim\Routing\RouteCollectorProxy;
use PsrJwt\Factory\JwtMiddleware;
// ...
$app->group('/myapp', function (RouteCollectorProxy $group) {
$group->post('/login', LoginProcess::class);
$group->post('/register', RegisterProcess::class);
$group->get('/list', RegisterProcess::class);
})->add(JwtMiddleware::html('Secret123!456$', 'jwt', 'Authorisation Failed'));