здесь у меня есть мой почтовый логин, чтобы войти в систему, а затем вернуть JWT, который содержит время инициализации, время истечения и идентификатор.
$app->post('/login', function ($request, $response) {
$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);
}
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
$payload = [
"iat" => time(),
"exp" => time() + 36000,
"id" => $input['id']
];
$token = JWT::encode($payload, $settings['jwt']['secret'],"HS256");
$_SESSION['user_id'] = $input['id'];
return $this->response->withJson($input, 200)
->withHeader('Content-type', 'application/json;charset=utf-8', 200)
->withAddedHeader('Authorization', $token);
});
$app->put('/address/{id}', function ($request, $response, $args) {
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
if(!empty($_SESSION['user_id'])) {
$input = $request->getParsedBody();
$sql = "UPDATE address SET s_pNumber =:s_pNumber, s_address =:s_address, s_address2 =:s_address2 , s_pCode =:s_pCode, s_city =:s_city,
s_state =:s_state, s_country =:s_country WHERE id =:id";
$sth = $this->db->prepare($sql);
$sth->bindParam("id", $args['id']);
$sth->bindParam("s_pNumber", $input['s_pNumber']);
$sth->bindParam("s_address", $input['s_address']);
$sth->bindParam("s_address2", $input['s_address2']);
$sth->bindParam("s_pCode", $input['s_pCode']);
$sth->bindParam("s_city", $input['s_city']);
$sth->bindParam("s_state", $input['s_state']);
$sth->bindParam("s_country", $input['s_country']);
$input['id'] = $args['id'];
$sth->execute();
session_destroy();
return $this->response->withJson($input, 200);
}
// if(empty($_SESSION['user_id'])) {
// return $response->withJson(['error' => true, 'message' => 'Login please'], 403);
// }
});
Мой вопрос: как я могу редактировать данные пользователя на основе идентификатора в полезной нагрузке, внутри которого пользователь уже вошел в систему? Как я могу декодировать полезную нагрузку и сделать мой mysql похожим на что-то вроде "WHERE (payload.id)" not "WHERE id =: id 'Спасибо