@nestjs-cognito/core
Version:
Cognito Provider for NestJS
50 lines • 1.94 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CookieJwtExtractor = void 0;
/**
* JWT extractor implementation that extracts tokens from HTTP-only cookies.
* Useful for web applications that store JWT tokens in secure cookies instead of headers.
*
* @example
* ```typescript
* // Usage in module configuration
* CognitoModule.register({
* jwtExtractor: new CookieJwtExtractor('access_token'),
* // ... other options
* })
* ```
*/
class CookieJwtExtractor {
/**
* Creates a new cookie-based JWT extractor.
* @param cookieName - The name of the cookie containing the JWT token. Defaults to 'access_token'
*/
constructor(cookieName = 'access_token') {
this.cookieName = cookieName;
}
/**
* Checks if the request has authentication information in the specified cookie.
* @param request - The request object (must have cookies property)
* @returns True if the cookie is present and not empty
*/
hasAuthenticationInfo(request) {
const cookies = (request === null || request === void 0 ? void 0 : request.cookies) || {};
const token = cookies === null || cookies === void 0 ? void 0 : cookies[this.cookieName];
return Boolean(token && token.trim());
}
/**
* Extracts the JWT token from the specified cookie.
* @param request - The request object (must have cookies property)
* @returns The JWT token string or null if not found
*/
getAuthorizationToken(request) {
const cookies = (request === null || request === void 0 ? void 0 : request.cookies) || {};
const token = cookies === null || cookies === void 0 ? void 0 : cookies[this.cookieName];
if (!token || !token.trim()) {
return null;
}
return token.trim();
}
}
exports.CookieJwtExtractor = CookieJwtExtractor;
//# sourceMappingURL=cookie-jwt.extractor.js.map