zgo/src/app/login/login.component.ts

253 lines
7.1 KiB
TypeScript
Raw Normal View History

2021-11-18 22:38:25 +00:00
import { Component, OnInit, OnDestroy, Injectable, ChangeDetectorRef, ViewChild, AfterViewInit } from '@angular/core';
import { CanActivate, Router, RouterStateSnapshot, ActivatedRouteSnapshot, ActivatedRoute } from '@angular/router';
2021-11-10 15:25:26 +00:00
import { MatDialog, MatDialogConfig} from '@angular/material/dialog';
2022-07-13 12:20:47 +00:00
import { UntypedFormBuilder, Validators, UntypedFormGroup, FormControl } from '@angular/forms';
2021-11-18 22:38:25 +00:00
import { ProgressBarMode } from '@angular/material/progress-bar';
import { MatStepper } from '@angular/material/stepper';
2021-10-15 19:14:49 +00:00
import { UserService } from '../user.service';
import { FullnodeService } from '../fullnode.service';
2021-11-10 15:25:26 +00:00
import { ScanComponent} from '../scan/scan.component';
import { Tx } from '../tx.model';
2023-01-27 23:05:55 +00:00
import { User } from '../user.model';
2022-01-17 20:49:08 +00:00
import { Owner } from '../owner.model';
2021-11-02 21:13:24 +00:00
import { Subscription, Observable } from 'rxjs';
2021-11-18 15:57:22 +00:00
import { take } from 'rxjs/operators';
2021-10-15 19:14:49 +00:00
import { v4 as uuidv4 } from 'uuid';
2023-01-27 23:05:55 +00:00
import { LanguageService } from '../language.service';
import { LanguageData } from '../language.model';
2023-01-27 23:05:55 +00:00
2021-10-15 19:14:49 +00:00
var QRCode = require('easyqrcodejs');
var URLSafeBase64 = require('urlsafe-base64');
var Buffer = require('buffer/').Buffer;
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
2021-11-18 22:38:25 +00:00
export class LoginComponent implements OnInit, AfterViewInit {
txs: Tx[] = [];
2021-11-02 21:13:24 +00:00
intervalHolder: any;
nodeAddress: string = '';
2021-11-10 15:25:26 +00:00
localToken: string | null = '';
selectedValue: number = 0.001;
pinError: boolean = false;
2021-11-11 15:18:38 +00:00
public user:User = {
address: '',
session: '',
blocktime: 0,
pin: '',
validated: false
};
2022-01-17 20:49:08 +00:00
public owner:Owner = {
address: '',
name: '',
currency: '',
tax: false,
taxValue:0,
vat: false,
vatValue: 0,
2022-01-28 20:03:35 +00:00
first: '',
last: '',
2022-01-17 20:49:08 +00:00
email: '',
street: '',
city: '',
state: '',
postal: '',
phone: '',
2022-01-18 22:40:20 +00:00
paid: false,
2022-01-19 16:26:25 +00:00
website: '',
2022-03-07 17:14:29 +00:00
country: '',
2022-05-18 20:51:39 +00:00
zats: false,
invoices: false,
2022-07-14 16:11:04 +00:00
expiration: new Date(Date.now()).toISOString(),
2022-07-18 20:29:27 +00:00
payconf: false,
2022-08-31 13:56:06 +00:00
viewkey: '',
crmToken: ''
2022-01-17 20:49:08 +00:00
};
2021-10-15 19:14:49 +00:00
private FullnodeSub: Subscription = new Subscription();
private UserSub: Subscription = new Subscription();
2021-11-02 21:13:24 +00:00
public heightUpdate: Observable<number>;
public uZaddrUpdate: Observable<string>;
public userUpdate: Observable<User>;
public ownerUpdate: Observable<Owner>;
public txsUpdate: Observable<Tx[]>;
2021-11-11 15:18:38 +00:00
prompt: boolean = false;
2021-11-17 22:03:46 +00:00
confirmedMemo: boolean = false;
targetBlock: number = 0;
2021-11-18 22:38:25 +00:00
barMode: ProgressBarMode = 'indeterminate';
barValue = 0;
barMessage = 'Scanning blockchain for login memo, please wait.';
@ViewChild('stepper') private myStepper?: MatStepper;
2021-11-10 15:25:26 +00:00
2022-07-13 12:20:47 +00:00
entryForm: UntypedFormGroup;
pinForm: UntypedFormGroup;
2021-11-10 15:25:26 +00:00
//
// Language Support
//
vE = {
2023-01-27 23:05:55 +00:00
loginConfirmLogin : '',
loginConnectToZGo : '',
loginEnterPin : '',
loginLastBlock : '',
loginLinkWallet : '',
loginCheckWallet : '',
loginWrongPin : '',
loginConfirmPin : ''
2023-01-27 23:05:55 +00:00
}
//
// ------------------------------------------------------------
2021-10-15 19:14:49 +00:00
constructor(
2022-07-13 12:20:47 +00:00
private fb: UntypedFormBuilder,
private activatedRoute: ActivatedRoute,
2021-10-15 19:14:49 +00:00
public fullnodeService: FullnodeService,
private router: Router,
2021-11-02 21:13:24 +00:00
public userService: UserService,
2021-11-10 15:25:26 +00:00
private dialog: MatDialog,
2023-01-27 23:05:55 +00:00
private _changeDetectorRef: ChangeDetectorRef,
private languageService: LanguageService
2021-10-15 19:14:49 +00:00
){
//this.fullnodeService.getAddr();
2021-11-10 15:25:26 +00:00
this.entryForm = fb.group({
selectedSession: [0.001, Validators.required]
});
2021-11-11 15:18:38 +00:00
this.pinForm = fb.group({
pinValue: [null, Validators.required]
});
2021-11-02 21:13:24 +00:00
this.heightUpdate = fullnodeService.heightUpdate;
this.uZaddrUpdate = userService.uZaddrUpdate;
2021-11-11 15:18:38 +00:00
this.userUpdate = userService.userUpdate;
2022-01-17 20:49:08 +00:00
this.ownerUpdate = userService.ownerUpdate;
2021-11-11 15:18:38 +00:00
this.userUpdate.subscribe((user) => {
this.user = user;
});
2022-01-17 20:49:08 +00:00
this.ownerUpdate.subscribe((owner) => {
this.owner = owner;
});
this.txsUpdate = userService.txUpdate;
this.txsUpdate.subscribe((txs) => {
this.txs = txs;
});
2021-10-15 19:14:49 +00:00
}
2021-11-18 22:38:25 +00:00
ngAfterViewInit(){
2023-01-27 23:05:55 +00:00
2021-11-18 22:38:25 +00:00
//console.log('Step', this.myStepper);
this.pinError = false;
2021-11-02 21:13:24 +00:00
//console.log('Activated route data in Component:::', this.activatedRoute.data);
this.activatedRoute.data.subscribe((addrData) => {
2021-11-02 21:13:24 +00:00
//console.log('FETCH ADDRESS', addrData);
this.nodeAddress = addrData.response.addr;
2021-11-02 21:13:24 +00:00
//console.log('Node addres ', this.nodeAddress);
2021-11-10 15:25:26 +00:00
this.localToken = localStorage.getItem('s4z_token');
2021-11-02 21:13:24 +00:00
//console.log(localToken);
2021-11-10 15:25:26 +00:00
if(this.localToken == null){
var token = uuidv4();
localStorage.setItem('s4z_token', token);
2021-11-10 15:25:26 +00:00
this.localToken = token;
}
2021-11-11 15:18:38 +00:00
this.loginCheck();
});
2021-11-18 22:38:25 +00:00
}
ngOnInit(){
this.chgUILanguage();
2021-11-02 21:13:24 +00:00
this.intervalHolder = setInterval(() => {
this.fullnodeService.getHeight();
2021-11-04 12:49:09 +00:00
//this.userService.findUser();
this.loginCheck();
2021-11-02 21:13:24 +00:00
this._changeDetectorRef.markForCheck();
2021-11-18 22:38:25 +00:00
}, 1000 * 60);
2021-10-15 19:14:49 +00:00
}
2021-11-04 12:49:09 +00:00
loginCheck(){
this.userService.findUser();
2021-11-11 15:18:38 +00:00
this.userUpdate.subscribe((user) => {
2022-01-18 22:40:20 +00:00
if (user.blocktime > 0) {
2022-05-20 20:41:57 +00:00
if(this.myStepper!.selectedIndex === 0) {
2021-11-18 22:38:25 +00:00
this.myStepper!.next();
2022-05-20 20:41:57 +00:00
this.myStepper!.next();
} else {
if(this.myStepper!.selectedIndex === 1){
this.myStepper!.next();
}
2021-11-18 22:38:25 +00:00
}
//console.log('Log in found in blockchain!');
2021-11-11 15:18:38 +00:00
if (user.validated) {
2022-07-14 18:12:26 +00:00
clearInterval(this.intervalHolder);
2022-01-18 22:40:20 +00:00
if (this.owner.paid) {
this.router.navigate(['/shop']);
} else {
this.router.navigate(['/biz']);
}
2021-11-11 15:18:38 +00:00
}
2021-11-04 12:49:09 +00:00
}
});
}
2021-11-18 22:38:25 +00:00
login(stepper: MatStepper) {
2021-11-11 15:18:38 +00:00
//console.log('Dropdown:', this.entryForm.value.selectedSession);
2021-11-10 15:25:26 +00:00
const dialogConfig = new MatDialogConfig();
dialogConfig.disableClose = true;
dialogConfig.autoFocus = true;
dialogConfig.data = {
2022-01-15 13:59:25 +00:00
totalZec: 1e-8,
2021-11-10 15:25:26 +00:00
addr: this.nodeAddress,
2022-01-22 13:49:22 +00:00
session: this.localToken,
pay: false
2021-11-10 15:25:26 +00:00
};
const dialogRef = this.dialog.open(ScanComponent, dialogConfig);
dialogRef.afterClosed().subscribe((val) => {
// console.log('Awaiting confirmation');
2021-11-18 22:38:25 +00:00
if(val){
stepper.next();
}
2021-11-10 15:25:26 +00:00
});
}
2021-11-11 15:18:38 +00:00
confirmPin(){
2022-05-20 18:41:48 +00:00
this.userService.validateUser(this.pinForm.value.pinValue).subscribe((val) => {
if (val) {
this.router.navigate(['/biz']);
2022-07-26 15:00:17 +00:00
clearInterval(this.intervalHolder);
2022-05-20 18:41:48 +00:00
} else {
this.pinError = true;
}
});
2021-11-11 15:18:38 +00:00
}
2021-10-15 19:14:49 +00:00
ngOnDestroy(){
this.FullnodeSub.unsubscribe();
this.UserSub.unsubscribe();
2021-11-02 21:13:24 +00:00
clearInterval(this.intervalHolder);
2021-10-15 19:14:49 +00:00
}
chgUILanguage(){
console.log('LOGIN.chgUILanguage Called ');
this.languageService.getViewElements('login').subscribe(
response => {
console.log('Received >> ', response );
console.log('Language Code : ', response.language);
console.log('Component Name : ',response.component);
console.log('Language data : ',response.data);
this.vE.loginLastBlock = response.data.login_last_block;
this.vE.loginConnectToZGo = response.data.login_connect_to_zgo;
this.vE.loginLinkWallet = response.data.login_link_wallet;
this.vE.loginConfirmLogin = response.data.login_confirm_login;
this.vE.loginEnterPin = response.data.login_enter_pin;
this.vE.loginCheckWallet = response.data.login_check_wallet;
this.vE.loginWrongPin = response.data.login_wrong_pin;
this.vE.loginConfirmPin = response.data.login_confirm_pin;
},
error => { console.log('Error >> ',error); }
);
}
2021-10-15 19:14:49 +00:00
}