Skip to content

Instantly share code, notes, and snippets.

@blogcacanid
Last active June 28, 2020 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blogcacanid/6899299ec12c4d7e83607eeff4fdf01e to your computer and use it in GitHub Desktop.
Save blogcacanid/6899299ec12c4d7e83607eeff4fdf01e to your computer and use it in GitHub Desktop.
middleware.php Rest API Dengan JSON Web Token Slim 3
<?php
use Slim\App;
return function (App $app) {
// e.g: $app->add(new \Slim\Csrf\Guard);
// A middleware for enabling CORS
$app->add(function ($req, $res, $next) {
$response = $next($req, $res);
return $response
->withHeader('Access-Control-Allow-Origin', '*')
->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization')
->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
});
$app->add(new \Tuupola\Middleware\JwtAuthentication([
"path" => "/api", /* or ["/api", "/admin"] */
"ignore" => ["/api/login", "/api/register"], /* or ["/api/token", "/admin/ping"],*/
"attribute" => "decoded_token_data",
"secret" => "supersecretkeyyoushouldnotcommittogithub",
"algorithm" => ["HS256"],
"error" => function ($response, $arguments) {
$data["status"] = "error";
$data["message"] = $arguments["message"];
return $response
->withHeader("Content-Type", "application/json")
->write(json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
}
]));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment