@mercury-labs/nest-auth
Version:
Mercury framework auth library. It supports local auth, jwt with both bearer token and cookie, basic auth.
45 lines • 2 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.transferTokenFromResponseToCookie = void 0;
const domain_1 = require("../../../domain");
const transferTokenFromResponseToCookie = (response, definitions) => (authResponse, mapKeys) => {
if ((response.httpAdaptorType === 'fastify' && !response.cookie) ||
(response.httpAdaptorType === 'express' && !response.cookie) ||
!definitions.transferTokenMethod ||
![
domain_1.AuthTransferTokenMethod.COOKIE_ONLY,
domain_1.AuthTransferTokenMethod.BOTH,
].includes(definitions.transferTokenMethod)) {
return authResponse;
}
const token = authResponse.token;
for (const responseKey in mapKeys) {
const currentToken = token[responseKey];
const currentKey = mapKeys[responseKey];
if (currentToken !== null && currentToken !== undefined) {
const cookieOptions = {
path: '/',
httpOnly: true,
sameSite: 'none',
secure: process.env.NODE_ENV !== 'local',
...definitions.cookieOptions,
expires: responseKey === 'refreshToken'
? token.refreshTokenExpiryDate
: token.expiryDate,
};
if (response.httpAdaptorType === 'fastify' && response.cookie) {
response.cookie(currentKey, currentToken, cookieOptions);
}
if (response.httpAdaptorType === 'express' && response.cookie) {
response.cookie(currentKey, currentToken, cookieOptions);
}
if (definitions.transferTokenMethod ===
domain_1.AuthTransferTokenMethod.COOKIE_ONLY) {
delete token[responseKey];
}
}
}
return { ...authResponse, token };
};
exports.transferTokenFromResponseToCookie = transferTokenFromResponseToCookie;
//# sourceMappingURL=cookie.interceptor.helper.js.map
;