@fran-834/gs-microservice-core
Version:
Core package for Node.js microservices by Galduria Software. Includes security, logging, validation, and error handling middlewares.
37 lines (36 loc) • 1.51 kB
JavaScript
import { getAuthSecret } from "../config/auth.config.js";
import jwt from "jsonwebtoken";
import { AppError, commonErrors, commonHTTPErrors } from "../helpers/errors/index.js";
const verifyPublicToken = (req, res, next) => {
try {
//console.log("Verificando token: ", req.headers);
const authHeader = req.headers["authorization"];
const token = typeof authHeader === "string" ? authHeader.split(" ")[1] : undefined;
if (!token) {
throw new AppError(commonErrors.unauthorized, commonHTTPErrors.unauthorized, "No token provided");
}
const secret = getAuthSecret();
//console.log("token", token);
jwt.verify(token, secret, (error, decoded) => {
if (error) {
throw new AppError(commonErrors.unauthorized, commonHTTPErrors.unauthorized, "Unauthorized");
}
const payload = decoded;
req.companyId = payload?.companyId;
req.module = payload?.module;
req.permissions = payload?.permissions;
req.audience = payload?.audience;
req.recipient = payload?.recipient; // For backward compatibility
// Example values:
// req.companyId = "companyId123";
// req.module = "bookings";
// req.permissions = ["read", "write", "delete"];
// req.audience = "web";
next();
});
}
catch (error) {
next(error);
}
};
export default verifyPublicToken;