@mercury-labs/nest-auth
Version:
Mercury framework auth library. It supports local auth, jwt with both bearer token and cookie, basic auth.
155 lines • 7.68 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 AuthModule_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AuthModule = void 0;
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 handlers_1 = require("./application/actions/handlers");
const handlers_2 = require("./application/queries/handlers");
const domain_1 = require("./domain");
const hashing_module_1 = require("./hashing.module");
const infrastructure_1 = require("./infrastructure");
const presentation_1 = require("./presentation");
let AuthModule = exports.AuthModule = AuthModule_1 = class AuthModule {
static forRootAsync(options) {
var _a, _b, _c, _d;
const useLocalAuth = typeof options.useLocalAuth === 'undefined' || options.useLocalAuth;
return {
module: AuthModule_1,
imports: [
...(options.imports || []),
cqrs_1.CqrsModule,
infrastructure_1.AuthDefinitionsModule.forRootAsync(options.definitions),
hashing_module_1.HashingModule.forRootAsync({
useFactory: (definitions) => {
return {
secretKey: definitions.hashingSecretKey,
enabled: definitions.enableHashingToken,
};
},
inject: [infrastructure_1.AUTH_DEFINITIONS_MODULE_OPTIONS],
}),
jwt_1.JwtModule.registerAsync({
useFactory: (definitions) => {
var _a, _b;
return {
secret: (_a = definitions.jwt) === null || _a === void 0 ? void 0 : _a.secret,
signOptions: {
expiresIn: (_b = definitions.jwt) === null || _b === void 0 ? void 0 : _b.expiresIn,
},
};
},
inject: [infrastructure_1.AUTH_DEFINITIONS_MODULE_OPTIONS],
}),
],
providers: [
...(options.otherProviders || []),
{
provide: domain_1.AUTH_PASSWORD_HASHER,
useFactory: ((_a = options.passwordHasher) === null || _a === void 0 ? void 0 : _a.useFactory) ||
(() => new domain_1.Pbkdf2PasswordHasherService()),
inject: (_b = options.passwordHasher) === null || _b === void 0 ? void 0 : _b.inject,
},
{
provide: domain_1.AuthRepository,
useFactory: ((_c = options.authRepository) === null || _c === void 0 ? void 0 : _c.useFactory)
? options.authRepository.useFactory
: () => { },
inject: ((_d = options.authRepository) === null || _d === void 0 ? void 0 : _d.inject) || [],
},
domain_1.TokenService,
handlers_1.LoginCommandHandler,
handlers_2.GetCurrentUserByAccessTokenQueryHandler,
handlers_2.GetCurrentUserByApiKeyQueryHandler,
handlers_2.GetCurrentUserByRefreshTokenQueryHandler,
handlers_1.UserLogoutCommandHandler,
domain_1.GetUserByJwtTokenAction,
domain_1.GetUserByApiKeyAction,
domain_1.GetUserByRefreshTokenAction,
domain_1.LocalLoginAction,
domain_1.ParseJwtTokenAction,
domain_1.LogoutAction,
domain_1.LocalStrategy,
domain_1.ApiKeyStrategy,
domain_1.JwtStrategy,
domain_1.RefreshTokenStrategy,
presentation_1.UserLogoutInterceptor,
presentation_1.CookieAuthInterceptor,
domain_1.AuthBasicGuard,
domain_1.AuthApiKeyGuard,
domain_1.AuthJwtGuard,
domain_1.AuthRefreshTokenGuard,
domain_1.GraphqlAuthJwtGuard,
domain_1.GraphqlAuthRefreshTokenGuard,
domain_1.GraphqlAuthApiKeyGuard,
presentation_1.BasicAuthMiddleware,
{
provide: core_1.APP_GUARD,
useFactory: (reflector, authJwtGuard, basicAuthGuard, authApiKeyGuard, refreshTokenGuard, graphqlAuthJwtGuard, graphqlAuthRefreshTokenGuard, options) => {
return new domain_1.AuthGlobalGuard(reflector, authJwtGuard, basicAuthGuard, authApiKeyGuard, refreshTokenGuard, graphqlAuthJwtGuard, graphqlAuthRefreshTokenGuard, options);
},
inject: [
core_1.Reflector,
domain_1.AuthJwtGuard,
domain_1.AuthBasicGuard,
domain_1.AuthApiKeyGuard,
domain_1.AuthRefreshTokenGuard,
domain_1.GraphqlAuthJwtGuard,
domain_1.GraphqlAuthRefreshTokenGuard,
infrastructure_1.AUTH_DEFINITIONS_MODULE_OPTIONS,
],
},
...(options.providers || []),
],
controllers: useLocalAuth
? [
presentation_1.LoginController,
presentation_1.RefreshTokenController,
presentation_1.LogoutController,
presentation_1.ProfileController,
presentation_1.ApiKeyController,
]
: [],
exports: [
domain_1.AuthRepository,
domain_1.TokenService,
domain_1.GetUserByJwtTokenAction,
domain_1.GetUserByApiKeyAction,
domain_1.GetUserByRefreshTokenAction,
domain_1.LocalLoginAction,
domain_1.ParseJwtTokenAction,
domain_1.LogoutAction,
domain_1.AUTH_PASSWORD_HASHER,
presentation_1.UserLogoutInterceptor,
presentation_1.CookieAuthInterceptor,
domain_1.LocalStrategy,
domain_1.JwtStrategy,
domain_1.RefreshTokenStrategy,
handlers_1.LoginCommandHandler,
handlers_1.UserLogoutCommandHandler,
handlers_2.GetCurrentUserByAccessTokenQueryHandler,
handlers_2.GetCurrentUserByRefreshTokenQueryHandler,
hashing_module_1.HashingModule,
...(options.otherProviders || []),
],
global: options.global || false,
};
}
configure(consumer) {
consumer
.apply(presentation_1.BasicAuthMiddleware)
.forRoutes({ path: '*', method: common_1.RequestMethod.ALL });
}
};
exports.AuthModule = AuthModule = AuthModule_1 = __decorate([
(0, common_1.Module)({})
], AuthModule);
//# sourceMappingURL=auth.module.js.map
;