@replyke/express
Version:
Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.
27 lines (26 loc) • 977 B
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
const config_1 = require("../config");
// Unlike "requireUserAuth", this function would still allow the request to pass through
exports.default = (req, _, next) => {
const authHeader = req.headers["authorization"];
if (!authHeader) {
next();
return;
}
const token = authHeader.split(" ")[1];
const { accessTokenSecret } = (0, config_1.getCoreConfig)();
jsonwebtoken_1.default.verify(token, accessTokenSecret, (err, // Type for error from jwt.verify
decoded) => {
if (err || !decoded || typeof decoded === "string" || !decoded.sub) {
next();
return;
}
req.userId = decoded.sub;
next();
});
};