import { Component, OnInit, OnDestroy, Injectable } from '@angular/core'; import { CanActivate, Router, RouterStateSnapshot, ActivatedRouteSnapshot } from '@angular/router'; import { UserService } from '../user.service'; import { FullnodeService } from '../fullnode.service'; import { Subscription } 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 { memos: string[] = []; private FullnodeSub: Subscription = new Subscription(); private UserSub: Subscription = new Subscription(); constructor( public fullnodeService: FullnodeService, private router: Router, public userService: UserService ){ } ngOnInit(){ this.fullnodeService.getMemos(); this.fullnodeService.getAddr(); var nodeAddr = this.fullnodeService.addrUpdate; var localToken = localStorage.getItem('s4z_token'); console.log(localToken); if(localToken == null){ var token = uuidv4(); localStorage.setItem('s4z_token', token); console.log('Showing QR code for login'); console.log(URLSafeBase64.encode(Buffer.from('S4ZEC::'.concat(token)))); var codeString = `zcash:${nodeAddr}?amount=0.001&memo=${URLSafeBase64.encode(Buffer.from('S4ZEC::'.concat(token)))}`; console.log(codeString); var qrcode = new QRCode(document.getElementById("qrcode"), { text: codeString, logo: "/assets/zcash.png", logoWidth: 80, logoHeight: 80 }); } else { this.userService.getUser(localToken); this.userService.getZaddrUpdateListener(). subscribe((userAddr: string) => { if (userAddr.length != 0) { console.log('Log in found!'); //this.router.navigate(['/view']); } 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:${nodeAddr}?amount=0.001&memo=${URLSafeBase64.encode(Buffer.from('S4ZEC::'.concat(localToken!)))}`; console.log(codeString); var qrcode = new QRCode(document.getElementById("qrcode"), { text: codeString, logo: "/assets/zcash.png", logoWidth: 80, logoHeight: 80 }); } }); } } ngOnDestroy(){ this.FullnodeSub.unsubscribe(); this.UserSub.unsubscribe(); } }