41 lines
1.4 KiB
JavaScript
41 lines
1.4 KiB
JavaScript
"use strict";
|
|
/**
|
|
* Optional JWT authentication — attaches user when a valid Bearer token is present,
|
|
* otherwise continues without req.user (for guest support routes).
|
|
*/
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.optionalAuthMiddleware = optionalAuthMiddleware;
|
|
exports.readVisitorToken = readVisitorToken;
|
|
const authService_1 = require("../services/authService");
|
|
const onlinePresence_1 = require("../realtime/redis/onlinePresence");
|
|
async function optionalAuthMiddleware(req, res, next) {
|
|
const authHeader = req.headers.authorization;
|
|
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
|
next();
|
|
return;
|
|
}
|
|
const token = authHeader.slice(7);
|
|
try {
|
|
const payload = await (0, authService_1.verifyToken)(token);
|
|
req.user = payload;
|
|
if (payload.userId)
|
|
void (0, onlinePresence_1.touchAppPresence)(payload.userId);
|
|
next();
|
|
}
|
|
catch {
|
|
res.status(401).json({ error: 'Invalid or expired token' });
|
|
}
|
|
}
|
|
/** Reads anonymous visitor token from header or query. */
|
|
function readVisitorToken(req) {
|
|
const header = req.headers['x-support-visitor-token'];
|
|
if (typeof header === 'string' && header.trim().length >= 16) {
|
|
return header.trim();
|
|
}
|
|
const q = req.query.visitorToken;
|
|
if (typeof q === 'string' && q.trim().length >= 16) {
|
|
return q.trim();
|
|
}
|
|
return null;
|
|
}
|
|
//# sourceMappingURL=optionalAuth.js.map
|