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

146 lines
4.4 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-02 21:13:24 +00:00
import { Subscription, Observable } from 'rxjs';
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;
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 txsUpdate: Observable<Tx[]>;
2021-11-10 15:25:26 +00:00
tickets = [
{
value: 0.001,
viewValue: 'One hour'
},{
value: 0.005,
viewValue: 'One day'
}
];
entryForm: FormGroup;
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-02 21:13:24 +00:00
this.heightUpdate = fullnodeService.heightUpdate;
this.uZaddrUpdate = userService.uZaddrUpdate;
this.txsUpdate = userService.txUpdate;
this.txsUpdate.subscribe((txs) => {
this.txs = txs;
});
2021-10-15 19:14:49 +00:00
}
ngOnInit(){
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-04 12:49:09 +00:00
this.userService.findUser();
this.userService.uZaddrUpdate.
subscribe((userAddr: string) => {
if (userAddr.length != 0) {
console.log('Log in found!');
2021-11-05 14:53:29 +00:00
this.router.navigate(['/shop']);
2021-11-04 12:49:09 +00:00
} else {
console.log('No login for existing token found');
2021-11-10 15:25:26 +00:00
//console.log('Showing QR code for login');
////console.log(URLSafeBase64.encode(Buffer.from('S4ZEC::'.concat(localToken))));
//var codeString = `zcash:${this.nodeAddress}?amount=0.005&memo=${URLSafeBase64.encode(Buffer.from('ZGO::'.concat(this.localToken!)))}`;
//console.log(codeString);
//var qrcode = new QRCode(document.getElementById("qrcode"), {
//text: codeString,
//logo: "/assets/zcash.png",
//logoWidth: 80,
//logoHeight: 80
//});
2021-11-04 12:49:09 +00:00
}
});
});
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(){
this.userService.findUser();
this.userService.findPending();
this.txsUpdate.subscribe((txs) => {
this.txs = txs;
});
2021-11-04 12:49:09 +00:00
this.uZaddrUpdate.subscribe((userAddr: string) => {
if (userAddr.length != 0) {
console.log('Log in found in blockchain!');
2021-11-05 14:53:29 +00:00
this.router.navigate(['/shop']);
2021-11-04 12:49:09 +00:00
}
});
}
2021-11-10 15:25:26 +00:00
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');
});
}
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
}
}