JWT decode() must be of the type array errorDeleting an element from an array in PHPHow do I get PHP errors to display?Best practices for server-side handling of JWT tokensWhat is secret key for JWT based authentication and how to generate it?Should I decode the JWT on the client side?angular2 with Slim framework jwt authenticationJWT on .NET Core 2.0Pass decoded JWT payload to micro servicesExplainations on JWT Tokens structureExpress Gateway JWT issue
Will a contempt of congress lawsuit actually reach the merits?
Did 007 exist before James Bond?
How Can I Process Untrusted Data Sources Securely?
How to delete certain lists from a nested list?
What is the meaning of [[:space:]] in bash?
Why are road bikes (not time trial bikes) used in many triathlons?
How to say no to more work as a PhD student so I can graduate
Why doesn't philosophy have higher standards for its arguments?
How to ask my office to remove the pride decorations without appearing anti-LGBTQ?
Is the Gritty Realism variant incompatible with dungeon-based adventures?
Why does "git status" show I'm on the master branch and "git branch" does not?
Why did Steve Rogers choose this character in Endgame?
What powers the air required for pneumatic brakes in aircraft?
Why did Spider-Man take a detour to Dorset?
Unix chat server making communication between terminals possible
Why do candidates not quit if they no longer have a realistic chance to win in the 2020 US presidents election
Manually select/unselect lines before forwarding to stdout
Alternator dying so junk car?
How to remove the first colon ':' from a timestamp?
Is there an English equivalent for "Les carottes sont cuites", while keeping the vegetable reference?
What impact would a dragon the size of Asia have on the environment?
What do these three diagonal lines that cross through three measures and both staves mean, and what are they called?
Finding the package which provides a given command
License validity of unreleased project
JWT decode() must be of the type array error
Deleting an element from an array in PHPHow do I get PHP errors to display?Best practices for server-side handling of JWT tokensWhat is secret key for JWT based authentication and how to generate it?Should I decode the JWT on the client side?angular2 with Slim framework jwt authenticationJWT on .NET Core 2.0Pass decoded JWT payload to micro servicesExplainations on JWT Tokens structureExpress Gateway JWT issue
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Here I have some slim PHP code which is log in and a function to check whether it decode the JWT that store in the header.
$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);
$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);
return $this->response->withJson($payload,200)
->withHeader('Content-type', 'application/json;charset=utf-8', 200)
->withAddedHeader('Authorization', $token);
);
$app->get('/get', function ($request, $response)
$jwt = $request->getHeader("Authorization");
$settings = $this->get('settings');
$token = JWT::decode($jwt, $settings['jwt']['secret'], "HS256"); // $token store the token of the user
if ($token)
return $this->response->withJson($token, 200)
->withHeader('Content-type', 'application/json;charset=utf-8', 200);
return $this->response->withJson($token,401)
->withHeader('Content-type', 'application/json;charset=utf-8', 401);
);
But it when i try to run http://localhost:8080/get it returns an error which is
Argument 3 passed to FirebaseJWTJWT::decode() must be of the type array.
Why does it happen and how can i fix it?
php jwt slim
add a comment |
Here I have some slim PHP code which is log in and a function to check whether it decode the JWT that store in the header.
$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);
$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);
return $this->response->withJson($payload,200)
->withHeader('Content-type', 'application/json;charset=utf-8', 200)
->withAddedHeader('Authorization', $token);
);
$app->get('/get', function ($request, $response)
$jwt = $request->getHeader("Authorization");
$settings = $this->get('settings');
$token = JWT::decode($jwt, $settings['jwt']['secret'], "HS256"); // $token store the token of the user
if ($token)
return $this->response->withJson($token, 200)
->withHeader('Content-type', 'application/json;charset=utf-8', 200);
return $this->response->withJson($token,401)
->withHeader('Content-type', 'application/json;charset=utf-8', 401);
);
But it when i try to run http://localhost:8080/get it returns an error which is
Argument 3 passed to FirebaseJWTJWT::decode() must be of the type array.
Why does it happen and how can i fix it?
php jwt slim
Warning!strcmp($input['password'],$user->password)
suggests that you're not properly hashing the passwords. You should always use password_hash and password_verify() when dealing with passwords. Never store them as plain text or use any self-built hashing algorithm.
– Magnus Eriksson
Mar 26 at 8:17
Yeah i know, I'm not doing for production. Will implement security later on. Thank you
– Hisyam Syazani
Mar 26 at 8:21
what does theprint_r($settings);
show? try to debug it
– pr1nc3
Mar 26 at 8:29
it is my jwt setting that contain the key, see my new comment
– Hisyam Syazani
Mar 26 at 8:42
add a comment |
Here I have some slim PHP code which is log in and a function to check whether it decode the JWT that store in the header.
$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);
$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);
return $this->response->withJson($payload,200)
->withHeader('Content-type', 'application/json;charset=utf-8', 200)
->withAddedHeader('Authorization', $token);
);
$app->get('/get', function ($request, $response)
$jwt = $request->getHeader("Authorization");
$settings = $this->get('settings');
$token = JWT::decode($jwt, $settings['jwt']['secret'], "HS256"); // $token store the token of the user
if ($token)
return $this->response->withJson($token, 200)
->withHeader('Content-type', 'application/json;charset=utf-8', 200);
return $this->response->withJson($token,401)
->withHeader('Content-type', 'application/json;charset=utf-8', 401);
);
But it when i try to run http://localhost:8080/get it returns an error which is
Argument 3 passed to FirebaseJWTJWT::decode() must be of the type array.
Why does it happen and how can i fix it?
php jwt slim
Here I have some slim PHP code which is log in and a function to check whether it decode the JWT that store in the header.
$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);
$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);
return $this->response->withJson($payload,200)
->withHeader('Content-type', 'application/json;charset=utf-8', 200)
->withAddedHeader('Authorization', $token);
);
$app->get('/get', function ($request, $response)
$jwt = $request->getHeader("Authorization");
$settings = $this->get('settings');
$token = JWT::decode($jwt, $settings['jwt']['secret'], "HS256"); // $token store the token of the user
if ($token)
return $this->response->withJson($token, 200)
->withHeader('Content-type', 'application/json;charset=utf-8', 200);
return $this->response->withJson($token,401)
->withHeader('Content-type', 'application/json;charset=utf-8', 401);
);
But it when i try to run http://localhost:8080/get it returns an error which is
Argument 3 passed to FirebaseJWTJWT::decode() must be of the type array.
Why does it happen and how can i fix it?
php jwt slim
php jwt slim
edited Mar 26 at 8:23
Nik
1,1707 silver badges17 bronze badges
1,1707 silver badges17 bronze badges
asked Mar 26 at 8:11
Hisyam SyazaniHisyam Syazani
319 bronze badges
319 bronze badges
Warning!strcmp($input['password'],$user->password)
suggests that you're not properly hashing the passwords. You should always use password_hash and password_verify() when dealing with passwords. Never store them as plain text or use any self-built hashing algorithm.
– Magnus Eriksson
Mar 26 at 8:17
Yeah i know, I'm not doing for production. Will implement security later on. Thank you
– Hisyam Syazani
Mar 26 at 8:21
what does theprint_r($settings);
show? try to debug it
– pr1nc3
Mar 26 at 8:29
it is my jwt setting that contain the key, see my new comment
– Hisyam Syazani
Mar 26 at 8:42
add a comment |
Warning!strcmp($input['password'],$user->password)
suggests that you're not properly hashing the passwords. You should always use password_hash and password_verify() when dealing with passwords. Never store them as plain text or use any self-built hashing algorithm.
– Magnus Eriksson
Mar 26 at 8:17
Yeah i know, I'm not doing for production. Will implement security later on. Thank you
– Hisyam Syazani
Mar 26 at 8:21
what does theprint_r($settings);
show? try to debug it
– pr1nc3
Mar 26 at 8:29
it is my jwt setting that contain the key, see my new comment
– Hisyam Syazani
Mar 26 at 8:42
Warning!
strcmp($input['password'],$user->password)
suggests that you're not properly hashing the passwords. You should always use password_hash and password_verify() when dealing with passwords. Never store them as plain text or use any self-built hashing algorithm.– Magnus Eriksson
Mar 26 at 8:17
Warning!
strcmp($input['password'],$user->password)
suggests that you're not properly hashing the passwords. You should always use password_hash and password_verify() when dealing with passwords. Never store them as plain text or use any self-built hashing algorithm.– Magnus Eriksson
Mar 26 at 8:17
Yeah i know, I'm not doing for production. Will implement security later on. Thank you
– Hisyam Syazani
Mar 26 at 8:21
Yeah i know, I'm not doing for production. Will implement security later on. Thank you
– Hisyam Syazani
Mar 26 at 8:21
what does the
print_r($settings);
show? try to debug it– pr1nc3
Mar 26 at 8:29
what does the
print_r($settings);
show? try to debug it– pr1nc3
Mar 26 at 8:29
it is my jwt setting that contain the key, see my new comment
– Hisyam Syazani
Mar 26 at 8:42
it is my jwt setting that contain the key, see my new comment
– Hisyam Syazani
Mar 26 at 8:42
add a comment |
2 Answers
2
active
oldest
votes
Try to follow what the error says:
$token = JWT::decode($jwt, $settings['jwt']['secret'], ["HS256"]);
You can see the Example of using here
still the same error, btw this is the bit of the error imgur.com/a/wMhwAAm
– Hisyam Syazani
Mar 26 at 8:20
it does say something like FirebaseJWTJWT::decode(Array, 'supersecretkeyy...', Array) with ur code . But with my code, its say FirebaseJWTJWT::decode(Array, 'supersecretkeyy...', "HS256")
– Hisyam Syazani
Mar 26 at 8:22
The key error isWrong number of segments
, your token is not a valid JWT, please print what you are passing intodecode
function
– Nik
Mar 26 at 8:32
Please accept the best answer if this helped to solve the problem ;)
– Nik
Mar 28 at 11:40
add a comment |
If i decode in the same function it returns the decoded JWT, but if i decode in other function it returns an error. How to pass the jwt to other function?
$app->post('/login', function ($request, $response)
$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, $key); // $token store the token of the user
catch (Exception $e)
echo json_encode($e);
// return $this->response->withJson($payload,200)
// ->withHeader('Content-type', 'application/json;charset=utf-8', 200)
// ->withHeader('Authorization', $token);
$decoded = JWT::decode($token, $key, array('HS256'));
print_r($decoded);
);
As I wrote, your token is invalid when you do a request, print$jwt
variable after$jwt = $request->getHeader("Authorization");
line.
– Nik
Mar 26 at 8:45
1
Edit your question please and don't post an updated question as an answer.
– pr1nc3
Mar 26 at 8:48
yeah, my jwt is empty.
– Hisyam Syazani
Mar 26 at 8:49
@pr1nc3 ok, sorry quite new to stack
– Hisyam Syazani
Mar 26 at 8:50
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55352476%2fjwt-decode-must-be-of-the-type-array-error%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try to follow what the error says:
$token = JWT::decode($jwt, $settings['jwt']['secret'], ["HS256"]);
You can see the Example of using here
still the same error, btw this is the bit of the error imgur.com/a/wMhwAAm
– Hisyam Syazani
Mar 26 at 8:20
it does say something like FirebaseJWTJWT::decode(Array, 'supersecretkeyy...', Array) with ur code . But with my code, its say FirebaseJWTJWT::decode(Array, 'supersecretkeyy...', "HS256")
– Hisyam Syazani
Mar 26 at 8:22
The key error isWrong number of segments
, your token is not a valid JWT, please print what you are passing intodecode
function
– Nik
Mar 26 at 8:32
Please accept the best answer if this helped to solve the problem ;)
– Nik
Mar 28 at 11:40
add a comment |
Try to follow what the error says:
$token = JWT::decode($jwt, $settings['jwt']['secret'], ["HS256"]);
You can see the Example of using here
still the same error, btw this is the bit of the error imgur.com/a/wMhwAAm
– Hisyam Syazani
Mar 26 at 8:20
it does say something like FirebaseJWTJWT::decode(Array, 'supersecretkeyy...', Array) with ur code . But with my code, its say FirebaseJWTJWT::decode(Array, 'supersecretkeyy...', "HS256")
– Hisyam Syazani
Mar 26 at 8:22
The key error isWrong number of segments
, your token is not a valid JWT, please print what you are passing intodecode
function
– Nik
Mar 26 at 8:32
Please accept the best answer if this helped to solve the problem ;)
– Nik
Mar 28 at 11:40
add a comment |
Try to follow what the error says:
$token = JWT::decode($jwt, $settings['jwt']['secret'], ["HS256"]);
You can see the Example of using here
Try to follow what the error says:
$token = JWT::decode($jwt, $settings['jwt']['secret'], ["HS256"]);
You can see the Example of using here
answered Mar 26 at 8:18
NikNik
1,1707 silver badges17 bronze badges
1,1707 silver badges17 bronze badges
still the same error, btw this is the bit of the error imgur.com/a/wMhwAAm
– Hisyam Syazani
Mar 26 at 8:20
it does say something like FirebaseJWTJWT::decode(Array, 'supersecretkeyy...', Array) with ur code . But with my code, its say FirebaseJWTJWT::decode(Array, 'supersecretkeyy...', "HS256")
– Hisyam Syazani
Mar 26 at 8:22
The key error isWrong number of segments
, your token is not a valid JWT, please print what you are passing intodecode
function
– Nik
Mar 26 at 8:32
Please accept the best answer if this helped to solve the problem ;)
– Nik
Mar 28 at 11:40
add a comment |
still the same error, btw this is the bit of the error imgur.com/a/wMhwAAm
– Hisyam Syazani
Mar 26 at 8:20
it does say something like FirebaseJWTJWT::decode(Array, 'supersecretkeyy...', Array) with ur code . But with my code, its say FirebaseJWTJWT::decode(Array, 'supersecretkeyy...', "HS256")
– Hisyam Syazani
Mar 26 at 8:22
The key error isWrong number of segments
, your token is not a valid JWT, please print what you are passing intodecode
function
– Nik
Mar 26 at 8:32
Please accept the best answer if this helped to solve the problem ;)
– Nik
Mar 28 at 11:40
still the same error, btw this is the bit of the error imgur.com/a/wMhwAAm
– Hisyam Syazani
Mar 26 at 8:20
still the same error, btw this is the bit of the error imgur.com/a/wMhwAAm
– Hisyam Syazani
Mar 26 at 8:20
it does say something like FirebaseJWTJWT::decode(Array, 'supersecretkeyy...', Array) with ur code . But with my code, its say FirebaseJWTJWT::decode(Array, 'supersecretkeyy...', "HS256")
– Hisyam Syazani
Mar 26 at 8:22
it does say something like FirebaseJWTJWT::decode(Array, 'supersecretkeyy...', Array) with ur code . But with my code, its say FirebaseJWTJWT::decode(Array, 'supersecretkeyy...', "HS256")
– Hisyam Syazani
Mar 26 at 8:22
The key error is
Wrong number of segments
, your token is not a valid JWT, please print what you are passing into decode
function– Nik
Mar 26 at 8:32
The key error is
Wrong number of segments
, your token is not a valid JWT, please print what you are passing into decode
function– Nik
Mar 26 at 8:32
Please accept the best answer if this helped to solve the problem ;)
– Nik
Mar 28 at 11:40
Please accept the best answer if this helped to solve the problem ;)
– Nik
Mar 28 at 11:40
add a comment |
If i decode in the same function it returns the decoded JWT, but if i decode in other function it returns an error. How to pass the jwt to other function?
$app->post('/login', function ($request, $response)
$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, $key); // $token store the token of the user
catch (Exception $e)
echo json_encode($e);
// return $this->response->withJson($payload,200)
// ->withHeader('Content-type', 'application/json;charset=utf-8', 200)
// ->withHeader('Authorization', $token);
$decoded = JWT::decode($token, $key, array('HS256'));
print_r($decoded);
);
As I wrote, your token is invalid when you do a request, print$jwt
variable after$jwt = $request->getHeader("Authorization");
line.
– Nik
Mar 26 at 8:45
1
Edit your question please and don't post an updated question as an answer.
– pr1nc3
Mar 26 at 8:48
yeah, my jwt is empty.
– Hisyam Syazani
Mar 26 at 8:49
@pr1nc3 ok, sorry quite new to stack
– Hisyam Syazani
Mar 26 at 8:50
add a comment |
If i decode in the same function it returns the decoded JWT, but if i decode in other function it returns an error. How to pass the jwt to other function?
$app->post('/login', function ($request, $response)
$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, $key); // $token store the token of the user
catch (Exception $e)
echo json_encode($e);
// return $this->response->withJson($payload,200)
// ->withHeader('Content-type', 'application/json;charset=utf-8', 200)
// ->withHeader('Authorization', $token);
$decoded = JWT::decode($token, $key, array('HS256'));
print_r($decoded);
);
As I wrote, your token is invalid when you do a request, print$jwt
variable after$jwt = $request->getHeader("Authorization");
line.
– Nik
Mar 26 at 8:45
1
Edit your question please and don't post an updated question as an answer.
– pr1nc3
Mar 26 at 8:48
yeah, my jwt is empty.
– Hisyam Syazani
Mar 26 at 8:49
@pr1nc3 ok, sorry quite new to stack
– Hisyam Syazani
Mar 26 at 8:50
add a comment |
If i decode in the same function it returns the decoded JWT, but if i decode in other function it returns an error. How to pass the jwt to other function?
$app->post('/login', function ($request, $response)
$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, $key); // $token store the token of the user
catch (Exception $e)
echo json_encode($e);
// return $this->response->withJson($payload,200)
// ->withHeader('Content-type', 'application/json;charset=utf-8', 200)
// ->withHeader('Authorization', $token);
$decoded = JWT::decode($token, $key, array('HS256'));
print_r($decoded);
);
If i decode in the same function it returns the decoded JWT, but if i decode in other function it returns an error. How to pass the jwt to other function?
$app->post('/login', function ($request, $response)
$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, $key); // $token store the token of the user
catch (Exception $e)
echo json_encode($e);
// return $this->response->withJson($payload,200)
// ->withHeader('Content-type', 'application/json;charset=utf-8', 200)
// ->withHeader('Authorization', $token);
$decoded = JWT::decode($token, $key, array('HS256'));
print_r($decoded);
);
answered Mar 26 at 8:41
Hisyam SyazaniHisyam Syazani
319 bronze badges
319 bronze badges
As I wrote, your token is invalid when you do a request, print$jwt
variable after$jwt = $request->getHeader("Authorization");
line.
– Nik
Mar 26 at 8:45
1
Edit your question please and don't post an updated question as an answer.
– pr1nc3
Mar 26 at 8:48
yeah, my jwt is empty.
– Hisyam Syazani
Mar 26 at 8:49
@pr1nc3 ok, sorry quite new to stack
– Hisyam Syazani
Mar 26 at 8:50
add a comment |
As I wrote, your token is invalid when you do a request, print$jwt
variable after$jwt = $request->getHeader("Authorization");
line.
– Nik
Mar 26 at 8:45
1
Edit your question please and don't post an updated question as an answer.
– pr1nc3
Mar 26 at 8:48
yeah, my jwt is empty.
– Hisyam Syazani
Mar 26 at 8:49
@pr1nc3 ok, sorry quite new to stack
– Hisyam Syazani
Mar 26 at 8:50
As I wrote, your token is invalid when you do a request, print
$jwt
variable after $jwt = $request->getHeader("Authorization");
line.– Nik
Mar 26 at 8:45
As I wrote, your token is invalid when you do a request, print
$jwt
variable after $jwt = $request->getHeader("Authorization");
line.– Nik
Mar 26 at 8:45
1
1
Edit your question please and don't post an updated question as an answer.
– pr1nc3
Mar 26 at 8:48
Edit your question please and don't post an updated question as an answer.
– pr1nc3
Mar 26 at 8:48
yeah, my jwt is empty.
– Hisyam Syazani
Mar 26 at 8:49
yeah, my jwt is empty.
– Hisyam Syazani
Mar 26 at 8:49
@pr1nc3 ok, sorry quite new to stack
– Hisyam Syazani
Mar 26 at 8:50
@pr1nc3 ok, sorry quite new to stack
– Hisyam Syazani
Mar 26 at 8:50
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55352476%2fjwt-decode-must-be-of-the-type-array-error%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Warning!
strcmp($input['password'],$user->password)
suggests that you're not properly hashing the passwords. You should always use password_hash and password_verify() when dealing with passwords. Never store them as plain text or use any self-built hashing algorithm.– Magnus Eriksson
Mar 26 at 8:17
Yeah i know, I'm not doing for production. Will implement security later on. Thank you
– Hisyam Syazani
Mar 26 at 8:21
what does the
print_r($settings);
show? try to debug it– pr1nc3
Mar 26 at 8:29
it is my jwt setting that contain the key, see my new comment
– Hisyam Syazani
Mar 26 at 8:42