diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts index 9648bc7..d939742 100644 --- a/src/auth/auth.controller.ts +++ b/src/auth/auth.controller.ts @@ -18,7 +18,7 @@ export class AuthController { @Post('1/login') @HttpCode(200) - async loginClient(@Body() body: AuthLoginOnlyDTO, @Res({ passthrough: true }) res: Response, @Req() req: Request) { + async loginClienta(@Body() body: AuthLoginOnlyDTO, @Res({ passthrough: true }) res: Response, @Req() req: Request) { let k: any = await this.authService.loginUser(body.P_EMAILADDR.toLowerCase(), body.P_PASSWORD, body.P_APPLICATIONNAME, req, 1); @@ -41,22 +41,79 @@ export class AuthController { return { statusCode: 200, message: "Logged-In Successfully", email: k.email } } + @Post('2/login') + @HttpCode(200) + async loginClientb(@Body() body: AuthLoginOnlyDTO, @Res({ passthrough: true }) res: Response, @Req() req: Request) { + + 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.refresh_token) { + res.cookie('refresh_token', k.refresh_token, { + httpOnly: true, + secure: true, + sameSite: 'none' + }); + } + + return { statusCode: 200, message: "Logged-In Successfully", email: k.email } + } + // @UseGuards(RegisterGuard) @Post('1/register') - async register(@Body() body: AuthLoginDTO) { + async registera(@Body() body: AuthLoginDTO) { + return this.authService.registerUser(body, 1); + } + + @Post('2/register') + async registerb(@Body() body: AuthLoginDTO) { return this.authService.registerUser(body, 1); } @UseGuards(RegisterGuard) @Put('1/forgot-password') - async forgotPassword(@Body() body: AuthLoginDTO, @Req() req: Request) { + async forgotPassworda(@Body() body: AuthLoginDTO, @Req() req: Request) { + return this.authService.forgotPassword(body, req); + } + + @UseGuards(RegisterGuard) + @Put('2/forgot-password') + async forgotPasswordb(@Body() body: AuthLoginDTO, @Req() req: Request) { return this.authService.forgotPassword(body, req); } @UseInterceptors(LogoutInterceptor) @HttpCode(200) @Post('1/logout') - async logout(@Req() req: any, @Res() res: Response) { + async logouta(@Req() req: any, @Res() res: Response) { + const refreshToken = req.user?.refreshToken; + let rst: any = await this.authService.logoutUser(refreshToken); + if (rst.statusCode === 200) { + res.clearCookie('access_token', { + httpOnly: true, + secure: true, + sameSite: 'none' + }); + res.clearCookie('refresh_token', { + httpOnly: true, + secure: true, + sameSite: 'none' + }); + } + res.json({ ...rst }) + } + + @UseInterceptors(LogoutInterceptor) + @HttpCode(200) + @Post('2/logout') + async logoutb(@Req() req: any, @Res() res: Response) { const refreshToken = req.user?.refreshToken; let rst: any = await this.authService.logoutUser(refreshToken); if (rst.statusCode === 200) { @@ -75,7 +132,34 @@ export class AuthController { } @Get('1/refresh-tokens') - async getTokenFromRefreshToken(@Req() req: Request, @Res({ passthrough: true }) res: Response) { + async getTokenFromRefreshTokena(@Req() req: Request, @Res({ passthrough: true }) res: Response) { + let k: any = await this.authService.getTokenFromRefreshToken(req) + + if (k.access_token) { + res.cookie('access_token', k.access_token, { + httpOnly: true, + secure: true, + sameSite: 'none', + maxAge: 8 * 60 * 60 * 1000, + path: '/' + }); + } + + if (k.refresh_token) { + res.cookie('refresh_token', k.refresh_token, { + httpOnly: true, + secure: true, + sameSite: 'none', + maxAge: 8 * 60 * 60 * 1000, + path: '/' + }); + } + + return { statusCode: 200, message: "Tokens refreshed successfully" } + } + + @Get('2/refresh-tokens') + async getTokenFromRefreshTokenb(@Req() req: Request, @Res({ passthrough: true }) res: Response) { let k: any = await this.authService.getTokenFromRefreshToken(req) if (k.access_token) { @@ -112,7 +196,7 @@ export class AuthController { @UseGuards(JwtAuthGuard, RolesGuard) - @Roles('sa') + @Roles('psa') @Post('2/sendmail') @HttpCode(200) async sendMailb(@Body() body: SendMailDTO) { diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index e0d99b0..e9b5aa8 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -278,7 +278,7 @@ export class AuthService { let ROLE = ""; if (Array.isArray(emailVerifyResponse) && !emailVerifyResponse[0].ERRORMESG) { - switch (emailVerifyResponse[0].SOURCE) { + switch ((emailVerifyResponse[0].SOURCE + "".toUpperCase())) { case "USCIB": ROLE = dbtype === 1 ? 'ua' : dbtype === 2 ? 'pua' : ''; break; case "SP": ROLE = dbtype === 1 ? 'sa' : dbtype === 2 ? 'psa' : ''; break; case "CLIENT": ROLE = dbtype === 1 ? 'ca' : dbtype === 2 ? 'pca' : ''; break; @@ -286,9 +286,9 @@ export class AuthService { } const isValidCombo = - (applicationName === 'policy' && ROLE === 'ua') || - (applicationName === 'service-provider' && ROLE === 'sa') || - (applicationName === 'client' && ROLE === 'ca'); + (applicationName === 'policy' && ROLE === (dbtype === 1 ? 'ua' : dbtype === 2 ? 'pua' : null)) || + (applicationName === 'service-provider' && ROLE === (dbtype === 1 ? 'sa' : dbtype === 2 ? 'psa' : null)) || + (applicationName === 'client' && ROLE === (dbtype === 1 ? 'ca' : dbtype === 2 ? 'pca' : null)); if (!isValidCombo) { throw new UnauthorizedException("Authentication failed") @@ -584,7 +584,7 @@ export class AuthService { if (Array.isArray(emailVerifyResponse) && !emailVerifyResponse[0].ERRORMESG) { - switch (emailVerifyResponse[0].SOURCE) { + switch ((emailVerifyResponse[0].SOURCE + "".toUpperCase())) { case "USCIB": ROLE = dbtype === 1 ? 'ua' : dbtype === 2 ? 'pua' : ''; break; case "SP": ROLE = dbtype === 1 ? 'sa' : dbtype === 2 ? 'psa' : ''; break; case "CLIENT": ROLE = dbtype === 1 ? 'ca' : dbtype === 2 ? 'pca' : ''; break; diff --git a/src/pg/uscib-managed-sp/region/region.controller.ts b/src/pg/uscib-managed-sp/region/region.controller.ts index 8254a23..4c624e3 100644 --- a/src/pg/uscib-managed-sp/region/region.controller.ts +++ b/src/pg/uscib-managed-sp/region/region.controller.ts @@ -7,8 +7,8 @@ import { RegionService } from './region.service'; import { InsertRegionsDto, SPID_DTO, UpdateRegionDto } from 'src/dto/property.dto'; @ApiTags('Regions - PG') -// @UseGuards(JwtAuthGuard, RolesGuard) -@Roles('sa', 'ua') +@UseGuards(JwtAuthGuard, RolesGuard) +@Roles('psa', 'pua') @Controller('2') export class RegionController { constructor(private readonly regionService: RegionService) { } diff --git a/src/pg/uscib-managed-sp/region/region.module.ts b/src/pg/uscib-managed-sp/region/region.module.ts index d776aee..2db7b37 100644 --- a/src/pg/uscib-managed-sp/region/region.module.ts +++ b/src/pg/uscib-managed-sp/region/region.module.ts @@ -1,9 +1,11 @@ -import { Module } from '@nestjs/common'; +import { forwardRef, Module } from '@nestjs/common'; import { RegionController } from './region.controller'; import { RegionService } from './region.service'; +import { AuthModule } from 'src/auth/auth.module'; @Module({ + imports: [forwardRef(() => AuthModule)], controllers: [RegionController], providers: [RegionService] }) -export class RegionModule {} +export class RegionModule { } diff --git a/src/pg/user-maintenance/user-maintenance.service.ts b/src/pg/user-maintenance/user-maintenance.service.ts index abd3921..6dcae19 100644 --- a/src/pg/user-maintenance/user-maintenance.service.ts +++ b/src/pg/user-maintenance/user-maintenance.service.ts @@ -1,6 +1,6 @@ import { forwardRef, Inject, Injectable, Logger } from '@nestjs/common'; import { CreateClientLoginsDTO, CreateSPLoginsDTO, CreateUSCIBLoginsDTO, EMAIL_DTO, SPID_CLIENTID_DTO, SPID_DTO, SPID_EMAIL_DTO, USERID_DTO } from 'src/dto/property.dto'; -import { checkPgUserDefinedErrors, handlePgError, releasePgClient, ResponseStatus } from 'src/utils/helper'; +import { checkPgUserDefinedErrors, handlePgError, normalizeKeysToUpperCase, releasePgClient, ResponseStatus } from 'src/utils/helper'; import { PoolClient } from 'pg'; import { PgDBService } from 'src/db/db.service'; @@ -430,7 +430,13 @@ export class UserMaintenanceService { checkPgUserDefinedErrors(fetchResult); - return fetchResult?.rows ? fetchResult?.rows : [] + if (fetchResult?.rows.length > 0) { + return normalizeKeysToUpperCase(fetchResult?.rows[0]) + } + else { + return []; + } + } catch (error) { if (client) await client.query('ROLLBACK'); diff --git a/src/utils/helper.ts b/src/utils/helper.ts index 6a600e7..f1f7256 100644 --- a/src/utils/helper.ts +++ b/src/utils/helper.ts @@ -28,6 +28,14 @@ export enum ResponseStatus { transmitted = 'Transmitted Successfully' } +export function normalizeKeysToUpperCase(obj) { + return Object.keys(obj).reduce((acc, key) => { + acc[key.toUpperCase()] = obj[key]; + return acc; + }, {}); +} + + export const handleError = (error: any, context: string = 'UnknownService'): never => { if (error instanceof BadRequestException || error instanceof BR) { logger.warn(`[${context}] ${error.message}`);