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

171 lines
4.7 KiB
TypeScript

import { Component, OnInit, OnDestroy, Injectable, ChangeDetectorRef } from '@angular/core';
import { CanActivate, Router, RouterStateSnapshot, ActivatedRouteSnapshot, ActivatedRoute } from '@angular/router';
import { MatDialog, MatDialogConfig} from '@angular/material/dialog';
import { FormBuilder, Validators, FormGroup, FormControl } from '@angular/forms';
import { UserService } from '../user.service';
import { FullnodeService } from '../fullnode.service';
import { ScanComponent} from '../scan/scan.component';
import { Tx } from '../tx.model';
import {User} from '../user.model';
import { Subscription, Observable } from 'rxjs';
import { take } from 'rxjs/operators';
import { v4 as uuidv4 } from 'uuid';
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']
})
export class LoginComponent implements OnInit {
txs: Tx[] = [];
intervalHolder: any;
nodeAddress: string = '';
localToken: string | null = '';
selectedValue: number = 0.001;
pinError: boolean = false;
public user:User = {
address: '',
session: '',
blocktime: 0,
expiration: 0,
pin: '',
validated: false
};
private FullnodeSub: Subscription = new Subscription();
private UserSub: Subscription = new Subscription();
public heightUpdate: Observable<number>;
public uZaddrUpdate: Observable<string>;
public userUpdate:Observable<User>;
public txsUpdate: Observable<Tx[]>;
tickets = [
{
value: 0.001,
viewValue: '1 hour: 0.001 ZEC'
},{
value: 0.005,
viewValue: '1 day: 0.005 ZEC'
},{
value: 0.025,
viewValue: '1 week: 0.025 ZEC'
}
];
prompt: boolean = false;
confirmedMemo: boolean = false;
targetBlock: number = 0;
entryForm: FormGroup;
pinForm: FormGroup;
constructor(
private fb: FormBuilder,
private activatedRoute: ActivatedRoute,
public fullnodeService: FullnodeService,
private router: Router,
public userService: UserService,
private dialog: MatDialog,
private _changeDetectorRef: ChangeDetectorRef
){
//this.fullnodeService.getAddr();
this.entryForm = fb.group({
selectedSession: [0.001, Validators.required]
});
this.pinForm = fb.group({
pinValue: [null, Validators.required]
});
this.heightUpdate = fullnodeService.heightUpdate;
this.uZaddrUpdate = userService.uZaddrUpdate;
this.userUpdate = userService.userUpdate;
this.userUpdate.subscribe((user) => {
this.user = user;
});
this.txsUpdate = userService.txUpdate;
this.txsUpdate.subscribe((txs) => {
this.txs = txs;
});
}
ngOnInit(){
this.pinError = false;
//console.log('Activated route data in Component:::', this.activatedRoute.data);
this.activatedRoute.data.subscribe((addrData) => {
//console.log('FETCH ADDRESS', addrData);
this.nodeAddress = addrData.response.addr;
//console.log('Node addres ', this.nodeAddress);
this.localToken = localStorage.getItem('s4z_token');
//console.log(localToken);
if(this.localToken == null){
var token = uuidv4();
localStorage.setItem('s4z_token', token);
this.localToken = token;
}
this.loginCheck();
});
this.intervalHolder = setInterval(() => {
this.fullnodeService.getHeight();
//this.userService.findUser();
this.loginCheck();
this._changeDetectorRef.markForCheck();
}, 1000 * 75);
}
loginCheck(){
var today = new Date().getTime() / 1000;
this.userService.findUser();
this.userService.findPending();
this.txsUpdate.subscribe((txs) => {
this.txs = txs;
});
this.userUpdate.subscribe((user) => {
if (user.expiration > today) {
this.prompt = true;
console.log('Log in found in blockchain!');
if (user.validated) {
this.router.navigate(['/shop']);
}
}
});
}
login() {
//console.log('Dropdown:', this.entryForm.value.selectedSession);
const dialogConfig = new MatDialogConfig();
dialogConfig.disableClose = true;
dialogConfig.autoFocus = true;
dialogConfig.data = {
totalZec: this.entryForm.value.selectedSession,
addr: this.nodeAddress,
session: this.localToken
};
const dialogRef = this.dialog.open(ScanComponent, dialogConfig);
dialogRef.afterClosed().subscribe((val) => {
console.log('Awaiting confirmation');
this.confirmedMemo = val;
this.heightUpdate.pipe(take(1)).subscribe(block => {
this.targetBlock = block + 10;
});
});
}
confirmPin(){
if (this.user.pin === this.pinForm.value.pinValue) {
this.userService.validateUser();
this.router.navigate(['/shop']);
} else {
this.pinError = true;
}
}
ngOnDestroy(){
this.FullnodeSub.unsubscribe();
this.UserSub.unsubscribe();
clearInterval(this.intervalHolder);
}
}