From b553ff0623b7b91df3b44478ce67a6d151ba872e Mon Sep 17 00:00:00 2001 From: Kallesh B S Date: Fri, 11 Jul 2025 11:30:58 +0530 Subject: [PATCH] auth and mail template fixed --- public/mail-templates/b.hbs | 2 +- src/auth/auth.controller.ts | 6 +-- src/auth/auth.service.ts | 58 +++++++++++++++++++++++++--- src/filters/auth-exception.filter.ts | 2 +- 4 files changed, 57 insertions(+), 11 deletions(-) diff --git a/public/mail-templates/b.hbs b/public/mail-templates/b.hbs index d20c3d3..49aaceb 100644 --- a/public/mail-templates/b.hbs +++ b/public/mail-templates/b.hbs @@ -4,7 +4,7 @@ {{!--

A Registration has been successfully initiated for the email: {{to}}

--}}

Please click below to complete your password reset:

- Register Now + Reset Now

Powered by

diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts index a4ee9c3..54f3a83 100644 --- a/src/auth/auth.controller.ts +++ b/src/auth/auth.controller.ts @@ -5,7 +5,6 @@ import { AuthLoginDTO, SendMailDTO } from './auth.dto'; import { Request, Response } from 'express'; import { LogoutInterceptor } from 'src/interceptors/logout.interceptor'; import { RegisterGuard } from 'src/guards/register.guard'; -import { UserMaintenanceService } from 'src/oracle/user-maintenance/user-maintenance.service'; import { RolesGuard } from 'src/guards/roles.guard'; import { Roles } from 'src/decorators/roles.decorator'; import { JwtAuthGuard } from 'src/guards/jwt-auth.guard'; @@ -15,18 +14,17 @@ import { JwtAuthGuard } from 'src/guards/jwt-auth.guard'; export class AuthController { constructor( private readonly authService: AuthService, - private readonly userMaintenanceService: UserMaintenanceService ) { } @Post('login') @HttpCode(200) async loginClient(@Body() body: AuthLoginDTO, @Res({ passthrough: true }) res: Response, @Req() req: Request) { - let k: any = await this.authService.loginUser(body.P_EMAILADDR, body.P_PASSWORD, req); + let k: any = await this.authService.loginUser(body.P_EMAILADDR.toLowerCase(), body.P_PASSWORD, req); if (k.access_token) { res.cookie('access_token', k.access_token, { httpOnly: true, - secure: true, + secure: true, sameSite: 'none', maxAge: 8 * 60 * 60 * 1000, }); diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index fbbcab0..7695cdb 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -267,11 +267,23 @@ export class AuthService { try { const userSession = await this.getUserSession(username) + if (access_token && refresh_token && userSession.length > 0) { + + + const getSub: any = jwt.decode(refresh_token, { complete: true }) + + const user: any = await this.getUserDetailsById(getSub?.payload.sub) + + if (user.email !== username) { + throw new UnauthorizedException("Authentication failed") + } + let tokens = await this.getTokenFromRefreshToken(req); const decoded = jwt.decode(access_token, { complete: true }); const email: any = (decoded && typeof decoded === 'object') ? (decoded as any).payload?.email : undefined; return { ...tokens, email } + } const response = await axios.post(this.TOKEN_URL, params, { @@ -325,6 +337,42 @@ export class AuthService { } } + async getUserDetailsById(id: string) { + try { + const adminAccessToken = await this.getAdminAccessToken(); + + const userResponse = await axios.get(`${this.USERS_URL}/${id}`, { + headers: { + Authorization: `Bearer ${adminAccessToken}` + }, + }); + + if (userResponse.status !== 200 || !Array.isArray(userResponse.data)) { + console.log("userResponse : ", userResponse); + console.log("userResponse status : ", userResponse.status); + + throw new UnauthorizedException("Authentication failed"); + } + + console.log("user response : ", userResponse); + + + return userResponse.data; + + } catch (error) { + console.log(error.message); + + console.log("Error in getUserDetailsById ..........."); + if (error instanceof UnauthorizedException) { + throw error; + } + else if (error instanceof BadRequestException) { + throw error; + } + throw new InternalServerErrorException(); + } + } + async getUserDetailsByEmail(email: string): Promise { try { const adminAccessToken = await this.getAdminAccessToken(); @@ -472,10 +520,10 @@ export class AuthService { } const userPayload = { - username: body.P_EMAILADDR, + username: body.P_EMAILADDR.toLowerCase(), // firstName:"A", // lastName:"B", - email: body.P_EMAILADDR, + email: body.P_EMAILADDR.toLowerCase(), emailVerified: true, enabled: true, attributes: { @@ -738,9 +786,9 @@ export class AuthService { template: 'b', context: { to: body.P_TO, - url: `${source === 'ua' ? `https://policy.alphaomegainfosys.com/register/${token}` - : source === 'sa' ? `https://sp.alphaomegainfosys.com/register/${token}` - : source === 'ca' ? `https://client.alphaomegainfosys.com/register/${token}` : ''}` + url: `${source === 'ua' ? `https://policy.alphaomegainfosys.com/forgot-password/${token}` + : source === 'sa' ? `https://sp.alphaomegainfosys.com/forgot-password/${token}` + : source === 'ca' ? `https://client.alphaomegainfosys.com/forgot-password/${token}` : ''}` } } diff --git a/src/filters/auth-exception.filter.ts b/src/filters/auth-exception.filter.ts index 5e8792b..28bc5e7 100644 --- a/src/filters/auth-exception.filter.ts +++ b/src/filters/auth-exception.filter.ts @@ -33,7 +33,7 @@ export class AuthExceptionFilter implements ExceptionFilter { // Send custom response return response.status(status).json({ statusCode: status, - message: 'Authentication failed. Tokens cleared.', + message: 'Authentication failed', }); }