sineedgelenderapp2.0/src/app/authencation/pin-lock/pin-lock.component.ts
2023-12-18 17:44:35 +05:30

249 lines
7.7 KiB
TypeScript

import { Component, OnInit } from '@angular/core';
import { FormBuilder } from '@angular/forms';
import { MenuController, NavController, NavParams } from '@ionic/angular';
import { CognitoService } from '../auth-service/cognito.service';
import { ToastService } from 'src/providers/common-provider/toast.service';
import { AuthService } from '../auth-service/auth.service';
import { LoadingService } from 'src/providers/common-provider/loading.service';
import { environment } from 'src/environments/environment';
import { ActivatedRoute, Router } from '@angular/router';
import { switchMap } from 'rxjs';
import { BiometricAuth, BiometryError, BiometryErrorType } from '@aparajita/capacitor-biometric-auth';
interface ApiResponse {
dataStatus: boolean;
records: any[]; // Adjust the type accordingly
}
@Component({
selector: 'app-pin-lock',
templateUrl: './pin-lock.component.html',
styleUrls: ['./pin-lock.component.scss'],
})
export class PinLockComponent implements OnInit {
commonImage: any;
pinAccess: any;
newpindetails: any;
userDetails: any;
resetlogin:any;
pinreset: Boolean=true;
pin:any;
mpinaccess:boolean=false;
newpassword:any;
ConfirmPassword:any;
private env = environment
resetpin: any;
isPinReset: any;
constructor(private router: Router,public route:ActivatedRoute,public navCtrl: NavController,public Menu:MenuController,public fb:FormBuilder,public shared:CognitoService,public toast:ToastService,public aws:AuthService,public loadingCtrl:LoadingService) {
let pinLockImage = localStorage.getItem('pinLockImage')
if(pinLockImage) {
this.commonImage = pinLockImage
}
else {
this.commonImage = this.env.commonimage;
this.aws.getBase64ImageFromUrl(this.commonImage).then((base64LockImg:any)=>{
if(base64LockImg) {
localStorage.setItem('pinLockImage',base64LockImg);
}
})
}
console.log("entered")
this.shared.refresh()
this.getuserDetails();
}
ngOnInit() {}
ionViewDidLoad() {
console.log('ionViewDidLoad AuthPage');
}
ionViewDidEnter(){
this.Menu.enable(false);
}
ionViewWillLeave(){
this.Menu.enable(true);
}
// Update the subscribe method in getuserDetails
async getuserDetails() {
try {
const userid = this.aws.getlocale();
this.aws.getUserProfile(userid).subscribe({
next: (res: ApiResponse) => {
this.userDetails = res;
if (this.userDetails.dataStatus === true) {
this.userDetails = this.userDetails.records;
this.route.queryParams.pipe(switchMap((params) => {
console.log(params);
this.resetpin = params['resetpin'];
console.log(this.resetpin);
const resetpinAsBoolean = Boolean(this.resetpin);
console.log(this.userDetails,resetpinAsBoolean,this.mpinaccess);
// console.log(typeof this.resetpin);
if (resetpinAsBoolean == true) {
console.log('enter')
this.mpinaccess = true;
this.isPinReset = true;
console.log(this.mpinaccess);
return [];
}
if (this.userDetails[0].mpin == null) {
this.mpinaccess = true;
} else {
this.mpinaccess = false;
}
return [];
})
)
.subscribe();
} else {
this.toast.lenderappmessage('This App is only for PD Officers');
localStorage.clear();
}
},
error: (error: any) => {
console.error(error);
this.loadingCtrl.dismissLoader();
}
});
} catch (error) {
console.error(error);
this.loadingCtrl.dismissLoader();
}
}
resetPin() {
this.router.navigate(['/auth'], { queryParams: { resetlogin: this.pinreset } });
}
goBack() {
this.isPinReset = false
this.mpinaccess = false
}
setnewpin(newpin:any, confirmPin:any) {
if (!newpin.checkValidity()) {
alert(newpin.validationMessage);
return;
}
if (newpin.value.toString().length < 4) {
this.toast.lenderappmessage("Pin must be 4 digits");
return;
}
if (newpin.value !== confirmPin.value) {
this.toast.lenderappmessage("Confirm Pin does not match.");
return;
}
this.aws.userPinUpdate(newpin.value).subscribe((res:any) => {
this.newpindetails = res;
if (this.newpindetails.dataStatus === true) {
if (this.userDetails[0].mpin !== null) {
this.toast.lenderappmessage("Pin Reset Successfully");
this.userDetails[0].mpin = newpin.value;
this.mpinaccess = false;
// Redirect to the appropriate page based on the user role
const role = this.aws.takeDecisionRouting(this.userDetails[0]);
if (role === 'sales') {
this.router.navigate(['/auth/pin-lock'], { queryParams: { userDetails: this.userDetails } });
}
// else {
// this.router.navigate(['/pd-list'], { state: { userDetails: this.userDetails } });
// }
}
}
});
}
checkpin(pin:any) {
console.log(pin);
if (pin !== '' && pin !== null) {
if (this.userDetails[0].mpin === pin) {
const role = this.aws.takeDecisionRouting(this.userDetails[0]);
if (role === 'sales') {
this.router.navigate(['/sales-pd-list']);
}
// else {
// this.router.navigate(['/pd-list'], { state: { userDetails: this.userDetails } });
// }
} else {
this.toast.lenderappmessage('Enter The Correct PIN');
}
} else {
this.toast.lenderappmessage('Enter The PIN');
}
}
// checkfingerprint()
// {
// this.faio.show({
// clientId: 'Fingerprint-Demo',
// clientSecret: 'password', // Only Android
// localizedFallbackTitle: 'Use Pin', // Only iOS
// localizedReason: 'Please authenticate',
// disableBackup:true,
// })
// .then((result: any) => {
// console.log(this.userDetails);
// let role_name=(this.userDetails[0].role_name).toLowerCase()
// console.log("e",role_name)
// if(role_name.includes('sales')){
// this.navCtrl.setRoot(SalesPDlistPage);
// }
// else{
// this.navCtrl.setRoot(PdListPage,{'userDetails':this.userDetails});
// }
// })
// .catch((error: any) => {
// this.toast.lenderappmessage('err: '+ error);
// });
// }
async checkfingerprint(): Promise<void> {
try {
await BiometricAuth.authenticate({
reason: 'Please authenticate',
cancelTitle: 'Cancel',
allowDeviceCredential: true,
iosFallbackTitle: 'Use passcode',
androidTitle: 'Biometric login',
androidSubtitle: 'Log in using biometric authentication',
androidConfirmationRequired: false,
})
console.log(this.userDetails);
let role_name=(this.userDetails[0].role_name).toLowerCase()
console.log("e",role_name)
if(role_name.includes('sales')){
console.log('sales-pd-list')
this.router.navigate(['/sales-pd-list']);
}
// else{
// alert('out')
// this.navCtrl.setRoot(PdListPage,{'userDetails':this.userDetails});
// }
} catch (error) {
// error is always an instance of BiometryError.
if (error instanceof BiometryError) {
if (error.code !== BiometryErrorType.userCancel) {
// Display the error.
await this.toast.lenderappmessage('err: '+ error.message);
}
}
}
}
}