заголовок не отправлено глобально - PullRequest
0 голосов
/ 26 марта 2019
$app->group('/api', function(\Slim\App $app) {

  $app->post('/login', function (Request $request, Response $response, array $args) {

    $key = "supersecretkeyyoushouldnotcommittogithub";

    $input = $request->getParsedBody();
    $settings = $this->get('settings'); // get settings array.
    $sql = "SELECT id, password FROM users WHERE id= :id";
    $sth = $this->db->prepare($sql);
    $sth->bindParam("id", $input['id']);
    $sth->execute();
    $user = $sth->fetchObject();

    // verify user id 
    if(!$user) {
        return $this->response->withJson(['error' => true, 'message' => 'NO ID '], 404)->withHeader('Content-type', 'application/json;charset=utf-8', 404);
    }
    // Compare the input password and the password from database for a validation
    if (strcmp($input['password'],$user->password)) {
        return $this->response->withJson(['error' => true, 'message' => 'These credentials do not match our records.'], 404)->withHeader('Content-type', 'application/json;charset=utf-8', 404);  
    }

    $payload = array(
        "iat" => time(),
        "exp" => time() + 36000,
        // "id" => $input['id']
        "context" => [
            "user" => [
                "id" => $input['id']
            ]
        ]
    );

    try {
        $token = JWT::encode($payload, $settings['jwt']['secret'], "HS256"); // $token store the token of the user
    } catch (\Exception $e) {
        echo json_encode($e);
    }

    $decoded = JWT::decode($token, $key, array('HS256'));


    return $this->response->withJson($decoded)
                          ->withHeader('Content-type', 'application/json', 200)
                          ->withHeader('Authorization', $token);


    // return $this->response->withJson(['token' => $token]);

     print_r($decoded);


});

$app->get('/get', function (Request $request, Response $response, array $args) {

    if ($request->getHeader('Authorization')) {
        print_r ($request);
    } else  {
        print_r ('FAILED');
    }

});

});

Здесь у меня есть мой код, в / api / login он может декодировать jwt, но когда он находится внутри / api / get, он не обнаруживает мой заголовок авторизации и возвращает инструкцию FAILED / else, что означает, что я не могу декодироватьJWT при входе пользователя.Как я могу исправить заголовок авторизации для отправки в другую функцию?спасибо

...