예제 #1
0
파일: api.php 프로젝트: vincurekf/TAJP
require 'autoload.php';
/*
 * Configure Slim app
 * 1. Create Slim app
 * 2. Add JWT middleware for json web token authentication
 * 3. Allow CORS request so itl will accept calls from other domains
 */
// 1
$app = new \Slim\Slim();
// 2
$app->add(new \Slim\Middleware\JwtAuthentication(["secret" => base64_decode(JWT_SECRET), "path" => ["/api"], "callback" => function ($options) use($app) {
    $app->oldJwt = $options["decoded"];
    $decodedData = (array) $app->oldJwt->data;
    $user_id = $decodedData['user_id'];
    $user_name = $decodedData['user_name'];
    $app->newToken = Authenticate::reissueToken($user_id, $user_name);
    // $options["decoded"];
}]));
// 3
$app->add(new \CorsSlim\CorsSlim(array("origin" => "*", "allowCredentials" => True)));
/*
 * API ROUTES
 */
// get all users
$app->get('/api/users', function () use($app, $database) {
    $resultArr['token'] = $app->newToken;
    $resultArr['result'] = UsersAPI::get($database);
    echo json_encode($resultArr);
});
// add new user
$app->post('/api/users', function () use($app, $database) {