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

171 lines
4.7 KiB
TypeScript
Raw Normal View History

2021-11-02 21:13:24 +00:00
import { Component, OnInit, OnDestroy, Injectable, ChangeDetectorRef } 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';
import { FormBuilder, Validators, FormGroup, FormControl } from '@angular/forms';
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';
2021-11-11 15:18:38 +00:00
import {User} from '../user.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';
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[] = [];
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,
expiration: 0,
pin: '',
validated: false
};
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>;
2021-11-11 15:18:38 +00:00
public userUpdate:Observable<User>;
public txsUpdate: Observable<Tx[]>;
2021-11-10 15:25:26 +00:00
tickets = [
{
value: 0.001,
viewValue: '1 hour: 0.001 ZEC'
2021-11-10 15:25:26 +00:00
},{
value: 0.005,
viewValue: '1 day: 0.005 ZEC'
},{
value: 0.025,
viewValue: '1 week: 0.025 ZEC'
2021-11-10 15:25:26 +00:00
}
];
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-10 15:25:26 +00:00
entryForm: FormGroup;
2021-11-11 15:18:38 +00:00
pinForm: FormGroup;
2021-11-10 15:25:26 +00:00
2021-10-15 19:14:49 +00:00
constructor(
2021-11-10 15:25:26 +00:00
private fb: FormBuilder,
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,
2021-11-02 21:13:24 +00:00
private _changeDetectorRef: ChangeDetectorRef
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;
this.userUpdate.subscribe((user) => {
this.user = user;
});
this.txsUpdate = userService.txUpdate;
this.txsUpdate.subscribe((txs) => {
this.txs = txs;
});
2021-10-15 19:14:49 +00:00
}
ngOnInit(){
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-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();
}, 1000 * 75);
2021-10-15 19:14:49 +00:00
}
2021-11-04 12:49:09 +00:00
loginCheck(){
2021-11-11 15:18:38 +00:00
var today = new Date().getTime() / 1000;
2021-11-04 12:49:09 +00:00
this.userService.findUser();
this.userService.findPending();
this.txsUpdate.subscribe((txs) => {
this.txs = txs;
});
2021-11-11 15:18:38 +00:00
this.userUpdate.subscribe((user) => {
if (user.expiration > today) {
this.prompt = true;
2021-11-04 12:49:09 +00:00
console.log('Log in found in blockchain!');
2021-11-11 15:18:38 +00:00
if (user.validated) {
this.router.navigate(['/shop']);
}
2021-11-04 12:49:09 +00:00
}
});
}
2021-11-10 15:25:26 +00:00
login() {
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 = {
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');
2021-11-17 22:03:46 +00:00
this.confirmedMemo = val;
2021-11-18 15:57:22 +00:00
this.heightUpdate.pipe(take(1)).subscribe(block => {
2021-11-17 22:03:46 +00:00
this.targetBlock = block + 10;
});
2021-11-10 15:25:26 +00:00
});
}
2021-11-11 15:18:38 +00:00
confirmPin(){
if (this.user.pin === this.pinForm.value.pinValue) {
this.userService.validateUser();
this.router.navigate(['/shop']);
} 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
}
}