UNPKG

@mercury-labs/auth

Version:

Mercury framework auth library. It supports local auth, jwt with both bearer token and cookie, basic auth.

106 lines 4.7 kB
"use strict"; 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 AuthModule_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.AuthModule = void 0; const hashing_1 = require("@mercury-labs/hashing"); const common_1 = require("@nestjs/common"); const core_1 = require("@nestjs/core"); const cqrs_1 = require("@nestjs/cqrs"); const jwt_1 = require("@nestjs/jwt"); const domain_1 = require("./domain"); const infrastructure_1 = require("./infrastructure"); const presentation_1 = require("./presentation"); const logout_controller_1 = require("./presentation/controllers/logout.controller"); let AuthModule = AuthModule_1 = class AuthModule { static forRootAsync(options) { var _a, _b; return { module: AuthModule_1, imports: [ ...(options.imports || []), cqrs_1.CqrsModule, infrastructure_1.AuthDefinitionsModule.forRootAsync(options.definitions), hashing_1.HashingModule.forRootAsync({ useFactory: (definitions) => { return { secretKey: definitions.hashingSecretKey, }; }, inject: [infrastructure_1.AUTH_DEFINITIONS_MODULE_OPTIONS], }), jwt_1.JwtModule.registerAsync({ useFactory: (definitions) => { return { secret: definitions.jwt.secret, signOptions: { expiresIn: definitions.jwt.expiresIn, }, }; }, inject: [infrastructure_1.AUTH_DEFINITIONS_MODULE_OPTIONS], }), ], providers: [ { provide: domain_1.AUTH_PASSWORD_HASHER, useFactory: ((_a = options.passwordHasher) === null || _a === void 0 ? void 0 : _a.useFactory) || (() => new domain_1.BcryptPasswordHasherService()), inject: (_b = options.passwordHasher) === null || _b === void 0 ? void 0 : _b.inject, }, { provide: domain_1.AuthRepository, useFactory: options.authRepository.useFactory, inject: options.authRepository.inject || [], }, { provide: core_1.APP_GUARD, useClass: domain_1.AuthGlobalGuard, }, domain_1.TokenService, domain_1.LocalLoginAction, domain_1.LocalStrategy, domain_1.JwtStrategy, domain_1.RefreshTokenStrategy, domain_1.AuthBasicGuard, domain_1.AuthRefreshTokenGuard, presentation_1.ClearAuthCookieInterceptor, presentation_1.CookieAuthInterceptor, presentation_1.BasicAuthMiddleware, ...(options.providers || []), ], controllers: [ presentation_1.LoginController, presentation_1.RefreshTokenController, logout_controller_1.LogoutController, presentation_1.ProfileController, ], exports: [ domain_1.AuthRepository, domain_1.TokenService, domain_1.AUTH_PASSWORD_HASHER, presentation_1.ClearAuthCookieInterceptor, presentation_1.CookieAuthInterceptor, domain_1.LocalStrategy, domain_1.JwtStrategy, domain_1.RefreshTokenStrategy, hashing_1.HashingModule, ], }; } configure(consumer) { consumer .apply(presentation_1.BasicAuthMiddleware) .forRoutes({ path: '*', method: common_1.RequestMethod.ALL }); } }; AuthModule = AuthModule_1 = __decorate([ (0, common_1.Module)({}) ], AuthModule); exports.AuthModule = AuthModule; //# sourceMappingURL=auth.module.js.map