1- import { Body , Controller , Get , Headers , HttpCode , HttpStatus , Post } from '@nestjs/common' ;
1+ import { Body , Controller , Delete , Get , Headers , HttpCode , HttpStatus , Post } from '@nestjs/common' ;
22import { AuthService } from './auth.service' ;
33import AuthSignInReqDto from './dto/req/auth-sign-in-req.dto' ;
44import { IsPublic } from './decorator' ;
@@ -8,6 +8,8 @@ import AccessTokenResponse from './dto/res/access-token-res.dto';
88import TokenValidityResDto from './dto/res/token-validity-res.dto' ;
99import { ApiAppErrorResponse } from '../app.dto' ;
1010import { ConfigModule } from '../config/config.module' ;
11+ import AuthCreateMagicDto from './dto/req/auth-create-magic.dto' ;
12+ import AuthDeleteMagicDto from './dto/req/auth-delete-magic.dto' ;
1113
1214@Controller ( 'auth' )
1315@ApiTags ( 'Authentication' )
@@ -84,4 +86,39 @@ export class AuthController {
8486 operation : user ? ( user . type === 'ADMIN' ? 'administrate' : 'refund' ) : false ,
8587 } ;
8688 }
89+
90+ @HttpCode ( HttpStatus . OK )
91+ @IsPublic ( )
92+ @Post ( 'magic' )
93+ @ApiOperation ( {
94+ description : 'Generates a magic link for the user. This link should be sent to the user by email.' ,
95+ } )
96+ @ApiBody ( { type : AuthCreateMagicDto } )
97+ async generateMagicLink ( @Body ( ) dto : AuthCreateMagicDto , @Headers ( ) { 'X-Forwarded-for' : ip } ) : Promise < void > {
98+ const linkData = await this . authService . generateMagicLink ( dto . login , ip ) ;
99+ if ( ! linkData ) throw new AppException ( ERROR_CODE . INVALID_CREDENTIALS ) ;
100+ await this . authService . sendMagicLink ( dto . login , linkData . code , linkData . name ) ;
101+ }
102+
103+ @HttpCode ( HttpStatus . OK )
104+ @IsPublic ( )
105+ @Delete ( 'magic' )
106+ @ApiOperation ( {
107+ description : 'Consumes/Deletes the magic link.' ,
108+ } )
109+ @ApiBody ( { type : AuthDeleteMagicDto } )
110+ async consumeMagicLink ( @Body ( ) dto : AuthDeleteMagicDto ) : Promise < AccessTokenResponse > {
111+ const { token, id } = ( await this . authService . consumeMagicLink ( dto . spell ) ) ?? { } ;
112+ if ( ! token ) throw new AppException ( ERROR_CODE . INVALID_CREDENTIALS ) ;
113+ const user = id ? await this . authService . getUser ( id ) : undefined ;
114+ return {
115+ access_token : token ,
116+ currentBalance : user . balance ,
117+ firstName : user . firstName ,
118+ paymentMethodRegistered : user . iban ? user . ibanFoolproof : null ,
119+ processed : ! ! user . processed ,
120+ eligible : user . balance >= this . config . BALANCE_MIN_VALUE ,
121+ operation : user . type === 'ADMIN' ? 'administrate' : 'refund' ,
122+ } ;
123+ }
87124}
0 commit comments