I need to replace Bearer from the header to verify the Token
if bearerHeader is something like "Bearer 456513" then your code
bearerHeader.replace("Bearer","");
will result: " 456513" (there are space before the token)
bearerHeader.replace('Bearer ','');
may solve your issue but I recommend to verify the authentification scheme first ("Bearer" term is really "Bearer"):
var parts = bearerHeader.split(' ');
if (parts.length === 2) {
var scheme = parts[0];
var credentials = parts[1];
if (/^Bearer$/i.test(scheme)) {
token = credentials;
//verify token
jwt.verify(token, 'super secret', function(err, decoded) {
}
}
}
You can try to split()
the string on spaces and discard the first element
// OPTION 1
bearerHeader.split(" ")[1];
or you can simple cut Bearer
from the string
// OPTION 2
bearerHeader.replace("Bearer", "");
Try this
bearer = bearerHeader.replace(/^Bearer\s/, '');
jwt.verify(bearer, 'super_secret', function (err, decoded) {
console.log(err);
console.log(decoded);`
}