@ngn-net/giftcard-shared
Version:
Shared library for gifts score microservices
72 lines • 2.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TwoFACredentials = exports.UserDetail = exports.Jwt = exports.UserRoles = exports.AuthUser = void 0;
const common_1 = require("@nestjs/common");
const class_transformer_1 = require("class-transformer");
const class_validator_1 = require("class-validator");
const user_dto_1 = require("./dto/user.dto");
exports.AuthUser = (0, common_1.createParamDecorator)((data, ctx) => {
try {
const auth = ctx.switchToHttp().getRequest().auth;
return auth.sub;
}
catch (error) {
throw new common_1.HttpException('Forbidden', common_1.HttpStatus.UNAUTHORIZED);
}
});
exports.UserRoles = (0, common_1.createParamDecorator)((data, ctx) => {
try {
const auth = ctx.switchToHttp().getRequest().auth;
return auth.roles;
}
catch (error) {
throw new common_1.HttpException('Forbidden', common_1.HttpStatus.UNAUTHORIZED);
}
});
exports.Jwt = (0, common_1.createParamDecorator)((data, ctx) => {
try {
const auth = ctx.switchToHttp().getRequest().auth;
return auth;
}
catch (error) {
throw new common_1.HttpException('Forbidden', common_1.HttpStatus.UNAUTHORIZED);
}
});
exports.UserDetail = (0, common_1.createParamDecorator)((data, ctx) => {
var _a;
const auth = (_a = ctx.switchToHttp().getRequest()) === null || _a === void 0 ? void 0 : _a.auth;
const headers = ctx.switchToHttp().getRequest().headers;
const ip = headers['x-forwarded-for'] ||
headers['x-real-ip'] ||
headers['remote-addr'] ||
ctx.switchToHttp().getRequest().ip ||
'';
const agent = headers['user-agent'];
const userDetail = {
id: auth === null || auth === void 0 ? void 0 : auth.sub,
username: auth === null || auth === void 0 ? void 0 : auth.username,
ipAddress: ip.trim(),
agent: agent,
};
return userDetail;
});
exports.TwoFACredentials = (0, common_1.createParamDecorator)(async (data, ctx) => {
try {
const headers = ctx.switchToHttp().getRequest().headers;
const passwd = headers.txpasswd;
const token = headers.gauthtoken;
const dto = (0, class_transformer_1.plainToInstance)(user_dto_1.GetCredentialDto, {
passwd,
token,
});
await (0, class_validator_1.validateOrReject)(dto);
return {
passwd,
token,
};
}
catch (error) {
throw new common_1.HttpException('Forbidden', common_1.HttpStatus.UNAUTHORIZED);
}
});
//# sourceMappingURL=auth.decorator.js.map