1- import { Body , Controller , Get , Inject , Param , Post , Query , Req , Res , UseGuards , OnModuleInit , OnModuleDestroy } from '@nestjs/common' ;
2- import { filter , Subject , take } from 'rxjs' ;
3- import { v4 as uuidv4 } from 'uuid' ;
4- import { Response } from 'express' ;
1+ import { Body , Controller , Get , Inject , Param , Post , Query , Req , Res , Session , UseGuards } from '@nestjs/common' ;
52import {
63 CREDENTIAL_CREATOR_FACADE ,
74 ICredentialCreatorFacade
85} from '../../core/applications/credentials/facade/icredential.facade' ;
6+ import { Response } from 'express' ;
97import { AUTH_CONTROLLER_MAPPER , IcredentialsControllerMapper } from './mapper/icredentials.controller.mapper' ;
8+ import { v4 as uuidv4 } from 'uuid' ;
9+ import { filter , Subject , take } from 'rxjs' ;
1010import { SubjectData } from './isubject.data' ;
1111import { JwtGuard } from '../../guards/jwt.guard' ;
1212import { CredentialsGenerateEmailOtpApiRequestQuery } from "./requests/credentials.generate-email-otp.request.api" ;
@@ -21,10 +21,9 @@ import { ChainId } from '../../core/domain/entities/environment';
2121type Siwens = { address : string , ens : string , chainId : ChainId } ;
2222
2323@Controller ( 'credentials' )
24- export class CredentialsController implements OnModuleInit , OnModuleDestroy {
24+ export class CredentialsController {
2525
2626 private authSubjects : Map < string , Subject < SubjectData > > = new Map ( ) ;
27- private heartbeatInterval : NodeJS . Timer ;
2827
2928 constructor (
3029 @Inject ( CREDENTIAL_CREATOR_FACADE )
@@ -34,32 +33,6 @@ export class CredentialsController implements OnModuleInit, OnModuleDestroy {
3433 private readonly authControllerMapper : IcredentialsControllerMapper
3534 ) { }
3635
37- onModuleInit ( ) {
38- this . heartbeatInterval = setInterval ( ( ) => {
39- this . sendHeartbeats ( ) ;
40- } , 10000 ) ;
41- }
42-
43- onModuleDestroy ( ) {
44- if ( this . heartbeatInterval ) {
45- clearInterval ( this . heartbeatInterval ) ;
46- }
47- }
48-
49- private sendHeartbeats ( ) {
50- this . authSubjects . forEach ( ( subject , authId ) => {
51- try {
52- subject . next ( {
53- authId,
54- heartbeat : true ,
55- } ) ;
56- } catch ( error ) {
57- this . authSubjects . delete ( authId ) ;
58- throw Error ( `Failed to send heartbeat to ${ authId } : ${ error } ` ) ;
59- }
60- } ) ;
61- }
62-
6336 @UseGuards ( JwtGuard )
6437 @Get ( 'socials/:authName' )
6538 async getAuthUrl (
@@ -78,6 +51,7 @@ export class CredentialsController implements OnModuleInit, OnModuleDestroy {
7851 authId
7952 )
8053
54+
8155 res . writeHead ( 200 , {
8256 'Content-Type' : 'text/event-stream' ,
8357 'Cache-Control' : 'no-cache' ,
@@ -87,11 +61,11 @@ export class CredentialsController implements OnModuleInit, OnModuleDestroy {
8761 res . write ( `data: ${ JSON . stringify ( { redirectUrl } ) } \n\n` ) ;
8862
8963 subject . pipe (
90- filter ( data => data . authId === authId && ! data . heartbeat ) ,
64+ filter ( data => data . authId === authId ) ,
9165 take ( 1 )
9266 ) . subscribe (
9367 ( data ) => {
94- res . write ( `data: ${ JSON . stringify ( { result : data . result } ) } \n\n` ) ;
68+ res . write ( `data: ${ JSON . stringify ( { result :data . result } ) } \n\n` ) ;
9569 res . end ( ) ;
9670 this . authSubjects . delete ( authId ) ;
9771 } ,
@@ -100,7 +74,7 @@ export class CredentialsController implements OnModuleInit, OnModuleDestroy {
10074 res . end ( ) ;
10175 this . authSubjects . delete ( authId ) ;
10276 }
103- )
77+ ) ;
10478 }
10579
10680 @Get ( 'socials/:authName/callback' )
@@ -121,7 +95,6 @@ export class CredentialsController implements OnModuleInit, OnModuleDestroy {
12195 const subject = this . authSubjects . get ( authId ) ;
12296 subject ?. next ( {
12397 authId,
124- heartbeat : false ,
12598 result : {
12699 verifiableCredential,
127100 dataKey
@@ -205,4 +178,4 @@ export class CredentialsController implements OnModuleInit, OnModuleDestroy {
205178
206179 this . authSubjects . delete ( authId ) ;
207180 }
208- }
181+ }
0 commit comments