Hello you all,
I would like to integrate jwt into my slim project but I have no idea about the beginning, can someone help me?
Hello you all,
I would like to integrate jwt into my slim project but I have no idea about the beginning, can someone help me?
What is your client ? PHP or JS?
I use JS.
It’s depends of the client ?
To make myself more clear. Just was wondering if your client application is js (react, vue, etc.) and you try to fetch some data from php endpoint or maybe your client application is another (also) php. Simply saying, which one sends a request to the endpoint, some js client or some php client ?
I’m asking because as far as I can see, last time there was a mobile engaged in the process.
Okay.
My client application is vuejs, and I would like to create web services including the token processes.
For example, I would like create authentication web service. How can I integrate the token processes into my authentication endpoint ?
This should give you a solid start point for an api endpoint token integration within Slim4:
$ cd my-api-project
$ composer require tuupola/slim-jwt-auth
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
config.php/settings.php
return [
...
'jwt_authentication' => [
'secret' => $_ENV['JWT_SECRET'],
'algorithm' => 'HS256',
'secure' => false, // only for localhost for prod and test env set true
'error' => static function ($response, $arguments) {
$data['status'] = 401;
$data['error'] = 'Unauthorized/'. $arguments['message'];
return $response
->withHeader('Content-Type', 'application/json;charset=utf-8')
->getBody()->write(json_encode(
$data,
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT
));
}
],
...
];
dependencies.php
use Tuupola\Middleware\JwtAuthentication;
return [
...
JwtAuthentication::class => static function (Container $container): JwtAuthentication {
return new JwtAuthentication(
$container->get('jwt_authentication'),
);
},
...
];
routes.php
use Tuupola\Middleware\JwtAuthentication;
if (isset($app, $container)) {
...
$app->get('/api/example', function (Request $request, Response $response) {
return $this->get('example_controller')->getExamples($request, $response);
})->setName('api-examples')
->addMiddleware($container->get(JwtAuthentication::class));
...
}
For further information:
Okay thanks, I will try it and give a feedback