31 lines
1.0 KiB
JavaScript
31 lines
1.0 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.authenticateWsToken = authenticateWsToken;
|
|
exports.extractBearerToken = extractBearerToken;
|
|
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
const config_1 = require("../../config");
|
|
function authenticateWsToken(token) {
|
|
if (!token)
|
|
return null;
|
|
try {
|
|
const decoded = jsonwebtoken_1.default.verify(token, config_1.config.jwt.secret);
|
|
if (!decoded?.userId)
|
|
return null;
|
|
return { userId: decoded.userId, role: decoded.role };
|
|
}
|
|
catch {
|
|
return null;
|
|
}
|
|
}
|
|
function extractBearerToken(headerValue) {
|
|
if (!headerValue)
|
|
return undefined;
|
|
const [scheme, token] = headerValue.split(' ');
|
|
if (scheme?.toLowerCase() !== 'bearer' || !token)
|
|
return undefined;
|
|
return token;
|
|
}
|
|
//# sourceMappingURL=auth.js.map
|