import { FactoryProvider, Injectable, Logger } from '@nestjs/common'; import { JwtModuleOptions, JwtOptionsFactory } from '@nestjs/jwt'; import ms from 'ms'; import { ThrowIfFailed } from 'picsur-shared/dist/types'; import { SysPreferenceService } from '../../collections/preference-db/sys-preference-db.service'; @Injectable() export class JwtConfigService implements JwtOptionsFactory { private readonly logger = new Logger('JwtConfigService'); constructor(private readonly prefService: SysPreferenceService) { this.printDebug().catch(this.logger.error); } private async printDebug() { const secret = await this.getJwtSecret(); const expiresIn = await this.getJwtExpiresIn(); this.logger.verbose('JWT secret: ' + secret); this.logger.verbose('JWT expiresIn: ' + expiresIn); } public async getJwtSecret(): Promise { const secret = ThrowIfFailed( await this.prefService.getStringPreference('jwt_secret'), ); return secret; } public async getJwtExpiresIn(): Promise { const expiresIn = ThrowIfFailed( await this.prefService.getStringPreference('jwt_expires_in'), ); let milliseconds = ms(expiresIn); if (milliseconds === undefined) { milliseconds = 1000 * 60 * 60 * 24; // 1 day } return milliseconds / 1000; } public async createJwtOptions(): Promise { return { secret: await this.getJwtSecret(), signOptions: { expiresIn: await this.getJwtExpiresIn(), }, }; } } export const JwtSecretProvider: FactoryProvider> = { provide: 'JWT_SECRET', useFactory: async (jwtConfigService: JwtConfigService) => { return await jwtConfigService.getJwtSecret(); }, inject: [JwtConfigService], };