import { Component, OnInit, ViewChild, ChangeDetectorRef, NgZone, OnDestroy } from '@angular/core'; import { CommonService } from 'src/app/hmg-common/services/common/common.service'; import { AuthenticationService } from 'src/app/hmg-common/services/authentication/authentication.service'; import { Router } from '@angular/router'; import { AlertController } from '@ionic/angular'; import { TranslatorService } from 'src/app/hmg-common/services/translator/translator.service'; import { CheckUserAuthenticationRequest } from 'src/app/hmg-common/services/authentication/models/check-user-auth.request'; import { CheckUserAuthenticationResponse } from 'src/app/hmg-common/services/authentication/models/check-user-auth.response'; import { CheckActivationCodeRequest } from 'src/app/hmg-common/services/authentication/models/check-activation-code.request'; import { CheckActivationCodeResponse } from 'src/app/hmg-common/services/authentication/models/check-activation-code.response'; import { SmsReaderService } from 'src/app/hmg-common/services/sms/sms-reader.service'; import { AuthenticatedUser } from 'src/app/hmg-common/services/authentication/models/authenticated-user'; import { PATIENT_TYPE } from 'src/app/hmg-common/services/models/patient.type'; import { FingerprintAIO } from '@ionic-native/fingerprint-aio/ngx'; import { GetLoginInfoRequest } from 'src/app/hmg-common/services/authentication/models/get-login-info.request'; import { GetLoginInfoResponse } from 'src/app/hmg-common/services/authentication/models/get-login-info.response'; import { Device } from '@ionic-native/device/ngx'; import { SplashScreen } from '@ionic-native/splash-screen/ngx'; import { CountryCode } from 'src/app/hmg-common/ui/mobile-number/international-mobile/models/country-code.model'; import { SMSService } from 'src/app/hmg-common/ui/sms/service/smsservice'; @Component({ selector: 'login', templateUrl: './login.component.html', styleUrls: ['./login.component.scss'] }) export class LoginComponent implements OnInit, OnDestroy { appLang:number=1; isExpired:boolean =false; isSupportAr:boolean=false; isAppleStore:boolean=false; memberLogin: any = {}; constructor( public cs: CommonService, public authService: AuthenticationService, public router: Router, public alertController: AlertController, public ts: TranslatorService, public smsService: SmsReaderService, private faio: FingerprintAIO, public ngZone: NgZone, public device: Device, public splash: SplashScreen ) { } ngOnInit() { //this.setIdPattern(); setTimeout(() => { this.checkIfLoggedInBefore(); // this.splash.hide(); }, 100); } ngOnDestroy(): void { this.backClicked(); } changeLanguage(){ this.ts.switchLanguage(); } private checkIfLoggedInBefore() { this.cs.startLoading(); // check if user logged in before this.authService.loadAuthenticatedUser().subscribe((user: AuthenticatedUser) => { if (user) { this.startBiometricLogin(user); } else { this.hideSplashScreen(true); } }); } private hideSplashScreen(stopLoading = false) { // this.splash.hide(); if (stopLoading) { this.cs.stopLoading(); } } private startBiometricLogin(user: AuthenticatedUser) { this.faio.isAvailable().then((options) => { this.hideSplashScreen(true); if (user.biometricEnabled) { // ask if login with face or finger this.cs.presentConfirmDialog( this.ts.trPK('login', options), () => this.presentBiometricDialog(user), () => { } ); } else { // ask to enable biometric this.getPermissionToActivateBiometric(user); } }, () => { this.hideSplashScreen(true); }); } private getPermissionToActivateBiometric(user: AuthenticatedUser) { this.cs.presentConfirmDialog( this.ts.trPK('login', 'enable-biometric'), () => { user['biometricEnabled'] = true; this.authService.updateLoggedInUser(user).subscribe((success: boolean) => { this.presentBiometricDialog(user); }); }, () => { } ); } /* activate biometric login for this user */ private getMobileInfo(user: AuthenticatedUser) { this.authService.getLoginInfo(new GetLoginInfoRequest(user), () => { }, this.ts.trPK('general', 'ok')).subscribe((result: GetLoginInfoResponse) => { if (this.cs.validResponse(result)) { if (!result.SMSLoginRequired) { this.loginTokenID = result.LogInTokenID; this.patientOutSA = result.PatientOutSA; this.initializeForAuthentictedUser(user); // sms for register the biometric if (result.isSMSSent) { this.startListeneingForSMS(this.ts.trPK('general', 'enter-sms-enable-biometric')); } else { this.checkActivationCode(); } } } }); } private initializeForAuthentictedUser(user: AuthenticatedUser) { this.ngZone.run(() => { this.isMobileFingerPrint = true; this.FingerPrintPatientIdentificationID = user.IdentificationNo; this.mobileNumber = user.MobileNumber; this.zipCode = CountryCode.localCode(user.ZipCode); }); } private presentBiometricDialog(user) { this.faio.show({ clientId: 'Fingerprint Authetnciation', clientSecret: 'Ate343_9347lajF', // Only necessary for Android disableBackup: true, // Only for Android(optional) localizedFallbackTitle: this.ts.trPK('login', 'use-pin'), // Only for iOS localizedReason: this.ts.trPK('login', 'auth-please') // Only for iOS }).then((result: any) => { // this.checkActivationCode(); this.getMobileInfo(user); }).catch((error: any) => console.log(error)); } /** we need holders here since the country code maybe is not loaded yet for automatic login */ private mobileNumber: string; private zipCode: string; public onLogin() { this.checkUserAuthentication(); } public loginWithMyAccount() { // this.loginWithTamer(); this.loginWithTamer(); } /* TODO to be removed later */ public loginWithEnas() { alert('you are doing slient login width enas account '); const user = new AuthenticatedUser(); user.PatientID = 862616; user.PatientTypeID = PATIENT_TYPE.PERMANENT; user.PatientOutSA = false; user.TokenID = '@dm!n'; user.ProjectID = 0; user.NationalityID = '2300948375'; user.MobileNo = user.MobileNumber = '554355126'; user.ZipCode = '+966'; user.Address = 'riyadh'; user.FirstName = 'MOHAMED'; user.MiddleName = 'yaghi'; user.LastName = 'mohammed'; user.Age = 30; user.agreed = true; const birthDate = new Date(); birthDate.setFullYear(birthDate.getFullYear() - 29); user.DateofBirth = this.cs.convertISODateToJsonDate(this.cs.getDateISO(birthDate)); user.Email = 'Mohamed.Afifi@cloudsolution-sa.com'; user.PatientName = 'enas yaghi'; this.authService.updateLoggedInUser(user).subscribe(done => { this.authService.startIdleMonitoring(); this.cs.openHome(); }); } public loginWithVaccineUser() { alert('you are doing slient login width vaccine account '); const user = new AuthenticatedUser(); user.PatientID = 862616; // user with vaccines in dev user.PatientTypeID = PATIENT_TYPE.PERMANENT; user.PatientOutSA = false; user.TokenID = '@dm!n'; user.NationalityID = '2300948375'; user.MobileNo = user.MobileNumber = '554355126'; user.ProjectID = 0; user.ZipCode = '+966'; user.Address = 'riyadh'; user.FirstName = 'MOHAMED'; user.MiddleName = 'yaghi'; user.LastName = 'mohammed'; user.Age = 30; user.agreed = true; const birthDate = new Date(); birthDate.setFullYear(birthDate.getFullYear() - 29); user.DateofBirth = this.cs.convertISODateToJsonDate(this.cs.getDateISO(birthDate)); user.Email = 'minna.barry@cloudsolution-sa.com'; user.PatientName = 'enas yaghi'; this.authService.updateLoggedInUser(user).subscribe(done => { this.authService.startIdleMonitoring(); this.cs.openHome(); }); } public loginWithEyeMeasureUser() { alert('you are doing slient login width eye measurements user account '); const user = new AuthenticatedUser(); user.PatientID = 873010; user.PatientTypeID = PATIENT_TYPE.PERMANENT; user.PatientOutSA = false; user.TokenID = '@dm!n'; user.NationalityID = '2302581828'; user.ProjectID = 0; user.MobileNo = user.MobileNumber = '555333541'; user.ZipCode = '+966'; user.Address = 'riyadh'; user.FirstName = 'eye'; user.MiddleName = 'user'; user.LastName = 'measurment'; user.Age = 30; user.agreed = true; const birthDate = new Date(); birthDate.setFullYear(birthDate.getFullYear() - 29); user.DateofBirth = this.cs.convertISODateToJsonDate(this.cs.getDateISO(birthDate)); user.Email = 'sultan.khan@hmg.local'; user.PatientName = 'eye user'; this.authService.updateLoggedInUser(user).subscribe(done => { this.authService.startIdleMonitoring(); this.cs.openHome(); }); } /* TODO login with mr rwaid */ public loginWithRwaid() { alert('you are doing slient login width mr: rwaid account'); const user = new AuthenticatedUser(); // tamer with eye measurments 1231755 user.PatientID = 1018977; user.PatientTypeID = PATIENT_TYPE.PERMANENT; user.ProjectID = 0; user.PatientOutSA = false; user.TokenID = '@dm!n'; user.NationalityID = '1001242559'; user.MobileNo = user.MobileNumber = '545156035'; user.ZipCode = '+966'; user.Address = 'riyadh'; user.FirstName = 'rwaid'; user.MiddleName = 'el mallah'; user.LastName = 'mohammed'; user.Age = 30; user.agreed = true; const birthDate = new Date(); birthDate.setFullYear(birthDate.getFullYear() - 29); user.DateofBirth = this.cs.convertISODateToJsonDate(this.cs.getDateISO(birthDate)); user.Email = 'mohamed.afifi@cloudsolution-sa.com'; user.PatientName = 'rwaid al mallah'; this.authService.updateLoggedInUser(user).subscribe(done => { this.authService.startIdleMonitoring(); this.cs.openHome(); }); } public loginWithTamer() { alert('you are doing slient login width tamer account'); const user = new AuthenticatedUser(); user.PatientID = 1231755; user.PatientTypeID = PATIENT_TYPE.PERMANENT; user.ProjectID = 0; user.PatientOutSA = false; user.TokenID = '@dm!n'; user.NationalityID = '1001242559'; user.MobileNo = user.MobileNumber = '537503378'; user.ZipCode = '+966'; user.Address = 'riyadh'; user.FirstName = 'tamer'; user.MiddleName = 'faneshah'; user.LastName = 'faneshah'; user.Age = 30; user.agreed = true; const birthDate = new Date(); birthDate.setFullYear(birthDate.getFullYear() - 29); user.DateofBirth = this.cs.convertISODateToJsonDate(this.cs.getDateISO(birthDate)); user.Email = 'mohamed.afifi@cloudsolution-sa.com'; user.PatientName = 'tamer fneshah'; this.authService.updateLoggedInUser(user).subscribe(done => { this.authService.startIdleMonitoring(); this.cs.openHome(); }); } private startListeneingForSMS(title?: string) { this.startReceivingSMS(); //this.presentSMSPasswordDialog(title); } private checkUserAuthentication() { const request = new CheckUserAuthenticationRequest(); request.PatientMobileNumber = this.mobileNumber; request.ZipCode = this.zipCode; request.isRegister = false; request.TokenID = ''; this.authService.checkUserAuthentication( request, () => { }, this.ts.trPK('general', 'ok')).subscribe((result: CheckUserAuthenticationResponse) => { if (this.cs.validResponse(result)) { this.loginTokenID = result.LogInTokenID; this.patientOutSA = result.PatientOutSA; this.isMobileFingerPrint = false; this.FingerPrintPatientIdentificationID = ''; if (result.isSMSSent) { this.startListeneingForSMS(); } else { this.smsService.stopSMSMonitoring(); this.checkActivationCode(); } } }); } public backClicked() { this.smsService.stopSMSMonitoring(); } private startReceivingSMS() { // this.smsModal.presentModal(); // this.smsService.startSMSMonitoring((code) => { // this.smsModal.dismiss; // this.global_code = code; // SMSService.code = this.global_code; // this.checkActivationCode(code); // this.cs.dismissSMSDialog().subscribe(cleared => { // this.checkActivationCode(code); // }); // }); } public presentSMSPasswordDialog(title?: string) { this.cs.presentSMSPasswordDialog( (code: string) => { this.checkActivationCode(code); }, null, title); } private patientOutSA: boolean; private loginTokenID: string; private isMobileFingerPrint: boolean; private FingerPrintPatientIdentificationID: string; private checkActivationCode(readedCode?) { const request = new CheckActivationCodeRequest(); request.IsMobileFingerPrint = this.isMobileFingerPrint; request.FingerPrintPatientIdentificationID = this.FingerPrintPatientIdentificationID; request.LogInTokenID = this.loginTokenID; request.PatientOutSA = this.patientOutSA ? 1 : 0; request.activationCode = readedCode || '0000'; request.IsSilentLogin = !readedCode; request.PatientMobileNumber = this.mobileNumber; request.ZipCode = this.zipCode; // request.SearchType = this.loginType; // if (this.loginType === LoginComponent.IDENTIFCIATION_LOGIN_TYPE) { // request.PatientIdentificationID = this.id; // request.PatientID = 0; // } else { // request.PatientID = Number(this.id); // request.PatientIdentificationID = ''; // } request.isRegister = false; // this.authService.checkActivationCode( // request, // () => { // //this.presentSMSPasswordDialog(); // this.smsModal.presentModal(); // }, this.ts.trPK('general', 'retry')).subscribe((result: CheckActivationCodeResponse) => { // if (this.cs.validResponse(result)) { // if (this.cs.hasData(result.List)) { // this.smsService.stopSMSMonitoring(); // this.checkIfUserAgreedBefore(result); // } // } // }); } private checkIfUserAgreedBefore(result: CheckActivationCodeResponse) { this.authService.setAuthenticatedUser(result).subscribe(() => { if (this.authService.isAgreedBefore()) { this.cs.openHome(); } else { this.cs.openAgreement(); } }); } private checkUserAgreement() { } public signOut() { // this.cs.presentConfirmDialog(this.ts.trPK('login', 'sign-out'), // () => { // this.authService.clearUser().subscribe(success => { // this.id = null; // if (this.countryCode) { // this.internationlMobile.setMobileNumber(this.countryCode.code, null); // } // }); // }); } public openForgotID() { this.cs.openUserForgot(); } public onDismiss() { // this.global_code = SMSService.code; //this.checkActivationCode(this.global_code); } public onCancelled() { console.log("Modal pop up cancelled"); } }