From 7e82150b240fe9bc2b8a039f423e4e5459461e1c Mon Sep 17 00:00:00 2001 From: Kallesh B S Date: Tue, 11 Mar 2025 10:52:58 +0530 Subject: [PATCH] auth api --- src/app.module.ts | 3 +- src/auth/auth.controller.ts | 16 +++++++ src/auth/auth.dto.ts | 13 ++++++ src/auth/auth.module.ts | 12 +++++ src/auth/auth.service.ts | 91 +++++++++++++++++++++++++++++++++++++ 5 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 src/auth/auth.controller.ts create mode 100644 src/auth/auth.dto.ts create mode 100644 src/auth/auth.module.ts create mode 100644 src/auth/auth.service.ts diff --git a/src/app.module.ts b/src/app.module.ts index 33e9de5..8be9222 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,9 +1,10 @@ import { Module } from '@nestjs/common'; import { DbModule } from './db/db.module'; import { OracleModule } from './oracle/oracle.module'; +import { AuthModule } from './auth/auth.module'; @Module({ - imports: [DbModule, OracleModule], + imports: [DbModule, OracleModule, AuthModule], controllers: [], providers: [], }) diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts new file mode 100644 index 0000000..63a6cca --- /dev/null +++ b/src/auth/auth.controller.ts @@ -0,0 +1,16 @@ +import { Body, Controller, Post } from '@nestjs/common'; +import { AuthService } from './auth.service'; +import { ApiTags } from '@nestjs/swagger'; +import { AuthLoginDTO } from './auth.dto'; + +@Controller('auth') +export class AuthController { + + constructor(private readonly authService:AuthService){} + + @ApiTags('Auth') + @Post('/login') + login(@Body() body:AuthLoginDTO){ + return this.authService.login(body); + } +} diff --git a/src/auth/auth.dto.ts b/src/auth/auth.dto.ts new file mode 100644 index 0000000..5b1267f --- /dev/null +++ b/src/auth/auth.dto.ts @@ -0,0 +1,13 @@ +import { ApiProperty } from "@nestjs/swagger"; +import { IsString } from "class-validator"; + +export class AuthLoginDTO{ + + @ApiProperty({required:true}) + @IsString() + p_emailaddr:string; + + @ApiProperty({required:true}) + @IsString() + p_password:string; +} \ No newline at end of file diff --git a/src/auth/auth.module.ts b/src/auth/auth.module.ts new file mode 100644 index 0000000..9ab79c8 --- /dev/null +++ b/src/auth/auth.module.ts @@ -0,0 +1,12 @@ +import { Module } from '@nestjs/common'; +import { AuthService } from './auth.service'; +import { AuthController } from './auth.controller'; +import { OracleDBService } from 'src/db/db.service'; +import { DbModule } from 'src/db/db.module'; + +@Module({ + imports:[DbModule], + providers: [AuthService], + controllers: [AuthController] +}) +export class AuthModule {} diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts new file mode 100644 index 0000000..5958124 --- /dev/null +++ b/src/auth/auth.service.ts @@ -0,0 +1,91 @@ +import { Injectable } from '@nestjs/common'; +import { OracleDBService } from 'src/db/db.service'; +import { AuthLoginDTO } from './auth.dto'; +import * as oracledb from 'oracledb' + +@Injectable() +export class AuthService { + + constructor(private readonly oracleDBService: OracleDBService) { } + + async login(body:AuthLoginDTO) { + let connection; + let rows = []; + let crows = []; + try { + connection = await this.oracleDBService.getConnection() + if (!connection) { + throw new Error('No DB Connected') + } + + const result = await connection.execute( + `BEGIN + USERLOGIN_PKG.ValidateUser(:p_emailaddr,:p_password,:p_login_cursor,:p_carnet_count_cur); + END;`, + { + p_emailaddr: { + val: body.p_emailaddr, + type: oracledb.DB_TYPE_NVARCHAR + }, + p_password: { + val: body.p_password, + type: oracledb.DB_TYPE_NVARCHAR + }, + p_login_cursor: { + type: oracledb.CURSOR, + dir: oracledb.BIND_OUT + }, + p_carnet_count_cur: { + type: oracledb.CURSOR, + dir: oracledb.BIND_OUT + } + }, + { + outFormat: oracledb.OUT_FORMAT_OBJECT + } + ); + + if (result.outBinds && result.outBinds.p_login_cursor) { + const cursor = result.outBinds.p_login_cursor; // The OUT cursor + let rowsBatch; + + do { + rowsBatch = await cursor.getRows(100); // Fetch 100 rows at a time + rows = rows.concat(rowsBatch); // Append fetched rows to the main array + } while (rowsBatch.length > 0); + + console.log('Rows fetched:', rows); + + // Close the cursor after you're done + await cursor.close(); + } else { + throw new Error('No cursor returned from the stored procedure'); + } + + if (result.outBinds && result.outBinds.p_carnet_count_cur) { + const cursor = result.outBinds.p_carnet_count_cur; // The OUT cursor + let rowsBatch; + + do { + rowsBatch = await cursor.getRows(100); // Fetch 100 rows at a time + crows = rows.concat(rowsBatch); // Append fetched rows to the main array + } while (rowsBatch.length > 0); + + console.log('Rows fetched:', crows); + + // Close the cursor after you're done + await cursor.close(); + } else { + throw new Error('No cursor returned from the stored procedure'); + } + + return {rows,crows}; + + } catch (err) { + console.error('Error fetching users: ', err); + throw new Error('Error fetching users'); + } finally { } + + + } +}