bb-inspired
Version:
Core library for BB-inspired NestJS backend
85 lines • 3.69 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AuthService = void 0;
const common_1 = require("@nestjs/common");
const axios_1 = require("@nestjs/axios");
const rxjs_1 = require("rxjs");
let AuthService = class AuthService {
constructor(httpService, config) {
var _a;
this.httpService = httpService;
this.config = config;
this.authServiceUrl = ((_a = this.config) === null || _a === void 0 ? void 0 : _a.authServiceUrl) || 'http://localhost:3001/api/auth';
}
async validateUser(username, password) {
const response = await (0, rxjs_1.firstValueFrom)(this.httpService.post(`${this.authServiceUrl}/login`, {
email: username,
password,
}));
return response.data;
}
async validateToken(token) {
try {
const response = await (0, rxjs_1.firstValueFrom)(this.httpService.post(`${this.authServiceUrl}/token/validate`, { token }));
return response.data;
}
catch (error) {
return {
valid: false,
error: error.message || 'Failed to validate token'
};
}
}
async checkPermissions(userId, permissions) {
try {
const response = await (0, rxjs_1.firstValueFrom)(this.httpService.post(`${this.authServiceUrl}/users/${userId}/check-permissions`, { permissions }));
return response.data;
}
catch (error) {
return {
hasPermission: false,
missingPermissions: permissions,
};
}
}
async checkRoles(userId, roles) {
try {
const response = await (0, rxjs_1.firstValueFrom)(this.httpService.post(`${this.authServiceUrl}/users/${userId}/check-roles`, { roles }));
return response.data;
}
catch (error) {
return {
hasRoles: false,
missingRoles: roles,
};
}
}
async getUserById(userId) {
const response = await (0, rxjs_1.firstValueFrom)(this.httpService.get(`${this.authServiceUrl}/users/${userId}/profile`));
return response.data;
}
async getUserPermissions(userId) {
const response = await (0, rxjs_1.firstValueFrom)(this.httpService.get(`${this.authServiceUrl}/users/${userId}/permissions`));
return response.data.permissions;
}
};
exports.AuthService = AuthService;
exports.AuthService = AuthService = __decorate([
(0, common_1.Injectable)(),
__param(1, (0, common_1.Optional)()),
__param(1, (0, common_1.Inject)('AUTH_SERVICE_CONFIG')),
__metadata("design:paramtypes", [axios_1.HttpService, Object])
], AuthService);
//# sourceMappingURL=auth.service.js.map