@vendure/core
Version:
A modern, headless ecommerce framework
38 lines • 1.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractSessionToken = extractSessionToken;
/**
* Get the session token from either the cookie or the Authorization header, depending
* on the configured tokenMethod.
*/
function extractSessionToken(req, tokenMethod) {
const tokenFromCookie = getFromCookie(req);
const tokenFromHeader = getFromHeader(req);
if (tokenMethod === 'cookie') {
return tokenFromCookie;
}
else if (tokenMethod === 'bearer') {
return tokenFromHeader;
}
if (tokenMethod.includes('cookie') && tokenFromCookie) {
return tokenFromCookie;
}
if (tokenMethod.includes('bearer') && tokenFromHeader) {
return tokenFromHeader;
}
}
function getFromCookie(req) {
if (req.session && req.session.token) {
return req.session.token;
}
}
function getFromHeader(req) {
const authHeader = req.get('Authorization');
if (authHeader) {
const matches = authHeader.trim().match(/^bearer\s(.+)$/i);
if (matches) {
return matches[1];
}
}
}
//# sourceMappingURL=extract-session-token.js.map