BE/src/auth/auth.service.ts

68 lines
1.9 KiB
TypeScript
Raw Normal View History

2025-04-02 13:03:48 +05:30
import { BadRequestException, Injectable } from '@nestjs/common';
2025-03-11 10:52:58 +05:30
import { OracleDBService } from 'src/db/db.service';
import { AuthLoginDTO } from './auth.dto';
2025-04-02 13:29:06 +05:30
import * as oracledb from 'oracledb';
2025-03-11 10:52:58 +05:30
@Injectable()
export class AuthService {
2025-04-02 13:29:06 +05:30
constructor(private readonly oracleDBService: OracleDBService) {}
2025-03-11 10:52:58 +05:30
2025-04-02 13:29:06 +05:30
async login(body: AuthLoginDTO) {
let connection;
let rows = [];
try {
connection = await this.oracleDBService.getConnection();
if (!connection) {
throw new Error('No DB Connected');
}
2025-03-11 10:52:58 +05:30
2025-04-02 13:29:06 +05:30
const result = await connection.execute(
`BEGIN
2025-03-17 11:05:03 +05:30
USERLOGIN_PKG.ValidateUser(:p_emailaddr,:p_password,:p_login_cursor);
2025-03-11 10:52:58 +05:30
END;`,
2025-04-02 13:29:06 +05:30
{
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,
},
},
{
outFormat: oracledb.OUT_FORMAT_OBJECT,
},
);
2025-03-11 10:52:58 +05:30
2025-04-02 13:29:06 +05:30
if (result.outBinds && result.outBinds.p_login_cursor) {
const cursor = result.outBinds.p_login_cursor;
let rowsBatch;
2025-03-11 10:52:58 +05:30
2025-04-02 13:29:06 +05:30
do {
rowsBatch = await cursor.getRows(100);
rows = rows.concat(rowsBatch);
} while (rowsBatch.length > 0);
2025-03-11 10:52:58 +05:30
2025-04-02 13:29:06 +05:30
await cursor.close();
} else {
return new BadRequestException({
Error: 'Error executing request try after some time!',
});
}
2025-03-11 10:52:58 +05:30
2025-04-02 13:29:06 +05:30
if (rows[0]['ERRORMESG']) {
return { error: 'Invalid username or password!' };
}
return { msg: 'Logged in successfully' };
} catch (err) {
console.error('Error fetching users: ', err.message);
return { error: 'Invalid username or password' };
2025-03-11 10:52:58 +05:30
}
2025-04-02 13:29:06 +05:30
}
2025-03-11 10:52:58 +05:30
}