1- import { ForbiddenException , Injectable , Logger , OnModuleInit , UnauthorizedException } from '@nestjs/common' ;
1+ import { ForbiddenException , Injectable , OnModuleInit , UnauthorizedException } from '@nestjs/common' ;
22import { AbstractService } from '~/_common/abstracts/abstract.service' ;
33import { ModuleRef } from '@nestjs/core' ;
44import { Redis } from 'ioredis' ;
@@ -11,6 +11,9 @@ import { AgentType } from '~/_common/types/agent.type';
1111import { omit } from 'radash' ;
1212import { JwtPayload } from 'jsonwebtoken' ;
1313import { JwtService } from '@nestjs/jwt' ;
14+ import { resolve } from 'path' ;
15+ import { existsSync , readFileSync , writeFileSync } from 'fs' ;
16+ import { ConsoleSession } from '~/_common/data/console-session' ;
1417
1518@Injectable ( )
1619export class AuthService extends AbstractService implements OnModuleInit {
@@ -32,8 +35,33 @@ export class AuthService extends AbstractService implements OnModuleInit {
3235 super ( ) ;
3336 }
3437
35- onModuleInit ( ) : any {
36- Logger . log ( 'Auth service initialized' ) ;
38+ public async onModuleInit ( ) : Promise < void > {
39+ if ( process . env . NODE_ENV !== 'production' ) {
40+ this . logger . warn ( 'DEV MODE ENABLED !' ) ;
41+ const devTokenPath = resolve ( process . cwd ( ) , this . DEV_TOKEN_PATH ) ;
42+ if ( existsSync ( devTokenPath ) ) {
43+ try {
44+ const data = JSON . parse ( readFileSync ( devTokenPath , 'utf-8' ) ) ;
45+ if ( data . access_token ) {
46+ this . logger . log ( `TOKEN ALREADY EXIST : <${ data . access_token } >` ) ;
47+ return ;
48+ }
49+ } catch ( e ) {
50+ this . logger . error ( `TOKEN FILE CORRUPTED ! REGENERATING...` ) ;
51+ }
52+ }
53+ const { access_token } = await this . createTokens ( new ConsoleSession ( ) , false , {
54+ expiresIn : '1y' ,
55+ } ) ;
56+ writeFileSync (
57+ devTokenPath ,
58+ JSON . stringify ( {
59+ access_token,
60+ } ) ,
61+ ) ;
62+
63+ this . logger . log ( `NEW TOKEN CREATED : <${ access_token } >` ) ;
64+ }
3765 }
3866
3967 public async authenticateWithLocal ( username : string , password : string ) : Promise < Agents | null > {
0 commit comments