From 16881b9c0a792e505b3ac13b7aca1380fb7311b5 Mon Sep 17 00:00:00 2001 From: Kallesh B S Date: Tue, 30 Sep 2025 11:15:23 +0530 Subject: [PATCH] auth bug fix 12 --- src/auth/auth.controller.ts | 113 ++++++++++++++---- .../carnet-application.service.ts | 16 +-- 2 files changed, 100 insertions(+), 29 deletions(-) diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts index 182c034..e61d627 100644 --- a/src/auth/auth.controller.ts +++ b/src/auth/auth.controller.ts @@ -48,21 +48,47 @@ export class AuthController { let k: any = await this.authService.loginUser(body.P_EMAILADDR.toLowerCase(), body.P_PASSWORD, body.P_APPLICATIONNAME, req, 2); - if (k.access_token) { - res.cookie('access_token', k.access_token, { - httpOnly: true, - secure: true, - sameSite: 'none' - }); + // if (k.access_token) { + // res.cookie('access_token', k.access_token, { + // httpOnly: true, + // secure: true, + // sameSite: 'none' + // }); + // } + + // if (k.refresh_token) { + // res.cookie('refresh_token', k.refresh_token, { + // httpOnly: true, + // secure: true, + // sameSite: 'none' + // }); + // } + + const hostname = req.hostname; + + // Check if we're on localhost (no domain scoping in that case) + const isLocalhost = hostname === 'localhost' || hostname === '127.0.0.1'; + + // Optional: you can also support ports, like 'localhost:3000' + const isDevEnv = isLocalhost || hostname.startsWith('localhost') || hostname.startsWith('127.0.0.1'); + + const cookieOptions: any = { + httpOnly: true, + secure: true, + sameSite: 'none' + }; + + if (!isDevEnv) { + cookieOptions.domain = hostname; // Set domain only for subdomains } - if (k.refresh_token) { - res.cookie('refresh_token', k.refresh_token, { - httpOnly: true, - secure: true, - sameSite: 'none' - }); - } + res.cookie('access_token', k.access_token, { + ...cookieOptions + }); + + res.cookie('refresh_token', k.refresh_token, { + ...cookieOptions + }); return { statusCode: 200, message: "Logged-In Successfully", email: k.email } } @@ -118,16 +144,61 @@ export class AuthController { const refreshToken = req.user?.refreshToken; let rst: any = await this.authService.logoutUser(refreshToken); if (rst.statusCode === 200) { - res.clearCookie('access_token', { + // res.clearCookie('access_token', { + // httpOnly: true, + // secure: true, + // sameSite: 'none' + // }); + // res.clearCookie('refresh_token', { + // httpOnly: true, + // secure: true, + // sameSite: 'none' + // }); + + const hostname = req.hostname; + + // Check if we're on localhost (no domain scoping in that case) + const isLocalhost = hostname === 'localhost' || hostname === '127.0.0.1'; + + // Optional: you can also support ports, like 'localhost:3000' + const isDevEnv = isLocalhost || hostname.startsWith('localhost') || hostname.startsWith('127.0.0.1'); + + const cookieOptions: any = { httpOnly: true, secure: true, sameSite: 'none' - }); - res.clearCookie('refresh_token', { - httpOnly: true, - secure: true, - sameSite: 'none' - }); + }; + + if (!isDevEnv) { + cookieOptions.domain = hostname; // Set domain only for subdomains + } + + if (!isDevEnv) { + res.clearCookie('access_token', { + httpOnly: true, + secure: true, + sameSite: 'none', + domain: hostname + }); + res.clearCookie('refresh_token', { + httpOnly: true, + secure: true, + sameSite: 'none', + domain: hostname + }); + } else { + res.clearCookie('access_token', { + httpOnly: true, + secure: true, + sameSite: 'none' + }); + res.clearCookie('refresh_token', { + httpOnly: true, + secure: true, + sameSite: 'none' + }); + } + } res.json({ ...rst }) } @@ -204,7 +275,7 @@ export class AuthController { @UseGuards(JwtAuthGuard, RolesGuard) - @Roles('pua','psa','pca') + @Roles('pua', 'psa', 'pca') @Post('2/sendmail') @HttpCode(200) async sendMailb(@Body() body: SendMailDTO, @Req() req: Request) { diff --git a/src/pg/carnet-application/carnet-application.service.ts b/src/pg/carnet-application/carnet-application.service.ts index eaf78a6..8ed471e 100644 --- a/src/pg/carnet-application/carnet-application.service.ts +++ b/src/pg/carnet-application/carnet-application.service.ts @@ -1245,7 +1245,7 @@ export class CarnetApplicationService { description: x.ITEMDESCRIPTION, noOfPieces: x.NOOFPIECES, weight: x.WEIGHT, - itemValue: x.ITEMVALUE, + itemValue: isNaN(Number(x.ITEMVALUE)) ? 0 : Number(x.ITEMVALUE), countryOfOrigin: x.GOODSORIGINCOUNTRY }; }); @@ -1256,15 +1256,15 @@ export class CarnetApplicationService { ISSUEDATE: fres[0].ISSUEDATE ?? "", EXPDATE: fres[0].EXPDATE ?? "", AUTHREP: fres[0].AUTHREP ?? "", - NOOFUSSETS: fres[0].NOOFUSSETS ?? 0, - NOOFFOREIGNSETS: fres[0].NOOFFOREIGNSETS ?? 0, - NOOFTRANSITSETS: fres[0].NOOFTRANSITSETS ?? 0, + NOOFUSSETS: isNaN(Number(fres[0].NOOFUSSETS)) ? 0 : Number(fres[0].NOOFUSSETS), + NOOFFOREIGNSETS: isNaN(Number(fres[0].NOOFFOREIGNSETS)) ? 0 : Number(fres[0].NOOFFOREIGNSETS), + NOOFTRANSITSETS: isNaN(Number(fres[0].NOOFTRANSITSETS)) ? 0 : Number(fres[0].NOOFTRANSITSETS), PRINTGOODSTOBEEXPORTED: fres[0].PRINTGOODSTOBEEXPORTED ?? "", HOLDERDATATOPRINT: fres[0].HOLDERDATATOPRINT ?? "", - CONTINUATIONSHEETS: fres[0].CONTINUATIONSHEETS ?? 0, - NEXTUSCFNO: fres[0].NEXTUSCFNO ?? 0, - NEXTFOREIGNCFNO: fres[0].NEXTFOREIGNCFNO ?? 0, - NEXTTRANSITCFNO: fres[0].NEXTTRANSITCFNO ?? 0, + CONTINUATIONSHEETS: isNaN(Number(fres[0].CONTINUATIONSHEETS)) ? 0 : Number(fres[0].CONTINUATIONSHEETS), + NEXTUSCFNO: isNaN(Number(fres[0].NEXTUSCFNO)) ? 0 : Number(fres[0].NEXTUSCFNO), + NEXTFOREIGNCFNO: isNaN(fres[0].NEXTFOREIGNCFNO) ? 0 : Number(fres[0].NEXTFOREIGNCFNO), + NEXTTRANSITCFNO: isNaN(Number(fres[0].NEXTTRANSITCFNO)) ? 0 : Number(fres[0].NEXTTRANSITCFNO), contPageNo: 0 }