|
|
|
|
@ -9,7 +9,7 @@ import { AuthCreadentialsDto } from './dto/auth-credentials.dto';
|
|
|
|
|
import { TokenDto } from './dto/token.dto';
|
|
|
|
|
import { JwtPayload } from './jwt-payload.interface';
|
|
|
|
|
import { PcduserDto } from 'src/auth/dto/pcd-user.dto';
|
|
|
|
|
import { Connection, createConnection, FindManyOptions, getConnection, getCustomRepository, In, IsNull, Not, QueryRunner } from 'typeorm';
|
|
|
|
|
import { Connection, createConnection, FindManyOptions, getConnection, getCustomRepository, In, IsNull, Like, Not, QueryRunner } from 'typeorm';
|
|
|
|
|
import { TempSignUpDto } from './dto/temp-sign-up.dto';
|
|
|
|
|
import { PcduserRepository } from './pcduser.repository';
|
|
|
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
|
|
|
@ -54,13 +54,14 @@ export class AuthService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async userInfo(authCredentialsDto: AuthCreadentialsDto){
|
|
|
|
|
const { username } = authCredentialsDto;
|
|
|
|
|
let user = await this.pcduserRepository.findOne({ mobileNumber: (username.trim()).toLocaleLowerCase() });
|
|
|
|
|
const { username ,secuLevel } = authCredentialsDto;
|
|
|
|
|
let user = await this.pcduserRepository.findOne({ email: (username.trim()).toLocaleLowerCase() });
|
|
|
|
|
if(!user){
|
|
|
|
|
throw new BadRequestException('User Details not found')
|
|
|
|
|
}
|
|
|
|
|
if(user){
|
|
|
|
|
return {username:user.userName}
|
|
|
|
|
// authCredentialsDto.secuLevel = user.secuLevel
|
|
|
|
|
return {username:user.userName ,secuLevel:user.secuLevel}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -81,7 +82,7 @@ export class AuthService {
|
|
|
|
|
async validateUserPassword(authCredentialsDto: AuthCreadentialsDto): Promise<Pcduser> {
|
|
|
|
|
const { username, password} = authCredentialsDto;
|
|
|
|
|
|
|
|
|
|
let user = await this.pcduserRepository.findOne({ mobileNumber: (username.trim()).toLocaleLowerCase() });
|
|
|
|
|
let user = await this.pcduserRepository.findOne({ email: (username.trim()).toLocaleLowerCase() });
|
|
|
|
|
console.log("user=============", user);
|
|
|
|
|
if (!user) {
|
|
|
|
|
throw new UnauthorizedException('User not Registered')
|
|
|
|
|
@ -96,26 +97,34 @@ export class AuthService {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async forgotPassword(body: { pass: string, mobNo: string, }): Promise<any> {
|
|
|
|
|
let iuserData: PcduserDto = await this.pcduserRepository.findOne({ where: { mobileNumber: body.mobNo } });
|
|
|
|
|
console.log("iuserdata", iuserData.deviceId);
|
|
|
|
|
async forgotPassword(body: { pass: string, email: string, otp:string }): Promise<any> {
|
|
|
|
|
let iuserData: PcduserDto = await this.pcduserRepository.findOne({ where: { email: body.email } });
|
|
|
|
|
console.log("iuserdata",iuserData);
|
|
|
|
|
|
|
|
|
|
const filter: FindManyOptions = { where: { "refCode": body.email, "status": "I" }, order: { code: "DESC" } };
|
|
|
|
|
const newOtp = await this.pcdotpRepository.find(filter);
|
|
|
|
|
|
|
|
|
|
if(body.otp != newOtp[0].otp){
|
|
|
|
|
throw new UnauthorizedException('Unauthorized Access')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const salt = await bcrypt.genSalt();
|
|
|
|
|
iuserData.salt = salt;
|
|
|
|
|
iuserData.pass = await this.pcduserRepository.encryptPassword(body.pass, salt);
|
|
|
|
|
iuserData.pass = await bcrypt.hash(body.pass, salt);
|
|
|
|
|
console.log("salt",iuserData.salt);
|
|
|
|
|
console.log("pass",iuserData.pass);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await this.pcduserRepository.save(iuserData);
|
|
|
|
|
}
|
|
|
|
|
catch (err) {
|
|
|
|
|
throw new BadRequestException("Error in Password creation");
|
|
|
|
|
throw new BadRequestException("Error in Password Reset");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async sendMail(email: string, data: string, flag: string) {
|
|
|
|
|
|
|
|
|
|
let content: string;
|
|
|
|
|
let subject: string = "Login Credentials";
|
|
|
|
|
if (flag == 'otp') {
|
|
|
|
|
@ -123,16 +132,14 @@ export class AuthService {
|
|
|
|
|
subject = 'OTP'
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
// if (flag == 'pwd') {
|
|
|
|
|
// content = `Your New Password for Accurate Application Registration is, ${data}`;
|
|
|
|
|
// subject = 'New Password'
|
|
|
|
|
// }
|
|
|
|
|
// if (flag == 'fpwd') {
|
|
|
|
|
// content = `Your one time password(OTP) for Password reset is, ${data}`;
|
|
|
|
|
// subject = 'OTP'
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
if (flag == 'fpwd') {
|
|
|
|
|
content = `Your one time password(OTP) for Password reset is, ${data}`;
|
|
|
|
|
subject = 'OTP'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await this.mailService.sendMail(email, subject, content, content);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -146,16 +153,20 @@ export class AuthService {
|
|
|
|
|
|
|
|
|
|
console.log("isUserExist",isUserExist);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(!isUserExist){
|
|
|
|
|
throw new BadRequestException("User not found");
|
|
|
|
|
}
|
|
|
|
|
if (flag == 'R' && isUserExist) {
|
|
|
|
|
throw new BadRequestException("Already registered with this email Id.");
|
|
|
|
|
}else{
|
|
|
|
|
let newOtp = await this.generateOtp(body.email, flag);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (body.email && flag == 'R') {
|
|
|
|
|
await this.sendMail(body.email, newOtp, 'otp');
|
|
|
|
|
}
|
|
|
|
|
if (body.email && flag == 'F') {
|
|
|
|
|
await this.sendMail(body.email, newOtp, 'fpwd');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} err => {
|
|
|
|
|
@ -165,22 +176,45 @@ export class AuthService {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// async duplicateCheckingEmail(email: string, flag: string, filter?: FindManyOptions): Promise<any> {
|
|
|
|
|
async tempSignUp(pcdUserDto: PcduserDto): Promise<any> {
|
|
|
|
|
const filter: FindManyOptions = { where: { "refCode": pcdUserDto.email, "status": "I" }, order: { code: "DESC" } };
|
|
|
|
|
const newOtp = await this.pcdotpRepository.find(filter);
|
|
|
|
|
const existingUser = await this.pcduserRepository.findOne({ where: { email: pcdUserDto.email } });
|
|
|
|
|
if (existingUser) {
|
|
|
|
|
throw new BadRequestException('User Already Exists');
|
|
|
|
|
}
|
|
|
|
|
if (pcdUserDto.otp != newOtp[0].otp) {
|
|
|
|
|
throw new UnauthorizedException('Unauthorized Access');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// const isUserExist = await this.pcduserRepository.findOne({ where: { email: email } });
|
|
|
|
|
|
|
|
|
|
// if (isUserExist && flag == 'F')
|
|
|
|
|
// return isUserExist;
|
|
|
|
|
// if (isUserExist && flag == 'R')
|
|
|
|
|
// return isUserExist;
|
|
|
|
|
// else if (isUserExist && flag == 'P')
|
|
|
|
|
// throw new BadRequestException(`Already registered with this email Id - ${isUserExist.email}`);
|
|
|
|
|
//Creating Random User_Id for every user registered
|
|
|
|
|
const lastUser: PcduserDto = await this.pcduserRepository.findOne({
|
|
|
|
|
where: {},
|
|
|
|
|
order: { userId: 'DESC' },
|
|
|
|
|
});
|
|
|
|
|
let maxCode = 10000;
|
|
|
|
|
if (lastUser && lastUser.userId) {
|
|
|
|
|
const lastUserId = parseInt(lastUser.userId);
|
|
|
|
|
maxCode = Math.max(maxCode, lastUserId);
|
|
|
|
|
}
|
|
|
|
|
const newUserId = String(maxCode + 1).padStart(5, '0');
|
|
|
|
|
/////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
pcdUserDto.userId = newUserId;
|
|
|
|
|
const salt = await bcrypt.genSalt();
|
|
|
|
|
pcdUserDto.salt = salt;
|
|
|
|
|
pcdUserDto.pass = await bcrypt.hash(pcdUserDto.pass, salt);
|
|
|
|
|
pcdUserDto.secuLevel = 'U';
|
|
|
|
|
delete pcdUserDto['otp'];
|
|
|
|
|
await this.pcduserRepository.save(pcdUserDto);
|
|
|
|
|
this.logger.verbose("User created with email:", pcdUserDto.email);
|
|
|
|
|
return { data: "Successfully Registered" };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
async verifyOtp(body: { otp: string, email: string }, flag: string): Promise<any> {
|
|
|
|
|
|
|
|
|
|
@ -272,77 +306,7 @@ export class AuthService {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// async updateUser(body: PcduserDto, user: PcduserDto, id: string): Promise<PcduserDto> {
|
|
|
|
|
|
|
|
|
|
// const updatedUser = await this.pcduserRepository.findOne(id);
|
|
|
|
|
// if (updatedUser) {
|
|
|
|
|
// await this.duplicateCheckingUser(body, 'U');
|
|
|
|
|
// updatedUser.userName = body.userName;
|
|
|
|
|
// // updatedUser.email = body.email;
|
|
|
|
|
// updatedUser.mobileNumber = body.mobileNumber;
|
|
|
|
|
// updatedUser.modifiedBy = user.userId;
|
|
|
|
|
// updatedUser.userImagePath = body.userImagePath;
|
|
|
|
|
// updatedUser.memberNo = body.memberNo;
|
|
|
|
|
// for (let eagerCol of await this.pcduserRepository.getEagerColumns()) {
|
|
|
|
|
// delete body[eagerCol];
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// try {
|
|
|
|
|
// delete updatedUser["pass"];
|
|
|
|
|
// delete updatedUser["salt"];
|
|
|
|
|
// delete updatedUser["refreshtoken"];
|
|
|
|
|
// return await this.pcduserRepository.save(updatedUser);
|
|
|
|
|
// } catch (error) {
|
|
|
|
|
|
|
|
|
|
// console.log("====error====", error);
|
|
|
|
|
// throw new BadRequestException(error.message);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// else
|
|
|
|
|
// throw new BadRequestException("User does not Exist");
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// async duplicateCheckingUser(body: PcduserDto, flag: string): Promise<void> {
|
|
|
|
|
// let existingUser: PcduserDto;
|
|
|
|
|
// if (flag == 'I') {
|
|
|
|
|
// let existingIgUser: PcduserDto = await this.pcduserRepository.findOne({ where: { email: body.email } });
|
|
|
|
|
// if (existingIgUser)
|
|
|
|
|
// throw new BadRequestException(`User with same Email exist`);
|
|
|
|
|
// existingUser = await this.pcduserRepository.findOne({ where: [{ "userId": body.userId }, { "email": body.email }, { "mobileNumber": body.mobileNumber }] });
|
|
|
|
|
// }
|
|
|
|
|
// else {
|
|
|
|
|
// existingUser = await this.pcduserRepository.findOne({ where: [{ "userId": (body.userId), "email": body.email }, { "userId": Not(body.userId), "mobileNumber": body.mobileNumber }] });
|
|
|
|
|
// }
|
|
|
|
|
// if (existingUser) {
|
|
|
|
|
// if (flag == 'I' && existingUser.userId == body.userId)
|
|
|
|
|
// throw new BadRequestException(`Duplication in UserId ${body.userId}`);
|
|
|
|
|
// if (existingUser.email == body.email)
|
|
|
|
|
// throw new BadRequestException(`Duplication in Email ${body.email}`);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// async googleSignIn(mediaData: any, ip?: string): Promise<TokenDto> {
|
|
|
|
|
// const iuser: IuserDto = await this.iuserRepository.findOne({ email: mediaData.email });
|
|
|
|
|
// if (!iuser) {
|
|
|
|
|
// throw new UnauthorizedException('User Not registered..');
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// return await this.generateTokens(iuser, iuser.userName, ip, ""); //specify the module here
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// async getVersion(): Promise<{ versionNo: string }> {
|
|
|
|
|
// const result = { versionNo: config.get('version').versionNo };
|
|
|
|
|
// return result;
|
|
|
|
|
// }
|
|
|
|
|
// async createConnection(bankcode: string) {
|
|
|
|
|
// return await connectOraDatabase(bankcode);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// async closeConnection(connection) {
|
|
|
|
|
// await closeOraConnection(connection);
|
|
|
|
|
// }
|
|
|
|
|
/* The below method is used for generating the access token and refresh token when login */
|
|
|
|
|
async generateTokens(user: PcduserDto, username: string, ip: string, module: string, changePassword?: string): Promise<TokenDto> {
|
|
|
|
|
|
|
|
|
|
@ -359,150 +323,10 @@ export class AuthService {
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
// async changePasswordManual(newPassword: ChangePasswordDto, id: string, user: PcduserDto, type?): Promise<{ msg: string, status: string }> {
|
|
|
|
|
// let salt = await bcrypt.genSalt();
|
|
|
|
|
// const selectedUser = await this.pcduserRepository.findOne(id);
|
|
|
|
|
// console.log('newPassword=====', newPassword);
|
|
|
|
|
// console.log('type=====', type);
|
|
|
|
|
// if (!newPassword || !newPassword.pass || !newPassword.pass.toString())
|
|
|
|
|
// throw new BadRequestException("Password can't be null");
|
|
|
|
|
// if (type == 'T') {
|
|
|
|
|
|
|
|
|
|
// selectedUser.tSalt = salt;
|
|
|
|
|
// selectedUser.tPass = await this.hashPassword(newPassword.pass, salt);
|
|
|
|
|
// } else if (type == 'A') {
|
|
|
|
|
|
|
|
|
|
// selectedUser.tSalt = salt;
|
|
|
|
|
// selectedUser.tPass = await this.hashPassword(newPassword.tpass, salt);
|
|
|
|
|
// salt = await bcrypt.genSalt();
|
|
|
|
|
// selectedUser.salt = salt;
|
|
|
|
|
// selectedUser.pass = await this.hashPassword(newPassword.pass, salt);
|
|
|
|
|
// }
|
|
|
|
|
// else {
|
|
|
|
|
|
|
|
|
|
// selectedUser.salt = salt;
|
|
|
|
|
// selectedUser.pass = await this.hashPassword(newPassword.pass, salt);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// try {
|
|
|
|
|
// await this.pcduserRepository.save(selectedUser);
|
|
|
|
|
// return { msg: "Your password changed sucessfully.", status: "Y" }
|
|
|
|
|
// } catch (error) {
|
|
|
|
|
// // if(error.code==23505) not working in orcale
|
|
|
|
|
// console.log("====error====", error);
|
|
|
|
|
// throw new BadRequestException(error.message);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// async changePasswordEmail(id: string, user: PcduserDto, body: PcduserDto): Promise<{ msg: string }> {
|
|
|
|
|
// const salt = await bcrypt.genSalt();
|
|
|
|
|
// const selectedUser = await this.pcduserRepository.findOne(id);
|
|
|
|
|
// if (!selectedUser)
|
|
|
|
|
// throw new BadRequestException("No such user exist");
|
|
|
|
|
// selectedUser.salt = salt;
|
|
|
|
|
// const password: string = generate({ length: 10, lowercase: true, numbers: true, uppercase: true });
|
|
|
|
|
// selectedUser.pass = await this.hashPassword(password, salt);
|
|
|
|
|
// try {
|
|
|
|
|
// await this.iuserRepository.save(selectedUser);
|
|
|
|
|
|
|
|
|
|
// await this.sendMail(selectedUser.email, password);
|
|
|
|
|
// await this.smsService.sendSms(selectedUser.mobileNumber, "Please check your email for new password");
|
|
|
|
|
// return { msg: "Please check your email for new password" };
|
|
|
|
|
// } catch (error) {
|
|
|
|
|
// // if(error.code==23505) not working in orcale
|
|
|
|
|
// console.log("====error====", error);
|
|
|
|
|
// throw new BadRequestException(error.message);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// async changePasswordCurrentUser(passwords: { currentPwd: string, pass: string, deviceId?: string }, id: string, user: PcduserDto): Promise<{ msg: string, status: string }> {
|
|
|
|
|
// let authcredentialsDto = new AuthCreadentialsDto();
|
|
|
|
|
|
|
|
|
|
// authcredentialsDto.password = passwords.currentPwd;
|
|
|
|
|
// authcredentialsDto.username = user.mobileNumber;
|
|
|
|
|
// authcredentialsDto.deviceId = passwords.deviceId;
|
|
|
|
|
// const currentUser = await this.validateUserPassword(authcredentialsDto);
|
|
|
|
|
// console.log(currentUser);
|
|
|
|
|
|
|
|
|
|
// if (currentUser) {
|
|
|
|
|
|
|
|
|
|
/* const connection = await this.createConnection(user.bankId);
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
let mobCustQuery = `SELECT CUST_ID "cust",mcode||'/'||memno "custId" ,CUST_NAME "custName" ,MOBILE "mobile",decript_pwd(mpin) as "mPin",
|
|
|
|
|
decript_pwd(nvl(tpin,mpin)) as "tPin",'001' "bankId"
|
|
|
|
|
FROM PACS_MOB_CUST PMC WHERE PMC.MOBILE = '${user.mobileNumber}' AND PMC.ISACTIVE = '1'`
|
|
|
|
|
const mobCustData = await connection.execute(mobCustQuery, [], { outFormat: oracledb.OBJECT });
|
|
|
|
|
console.log(mobCustData.rows[0]);
|
|
|
|
|
if (mobCustData.rows[0]?.mPin == passwords.pass) {
|
|
|
|
|
return { msg: "Generated Password Cannot be same as New Password", status:"N" }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (err) {
|
|
|
|
|
console.log(err)
|
|
|
|
|
} finally {
|
|
|
|
|
this.closeConnection(connection)
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
// return await this.changePasswordManual(passwords, user.userId, user, 'P');
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// async changeTransactionPinCurrentUser(passwords: { currentPwd: string, pass: string, deviceId?: string }, id: string, user: PcduserDto): Promise<{ msg: string }> {
|
|
|
|
|
// let authcredentialsDto = new AuthCreadentialsDto();
|
|
|
|
|
|
|
|
|
|
// authcredentialsDto.password = passwords.currentPwd;
|
|
|
|
|
// authcredentialsDto.username = user.mobileNumber;
|
|
|
|
|
// authcredentialsDto.deviceId = passwords.deviceId
|
|
|
|
|
// const currentUser = await this.validateTransactionPassword(authcredentialsDto);
|
|
|
|
|
|
|
|
|
|
// if (currentUser) {
|
|
|
|
|
// return await this.changePasswordManual(passwords, user.userId, user, 'T');
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// async changeTransactionPinMpinCurrentUser(passwords: ChangePasswordDto): Promise<{ msg: string }> {
|
|
|
|
|
// let authcredentialsDto = new AuthCreadentialsDto();
|
|
|
|
|
// let user: PcduserDto
|
|
|
|
|
// user = await this.pcduserRepository.findOne({ mobileNumber: passwords.mobNo });
|
|
|
|
|
|
|
|
|
|
// console.log("changebothpin");
|
|
|
|
|
|
|
|
|
|
// authcredentialsDto.password = passwords.pass;
|
|
|
|
|
// authcredentialsDto.username = user.mobileNumber;
|
|
|
|
|
// authcredentialsDto.deviceId = passwords.deviceId;
|
|
|
|
|
// // authcredentialsDto.tpassword = passwords.tpass;
|
|
|
|
|
// // const currentUser = await this.validateUserPassword(authcredentialsDto);
|
|
|
|
|
// // console.log("currentuser", currentUser);
|
|
|
|
|
|
|
|
|
|
// if (user) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// return await this.changePasswordManual(passwords, user.userId, user, 'A');
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
private async hashPassword(password: string, salt: string): Promise<string> {
|
|
|
|
|
return await bcrypt.hash(password, salt);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// async googleSignIn(mediaData: any, ip?: string): Promise<TokenDto> {
|
|
|
|
|
// let pcduser: PcduserDto;
|
|
|
|
|
// pcduser = await this.pcduserRepository.findOne({ email: mediaData.email });
|
|
|
|
|
// if (!pcduser) {
|
|
|
|
|
// // pcduser = await this.tempSignUp({ email: mediaData.email, username: mediaData.name, password: '' })
|
|
|
|
|
// // throw new UnauthorizedException('User Not registered..');
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// return await this.generateTokens(pcduser, pcduser.userName, ip, ""); //specify the module here
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
}
|