Saturday, May 11, 2024
 Popular · Latest · Hot · Upcoming
87
rated 0 times [  89] [ 2]  / answers: 1 / hits: 23298  / 8 Years ago, tue, august 23, 2016, 12:00:00

i get the following error, using the jwt-simple lib:



TypeError: Cannot read property 'split' of undefined
at module.exports (C:my_applicationservicesmylist.js:5:40)
at Layer.handle [as handle_request] (C:my_applicationnode_modulesexpresslibrouterlayer.js:95:5)
at next (C:my_applicationnode_modulesexpresslibrouterroute.js:131:13)
at Route.dispatch (C:my_applicationnode_modulesexpresslibrouterroute.js:112:3)
at Layer.handle [as handle_request] (C:my_applicationnode_modulesexpresslibrouterlayer.js:95:5)
at C:my_applicationnode_modulesexpresslibrouterindex.js:277:22
at Function.process_params (C:my_applicationnode_modulesexpresslibrouterindex.js:330:12)
at next (C:my_applicationnode_modulesexpresslibrouterindex.js:271:10)
at C:my_applicationapi.js:39:3
at Layer.handle [as handle_request] (C:my_applicationnode_modulesexpresslibrouterlayer.js:95:5)
at trim_prefix (C:my_applicationnode_modulesexpresslibrouterindex.js:312:13)
at C:my_applicationnode_modulesexpresslibrouterindex.js:280:7
at Function.process_params (C:my_applicationnode_modulesexpresslibrouterindex.js:330:12)
at next (C:my_applicationnode_modulesexpresslibrouterindex.js:271:10)
at logger (C:my_applicationnode_modulesmorganindex.js:144:5)
at Layer.handle [as handle_request] (C:my_applicationnode_modulesexpresslibrouterlayer.js:95:5)


and here is mylist.js file:



var jwt = require('jwt-simple');


module.exports = function (req, res) {
var token = req.headers.authorization.split(' ')[1];
var payload = jwt.decode(token, shhh..);
if(!payload.sub) {
res.status(401).send({
message: 'Authentication failed'
});
}
if(!req.headers.authorization){
return res.status(401).send({
message: 'You are not authorized'
});
}
res.json(mylist);
};

var mylist = [
'Proj 1',
'Proj 2',
'Proj 3',
'Proj 4'
];


i am trying to see if the user is authorized to access the mylist resource on frontend.

does anyone have any idea?


More From » node.js

 Answers
56

you assume it's a string, even if you don't know if there really is a string there.
You should add some error checking first



module.exports = function (req, res) {
if (typeof req.headers.authorization !== 'string') {
res.sendStatus(400);
return;
}

var tokens = req.headers.authorization.split(' ');

if (tokens.length < 2) {
res.sendStatus(400);
return;
}

var token = tokens[1];

var payload = jwt.decode(token, shhh..);
if(!payload.sub) {
res.status(401).send({
message: 'Authentication failed'
});
}
...
};


Edit: But why exactly do you want the second token and not the first?


[#60946] Saturday, August 20, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
stefanicarolinat

Total Points: 145
Total Questions: 91
Total Answers: 93

Location: Cambodia
Member since Thu, Oct 7, 2021
3 Years ago
stefanicarolinat questions
Mon, Nov 15, 21, 00:00, 3 Years ago
Fri, Apr 16, 21, 00:00, 3 Years ago
Thu, Oct 15, 20, 00:00, 4 Years ago
Fri, Jul 17, 20, 00:00, 4 Years ago
;