const jwt = require('jsonwebtoken');
/**
* Verifies the given JWT using the provided public key.
*
* @param {string} token - The JWT to be verified.
* @param {string} publicKey - The public key to verify the JWT signature.
* @returns {object | null} The decoded JWT payload if the verification is successful, null otherwise.
* @throws {JsonWebTokenError} Throws an error if verification fails.
*/
function verifyJWT(token, publicKey) {
try {
const decoded = jwt.verify(token, publicKey, { algorithms: ['RS256'] });
return decoded;
} catch (error) {
console.error("JWT verification failed:", error.message);
return null;
}
}
module.exports = {
verifyJWT
};