2021-11-02 21:13:24 +00:00
|
|
|
import { Component, OnInit, OnDestroy, Injectable, ChangeDetectorRef } from '@angular/core';
|
2021-10-21 15:23:58 +00:00
|
|
|
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';
|
2021-11-09 21:00:01 +00:00
|
|
|
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-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-09 21:00:01 +00:00
|
|
|
txs: Tx[] = [];
|
2021-11-02 21:13:24 +00:00
|
|
|
intervalHolder: any;
|
2021-10-21 15:23:58 +00:00
|
|
|
nodeAddress: string = '';
|
2021-11-10 15:25:26 +00:00
|
|
|
localToken: string | null = '';
|
|
|
|
selectedValue: number = 0.001;
|
2021-11-11 19:52:07 +00:00
|
|
|
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>;
|
2021-11-09 21:00:01 +00:00
|
|
|
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'
|
|
|
|
}
|
|
|
|
];
|
2021-11-11 15:18:38 +00:00
|
|
|
prompt: boolean = false;
|
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,
|
2021-10-21 15:23:58 +00:00
|
|
|
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
|
|
|
){
|
2021-10-21 15:23:58 +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;
|
|
|
|
});
|
2021-11-09 21:00:01 +00:00
|
|
|
this.txsUpdate = userService.txUpdate;
|
|
|
|
this.txsUpdate.subscribe((txs) => {
|
|
|
|
this.txs = txs;
|
|
|
|
});
|
2021-10-15 19:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit(){
|
2021-11-11 19:52:07 +00:00
|
|
|
this.pinError = false;
|
2021-11-02 21:13:24 +00:00
|
|
|
//console.log('Activated route data in Component:::', this.activatedRoute.data);
|
2021-10-21 15:23:58 +00:00
|
|
|
this.activatedRoute.data.subscribe((addrData) => {
|
2021-11-02 21:13:24 +00:00
|
|
|
//console.log('FETCH ADDRESS', addrData);
|
2021-10-21 15:23:58 +00:00
|
|
|
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){
|
2021-10-21 15:23:58 +00:00
|
|
|
var token = uuidv4();
|
|
|
|
localStorage.setItem('s4z_token', token);
|
2021-11-10 15:25:26 +00:00
|
|
|
this.localToken = token;
|
2021-10-21 15:23:58 +00:00
|
|
|
}
|
2021-11-11 15:18:38 +00:00
|
|
|
this.loginCheck();
|
2021-10-21 15:23:58 +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(){
|
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();
|
2021-11-09 21:00:01 +00:00
|
|
|
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-11 15:18:38 +00:00
|
|
|
confirmPin(){
|
|
|
|
if (this.user.pin === this.pinForm.value.pinValue) {
|
|
|
|
this.userService.validateUser();
|
|
|
|
this.router.navigate(['/shop']);
|
2021-11-11 19:52:07 +00:00
|
|
|
} 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
|
|
|
}
|
|
|
|
}
|