auth for pg

This commit is contained in:
Kallesh B S 2025-09-20 13:20:19 +05:30
parent 19115d50fe
commit 22fe43380b
6 changed files with 117 additions and 17 deletions

View File

@ -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) {

View File

@ -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;

View File

@ -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) { }

View File

@ -1,8 +1,10 @@
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]
})

View File

@ -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');

View File

@ -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}`);