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 NowPowered 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