UNPKG

@wristband/nestjs-auth

Version:

SDK for integrating your NestJS application with Wristband. Handles user authentication, session management, and token management.

99 lines (98 loc) 4.4 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 WristbandExpressAuthModule_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.WristbandExpressAuthModule = void 0; const common_1 = require("@nestjs/common"); const auth_service_1 = require("./auth.service"); const constants_1 = require("../constants"); /** * The WristbandExpressAuthModule is a dynamic NestJS module that integrates the Wristband Authentication Service * for NestJS/Express-based applications. It allows for the configuration and injection of the * `WristbandExpressAuthService`, enabling authentication functionality within the application. * * Usage: * - Call `forRootAsync` with configuration options and a token name to set up the module. * - The module exports the `WristbandExpressAuthService` with the specified token, making it available across the app. * * Example: * * ```typescript * import { ConfigModule } from '@nestjs/config'; * import { WristbandExpressAuthModule } from '@wristband/nestjs-auth'; * * WristbandExpressAuthModule.forRootAsync({ * imports: [ConfigModule], * useFactory: (configService: ConfigService) => ({ * clientId: configService.get('WRISTBAND_CLIENT_ID'), * clientSecret: configService.get('WRISTBAND_CLIENT_SECRET'), * // ... other config * }), * inject: [ConfigService], * }, 'MyWristbandAuth'); * ``` * * For static configuration: * * ```typescript * import { ConfigModule } from '@nestjs/config'; * import { WristbandExpressAuthModule } from '@wristband/nestjs-auth'; * * WristbandExpressAuthModule.forRootAsync({ * useFactory: () => ({ * clientId: 'your-client-id', * clientSecret: 'your-client-secret', * // ... other config * }), * }, 'MyWristbandAuth'); * ``` * * This module is designed to be globally available, ensuring the `WristbandExpressAuthService` can be easily injected * and used across different modules in the application. Multiple instances of this SDK can be injected into the same * application, if required. */ let WristbandExpressAuthModule = WristbandExpressAuthModule_1 = class WristbandExpressAuthModule { /** * Configures and initializes the WristbandExpressAuthModule with async configuration. * * @param {WristbandAuthAsyncOptions} options - Configuration options including useFactory, inject, and imports * @param {string} token - Token name used to identify the service instance. Required for multi-instance support. * @returns {DynamicModule} - A NestJS DynamicModule that provides and exports the `WristbandExpressAuthService` * with the specified token. */ static forRootAsync(options, token = constants_1.DEFAULT_AUTH_TOKEN) { // Create a unique config token for this instance const configToken = `${token}_CONFIG`; return { module: WristbandExpressAuthModule_1, global: true, imports: options.imports || [], providers: [ // Provide the config with instance-specific token { provide: configToken, useFactory: options.useFactory, inject: options.inject || [], }, // Provide the service with the custom token { provide: token, useFactory: (config) => { return new auth_service_1.WristbandExpressAuthService(config); }, inject: [configToken], }, ], exports: [token], }; } }; exports.WristbandExpressAuthModule = WristbandExpressAuthModule; exports.WristbandExpressAuthModule = WristbandExpressAuthModule = WristbandExpressAuthModule_1 = __decorate([ (0, common_1.Module)({}) ], WristbandExpressAuthModule);