Last active
June 28, 2020 10:19
-
-
Save blogcacanid/6899299ec12c4d7e83607eeff4fdf01e to your computer and use it in GitHub Desktop.
middleware.php Rest API Dengan JSON Web Token Slim 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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