| 1 | import type { DynamicModule, FactoryProvider, ModuleMetadata } from '@nestjs/common' |
| 2 | import { Module } from '@nestjs/common' |
| 3 | import { APP_GUARD } from '@nestjs/core' |
| 4 | import { JwtModule } from '@nestjs/jwt' |
| 5 | import { AITOEARN_AUTH_OPTIONS, AitoearnAuthOptions } from './aitoearn-auth.config' |
| 6 | import { AitoearnAuthGuard } from './aitoearn-auth.guard' |
| 7 | |
| 8 | export interface AitoearnAuthModuleAsyncOptions<TTokenInfo = unknown> { |
| 9 | imports?: ModuleMetadata['imports'] |
| 10 | inject?: FactoryProvider['inject'] |
| 11 | useFactory: (...args: any[]) => AitoearnAuthOptions<TTokenInfo> | Promise<AitoearnAuthOptions<TTokenInfo>> |
| 12 | } |
| 13 | |
| 14 | @Module({}) |
| 15 | export class AitoearnAuthModule { |
| 16 | static forRootAsync<TTokenInfo = unknown>(options: AitoearnAuthModuleAsyncOptions<TTokenInfo>): DynamicModule { |
| 17 | return { |
| 18 | global: true, |
| 19 | module: AitoearnAuthModule, |
| 20 | imports: [ |
| 21 | ...(options.imports ?? []), |
| 22 | JwtModule.register({}), |
| 23 | ], |
| 24 | providers: [ |
| 25 | { |
| 26 | provide: AITOEARN_AUTH_OPTIONS, |
| 27 | useFactory: options.useFactory, |
| 28 | inject: options.inject ?? [], |
| 29 | }, |
| 30 | { |
| 31 | provide: APP_GUARD, |
| 32 | useClass: AitoearnAuthGuard, |
| 33 | }, |
| 34 | ], |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 |