Im using JWTs with Auth0. Im verifying the token with the public key (pemCert).
However I've seen examples where the token is decoded in other ways eg with a private key. Is using the public key equally secure as other methods?
const jwt = require('jsonwebtoken');
function authoriseToken(req, res, next) {
const token = req.headers.authorization;
const pemCert = process.env.JWT_PEM;
// If there is no token the user is not logged in
if (!token || token.length === 0) {
next();
return;
}
const tokenCrop = token.replace('Bearer ', '');
jwt.verify(
tokenCrop,
pemCert,
{ algorithm: 'RS256' },
(err, decodedToken) => {
if (err) {
// If the token isn't verified eg expired then forward as if no token
next();
} else if (decodedToken) {
// If the token is verified then add fields to the res
req.authId = decodedToken.sub
next();
}
},
);
}