mongodb-dynamic-api
Version:
Auto generated CRUD API for MongoDB using NestJS
134 lines • 7.02 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.localStrategyProviderName = exports.createLocalStrategyProvider = exports.createAuthGateway = exports.createAuthServiceProvider = exports.createAuthController = exports.authGatewayProviderName = exports.authServiceProviderName = void 0;
const common_1 = require("@nestjs/common");
const jwt_1 = require("@nestjs/jwt");
const mongoose_1 = require("@nestjs/mongoose");
const passport_1 = require("@nestjs/passport");
const swagger_1 = require("@nestjs/swagger");
const websockets_1 = require("@nestjs/websockets");
const mongoose_2 = require("mongoose");
const passport_local_1 = require("passport-local");
const decorators_1 = require("../../decorators");
const dynamic_api_module_1 = require("../../dynamic-api.module");
const filters_1 = require("../../filters");
const services_1 = require("../../services");
const mixins_1 = require("./mixins");
const services_2 = require("./services");
const authServiceProviderName = 'DynamicApiAuthService';
exports.authServiceProviderName = authServiceProviderName;
const authGatewayProviderName = 'DynamicApiAuthGateway';
exports.authGatewayProviderName = authGatewayProviderName;
const localStrategyProviderName = 'DynamicApiLocalStrategy';
exports.localStrategyProviderName = localStrategyProviderName;
function createLocalStrategyProvider(loginField, passwordField, abilityPredicate) {
let LocalStrategy = class LocalStrategy extends (0, passport_1.PassportStrategy)(passport_local_1.Strategy) {
constructor(authService) {
super({
usernameField: loginField,
passwordField: passwordField,
});
this.authService = authService;
this.abilityPredicate = abilityPredicate;
}
async validate(login, pass) {
const user = await this.authService.validateUser(login, pass);
if (!user) {
throw new common_1.UnauthorizedException('Invalid credentials');
}
if (this.abilityPredicate && !this.abilityPredicate(user)) {
throw new common_1.ForbiddenException('Access denied');
}
return user;
}
};
LocalStrategy = __decorate([
(0, common_1.Injectable)(),
__param(0, (0, common_1.Inject)(authServiceProviderName)),
__metadata("design:paramtypes", [Object])
], LocalStrategy);
return {
provide: localStrategyProviderName,
useClass: LocalStrategy,
};
}
exports.createLocalStrategyProvider = createLocalStrategyProvider;
function createAuthServiceProvider(userEntity, { loginField, passwordField, additionalFields = [], callback: loginCallback }, registerCallback, resetPasswordOptions, updateAccountCallback, beforeRegisterCallback, beforeUpdateAccountCallback) {
let AuthService = class AuthService extends services_2.BaseAuthService {
constructor(model, jwtService, bcryptService) {
super(model, jwtService, bcryptService);
this.model = model;
this.jwtService = jwtService;
this.bcryptService = bcryptService;
this.entity = userEntity;
this.additionalRequestFields = additionalFields;
this.loginField = loginField;
this.passwordField = passwordField;
this.beforeRegisterCallback = beforeRegisterCallback;
this.registerCallback = registerCallback;
this.beforeUpdateAccountCallback = beforeUpdateAccountCallback;
this.updateAccountCallback = updateAccountCallback;
this.loginCallback = loginCallback;
this.resetPasswordOptions = resetPasswordOptions;
}
};
AuthService = __decorate([
__param(0, (0, mongoose_1.InjectModel)(userEntity.name, dynamic_api_module_1.DynamicApiModule.state.get('connectionName'))),
__metadata("design:paramtypes", [mongoose_2.Model,
jwt_1.JwtService,
services_1.BcryptService])
], AuthService);
return {
provide: authServiceProviderName,
useClass: AuthService,
};
}
exports.createAuthServiceProvider = createAuthServiceProvider;
function createAuthController(userEntity, { loginField, passwordField, additionalFields }, registerOptions, validationPipeOptions, resetPasswordOptions, updateAccountOptions) {
let AuthController = class AuthController extends (0, mixins_1.AuthControllerMixin)(userEntity, loginField, passwordField, additionalFields, registerOptions, resetPasswordOptions, updateAccountOptions) {
constructor(service) {
super(service);
this.service = service;
}
};
AuthController = __decorate([
(0, common_1.Controller)('auth'),
(0, swagger_1.ApiTags)('Auth'),
(0, decorators_1.ValidatorPipe)(validationPipeOptions),
__param(0, (0, common_1.Inject)(authServiceProviderName)),
__metadata("design:paramtypes", [Object])
], AuthController);
return AuthController;
}
exports.createAuthController = createAuthController;
function createAuthGateway(userEntity, loginOptions, registerOptions, validationPipeOptions, resetPasswordOptions, updateAccountOptions, gatewayOptions) {
let AuthGateway = class AuthGateway extends (0, mixins_1.AuthGatewayMixin)(userEntity, loginOptions, registerOptions ?? {}, resetPasswordOptions, updateAccountOptions) {
constructor(service, jwtService) {
super(service, jwtService);
this.service = service;
this.jwtService = jwtService;
}
};
AuthGateway = __decorate([
(0, websockets_1.WebSocketGateway)(gatewayOptions),
(0, common_1.UseFilters)(new filters_1.DynamicAPIWsExceptionFilter()),
(0, decorators_1.ValidatorPipe)(validationPipeOptions),
__param(0, (0, common_1.Inject)(authServiceProviderName)),
__metadata("design:paramtypes", [Object, jwt_1.JwtService])
], AuthGateway);
return AuthGateway;
}
exports.createAuthGateway = createAuthGateway;
//# sourceMappingURL=auth.helper.js.map