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

95 lines
3.3 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-10-15 19:14:49 +00:00
import { UserService } from '../user.service';
import { FullnodeService } from '../fullnode.service';
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 {
2021-11-02 21:13:24 +00:00
intervalHolder: any;
2021-10-15 19:14:49 +00:00
memos: string[] = [];
nodeAddress: string = '';
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-10-15 19:14:49 +00:00
constructor(
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,
private _changeDetectorRef: ChangeDetectorRef
2021-10-15 19:14:49 +00:00
){
//this.fullnodeService.getAddr();
2021-11-02 21:13:24 +00:00
this.heightUpdate = fullnodeService.heightUpdate;
this.uZaddrUpdate = userService.uZaddrUpdate;
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);
var localToken = localStorage.getItem('s4z_token');
2021-11-02 21:13:24 +00:00
//console.log(localToken);
if(localToken == null){
var token = uuidv4();
localStorage.setItem('s4z_token', token);
console.log('Showing QR code for login');
2021-11-02 21:13:24 +00:00
console.log(URLSafeBase64.encode(Buffer.from('ZGO::'.concat(token))));
var codeString = `zcash:${this.nodeAddress}?amount=0.001&memo=${URLSafeBase64.encode(Buffer.from('ZGO::'.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.uZaddrUpdate.
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))));
2021-11-02 21:13:24 +00:00
var codeString = `zcash:${this.nodeAddress}?amount=0.001&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
});
}
});
}
});
2021-11-02 21:13:24 +00:00
this.intervalHolder = setInterval(() => {
this.fullnodeService.getHeight();
this.fullnodeService.getMemos();
this._changeDetectorRef.markForCheck();
}, 1000 * 75);
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
}
}