import { Component, OnInit, OnDestroy, Injectable, ChangeDetectorRef } from '@angular/core'; import { CanActivate, Router, RouterStateSnapshot, ActivatedRouteSnapshot, ActivatedRoute } from '@angular/router'; import { UserService } from '../user.service'; import { FullnodeService } from '../fullnode.service'; import { Subscription, Observable } from 'rxjs'; 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 { intervalHolder: any; nodeAddress: string = ''; private FullnodeSub: Subscription = new Subscription(); private UserSub: Subscription = new Subscription(); public heightUpdate: Observable; public uZaddrUpdate: Observable; constructor( private activatedRoute: ActivatedRoute, public fullnodeService: FullnodeService, private router: Router, public userService: UserService, private _changeDetectorRef: ChangeDetectorRef ){ //this.fullnodeService.getAddr(); this.heightUpdate = fullnodeService.heightUpdate; this.uZaddrUpdate = userService.uZaddrUpdate; } ngOnInit(){ //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); var localToken = localStorage.getItem('s4z_token'); //console.log(localToken); if(localToken == null){ var token = uuidv4(); localStorage.setItem('s4z_token', token); localToken = token; } this.userService.findUser(); this.userService.uZaddrUpdate. subscribe((userAddr: string) => { if (userAddr.length != 0) { console.log('Log in found!'); this.router.navigate(['/shop']); } else { console.log('No login for existing token found'); 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(localToken!)))}`; console.log(codeString); var qrcode = new QRCode(document.getElementById("qrcode"), { text: codeString, logo: "/assets/zcash.png", logoWidth: 80, logoHeight: 80 }); } }); }); this.intervalHolder = setInterval(() => { this.fullnodeService.getHeight(); //this.userService.findUser(); this.loginCheck(); this._changeDetectorRef.markForCheck(); }, 1000 * 75); } loginCheck(){ this.userService.findUser(); this.uZaddrUpdate.subscribe((userAddr: string) => { if (userAddr.length != 0) { console.log('Log in found in blockchain!'); this.router.navigate(['/shop']); } }); } ngOnDestroy(){ this.FullnodeSub.unsubscribe(); this.UserSub.unsubscribe(); clearInterval(this.intervalHolder); } }