I am new to PHP and developing restful services using jwt authorization token. I have followed this GitHub example
and have understood code to some extent but I am stuck on error on this line $stmt->bindParam("user_id", $decoded->context->user->user_id);
saying
Notice: Array to string conversion in C:\xampp\htdocs\slim2\src\routes.php on line.
please help me solving this problem, I cant understand what is context->user->user_id where these are coming from. The full code is given below
// The route to get a secured data.
$app->get('/restricted', function (Request $request, Response $response) {
$jwt = $request->getHeaders();
$key = "testsecretekey";
try {
$decoded = JWT::decode($jwt['HTTP_AUTHORIZATION'][0], $key, array('HS256'));
} catch (UnexpectedValueException $e) {
echo $e->getMessage();
}
if (isset($decoded)) {
$sql = "SELECT * FROM tokens WHERE user_id = :user_id";
try {
$db = $this->db;
$stmt = $db->prepare($sql);
$stmt->bindParam("user_id", $decoded->context->user->user_id);
$stmt->execute();
$user_from_db = $stmt->fetchObject();
$db = null;
if (isset($user_from_db->user_id)) {
echo json_encode([
"response" => "This is your secure resource !"
]);
}
} catch (PDOException $e) {
echo '{"error":{"text":' . $e->getMessage() . '}}';
}
}
});