auth for pg
This commit is contained in:
parent
19115d50fe
commit
22fe43380b
@ -18,7 +18,7 @@ export class AuthController {
|
|||||||
|
|
||||||
@Post('1/login')
|
@Post('1/login')
|
||||||
@HttpCode(200)
|
@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);
|
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 }
|
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)
|
// @UseGuards(RegisterGuard)
|
||||||
@Post('1/register')
|
@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);
|
return this.authService.registerUser(body, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@UseGuards(RegisterGuard)
|
@UseGuards(RegisterGuard)
|
||||||
@Put('1/forgot-password')
|
@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);
|
return this.authService.forgotPassword(body, req);
|
||||||
}
|
}
|
||||||
|
|
||||||
@UseInterceptors(LogoutInterceptor)
|
@UseInterceptors(LogoutInterceptor)
|
||||||
@HttpCode(200)
|
@HttpCode(200)
|
||||||
@Post('1/logout')
|
@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;
|
const refreshToken = req.user?.refreshToken;
|
||||||
let rst: any = await this.authService.logoutUser(refreshToken);
|
let rst: any = await this.authService.logoutUser(refreshToken);
|
||||||
if (rst.statusCode === 200) {
|
if (rst.statusCode === 200) {
|
||||||
@ -75,7 +132,34 @@ export class AuthController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Get('1/refresh-tokens')
|
@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)
|
let k: any = await this.authService.getTokenFromRefreshToken(req)
|
||||||
|
|
||||||
if (k.access_token) {
|
if (k.access_token) {
|
||||||
@ -112,7 +196,7 @@ export class AuthController {
|
|||||||
|
|
||||||
|
|
||||||
@UseGuards(JwtAuthGuard, RolesGuard)
|
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||||
@Roles('sa')
|
@Roles('psa')
|
||||||
@Post('2/sendmail')
|
@Post('2/sendmail')
|
||||||
@HttpCode(200)
|
@HttpCode(200)
|
||||||
async sendMailb(@Body() body: SendMailDTO) {
|
async sendMailb(@Body() body: SendMailDTO) {
|
||||||
|
|||||||
@ -278,7 +278,7 @@ export class AuthService {
|
|||||||
let ROLE = "";
|
let ROLE = "";
|
||||||
|
|
||||||
if (Array.isArray(emailVerifyResponse) && !emailVerifyResponse[0].ERRORMESG) {
|
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 "USCIB": ROLE = dbtype === 1 ? 'ua' : dbtype === 2 ? 'pua' : ''; break;
|
||||||
case "SP": ROLE = dbtype === 1 ? 'sa' : dbtype === 2 ? 'psa' : ''; break;
|
case "SP": ROLE = dbtype === 1 ? 'sa' : dbtype === 2 ? 'psa' : ''; break;
|
||||||
case "CLIENT": ROLE = dbtype === 1 ? 'ca' : dbtype === 2 ? 'pca' : ''; break;
|
case "CLIENT": ROLE = dbtype === 1 ? 'ca' : dbtype === 2 ? 'pca' : ''; break;
|
||||||
@ -286,9 +286,9 @@ export class AuthService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const isValidCombo =
|
const isValidCombo =
|
||||||
(applicationName === 'policy' && ROLE === 'ua') ||
|
(applicationName === 'policy' && ROLE === (dbtype === 1 ? 'ua' : dbtype === 2 ? 'pua' : null)) ||
|
||||||
(applicationName === 'service-provider' && ROLE === 'sa') ||
|
(applicationName === 'service-provider' && ROLE === (dbtype === 1 ? 'sa' : dbtype === 2 ? 'psa' : null)) ||
|
||||||
(applicationName === 'client' && ROLE === 'ca');
|
(applicationName === 'client' && ROLE === (dbtype === 1 ? 'ca' : dbtype === 2 ? 'pca' : null));
|
||||||
|
|
||||||
if (!isValidCombo) {
|
if (!isValidCombo) {
|
||||||
throw new UnauthorizedException("Authentication failed")
|
throw new UnauthorizedException("Authentication failed")
|
||||||
@ -584,7 +584,7 @@ export class AuthService {
|
|||||||
|
|
||||||
|
|
||||||
if (Array.isArray(emailVerifyResponse) && !emailVerifyResponse[0].ERRORMESG) {
|
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 "USCIB": ROLE = dbtype === 1 ? 'ua' : dbtype === 2 ? 'pua' : ''; break;
|
||||||
case "SP": ROLE = dbtype === 1 ? 'sa' : dbtype === 2 ? 'psa' : ''; break;
|
case "SP": ROLE = dbtype === 1 ? 'sa' : dbtype === 2 ? 'psa' : ''; break;
|
||||||
case "CLIENT": ROLE = dbtype === 1 ? 'ca' : dbtype === 2 ? 'pca' : ''; break;
|
case "CLIENT": ROLE = dbtype === 1 ? 'ca' : dbtype === 2 ? 'pca' : ''; break;
|
||||||
|
|||||||
@ -7,8 +7,8 @@ import { RegionService } from './region.service';
|
|||||||
import { InsertRegionsDto, SPID_DTO, UpdateRegionDto } from 'src/dto/property.dto';
|
import { InsertRegionsDto, SPID_DTO, UpdateRegionDto } from 'src/dto/property.dto';
|
||||||
|
|
||||||
@ApiTags('Regions - PG')
|
@ApiTags('Regions - PG')
|
||||||
// @UseGuards(JwtAuthGuard, RolesGuard)
|
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||||
@Roles('sa', 'ua')
|
@Roles('psa', 'pua')
|
||||||
@Controller('2')
|
@Controller('2')
|
||||||
export class RegionController {
|
export class RegionController {
|
||||||
constructor(private readonly regionService: RegionService) { }
|
constructor(private readonly regionService: RegionService) { }
|
||||||
|
|||||||
@ -1,9 +1,11 @@
|
|||||||
import { Module } from '@nestjs/common';
|
import { forwardRef, Module } from '@nestjs/common';
|
||||||
import { RegionController } from './region.controller';
|
import { RegionController } from './region.controller';
|
||||||
import { RegionService } from './region.service';
|
import { RegionService } from './region.service';
|
||||||
|
import { AuthModule } from 'src/auth/auth.module';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
|
imports: [forwardRef(() => AuthModule)],
|
||||||
controllers: [RegionController],
|
controllers: [RegionController],
|
||||||
providers: [RegionService]
|
providers: [RegionService]
|
||||||
})
|
})
|
||||||
export class RegionModule {}
|
export class RegionModule { }
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
import { forwardRef, Inject, Injectable, Logger } from '@nestjs/common';
|
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 { 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 { PoolClient } from 'pg';
|
||||||
import { PgDBService } from 'src/db/db.service';
|
import { PgDBService } from 'src/db/db.service';
|
||||||
@ -430,7 +430,13 @@ export class UserMaintenanceService {
|
|||||||
|
|
||||||
checkPgUserDefinedErrors(fetchResult);
|
checkPgUserDefinedErrors(fetchResult);
|
||||||
|
|
||||||
return fetchResult?.rows ? fetchResult?.rows : []
|
if (fetchResult?.rows.length > 0) {
|
||||||
|
return normalizeKeysToUpperCase(fetchResult?.rows[0])
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (client) await client.query('ROLLBACK');
|
if (client) await client.query('ROLLBACK');
|
||||||
|
|||||||
@ -28,6 +28,14 @@ export enum ResponseStatus {
|
|||||||
transmitted = 'Transmitted Successfully'
|
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 => {
|
export const handleError = (error: any, context: string = 'UnknownService'): never => {
|
||||||
if (error instanceof BadRequestException || error instanceof BR) {
|
if (error instanceof BadRequestException || error instanceof BR) {
|
||||||
logger.warn(`[${context}] ${error.message}`);
|
logger.warn(`[${context}] ${error.message}`);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user