1
0
Fork 0
forked from lthn/blockchain
blockchain/src/gui/qt-daemon/html_source/src/app/login/login.component.ts

158 lines
5.6 KiB
TypeScript
Raw Normal View History

2019-01-09 15:31:41 +02:00
import {Component, NgZone, OnInit, OnDestroy} from '@angular/core';
2019-05-07 16:26:35 +03:00
import {FormGroup, FormControl} from '@angular/forms';
2018-12-27 18:50:45 +03:00
import {ActivatedRoute, Router} from '@angular/router';
import {BackendService} from '../_helpers/services/backend.service';
import {VariablesService} from '../_helpers/services/variables.service';
2019-01-09 15:31:41 +02:00
import {ModalService} from '../_helpers/services/modal.service';
2018-12-27 18:50:45 +03:00
import {Wallet} from '../_helpers/models/wallet.model';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.scss']
})
2019-01-09 15:31:41 +02:00
export class LoginComponent implements OnInit, OnDestroy {
queryRouting;
2018-12-27 18:50:45 +03:00
regForm = new FormGroup({
password: new FormControl(''),
confirmation: new FormControl('')
2018-12-27 18:50:45 +03:00
}, function (g: FormGroup) {
return g.get('password').value === g.get('confirmation').value ? null : {'mismatch': true};
});
authForm = new FormGroup({
password: new FormControl('')
2018-12-27 18:50:45 +03:00
});
type = 'reg';
constructor(
private route: ActivatedRoute,
private router: Router,
private backend: BackendService,
public variablesService: VariablesService,
2019-01-09 15:31:41 +02:00
private modalService: ModalService,
2018-12-27 18:50:45 +03:00
private ngZone: NgZone
2019-05-02 14:00:35 +03:00
) {}
2018-12-27 18:50:45 +03:00
ngOnInit() {
2019-01-09 15:31:41 +02:00
this.queryRouting = this.route.queryParams.subscribe(params => {
2018-12-27 18:50:45 +03:00
if (params.type) {
this.type = params.type;
}
});
}
onSubmitCreatePass(): void {
if (this.regForm.valid) {
this.variablesService.appPass = this.regForm.get('password').value;
this.backend.storeSecureAppData((status, data) => {
if (status) {
2019-04-22 12:22:16 +03:00
this.variablesService.appLogin = true;
this.variablesService.startCountdown();
2018-12-27 18:50:45 +03:00
this.ngZone.run(() => {
this.router.navigate(['/']);
});
} else {
console.log(data['error_code']);
}
});
}
}
2019-04-22 12:22:16 +03:00
onSkipCreatePass(): void {
this.variablesService.appPass = '';
2019-04-22 12:22:16 +03:00
this.ngZone.run(() => {
this.variablesService.appLogin = true;
this.router.navigate(['/']);
});
}
2018-12-27 18:50:45 +03:00
onSubmitAuthPass(): void {
if (this.authForm.valid) {
const appPass = this.authForm.get('password').value;
this.backend.getSecureAppData({pass: appPass}, (status, data) => {
2019-01-09 15:31:41 +02:00
if (!data.error_code) {
2019-04-22 12:22:16 +03:00
this.variablesService.appLogin = true;
2018-12-27 18:50:45 +03:00
this.variablesService.startCountdown();
this.variablesService.appPass = appPass;
if (this.variablesService.wallets.length) {
this.ngZone.run(() => {
this.router.navigate(['/wallet/' + this.variablesService.wallets[0].wallet_id]);
});
return;
}
if (Object.keys(data).length !== 0) {
let openWallets = 0;
let runWallets = 0;
data.forEach((wallet, wallet_index) => {
2019-04-01 17:59:59 +03:00
this.backend.openWallet(wallet.path, wallet.pass, true, (open_status, open_data, open_error) => {
if (open_status || open_error === 'FILE_RESTORED') {
2018-12-27 18:50:45 +03:00
openWallets++;
2019-03-27 16:27:47 +02:00
this.ngZone.run(() => {
const new_wallet = new Wallet(
open_data.wallet_id,
wallet.name,
wallet.pass,
open_data['wi'].path,
open_data['wi'].address,
open_data['wi'].balance,
open_data['wi'].unlocked_balance,
open_data['wi'].mined_total,
open_data['wi'].tracking_hey
);
new_wallet.alias = this.backend.getWalletAlias(new_wallet.address);
if (open_data.recent_history && open_data.recent_history.history) {
new_wallet.prepareHistory(open_data.recent_history.history);
}
this.backend.getContracts(open_data.wallet_id, (contracts_status, contracts_data) => {
if (contracts_status && contracts_data.hasOwnProperty('contracts')) {
this.ngZone.run(() => {
new_wallet.prepareContractsAfterOpen(contracts_data.contracts, this.variablesService.exp_med_ts, this.variablesService.height_app, this.variablesService.settings.viewedContracts, this.variablesService.settings.notViewedContracts);
});
}
});
this.variablesService.wallets.push(new_wallet);
if (this.variablesService.wallets.length === 1) {
this.router.navigate(['/wallet/' + this.variablesService.wallets[0].wallet_id]);
}
});
2018-12-27 18:50:45 +03:00
this.backend.runWallet(open_data.wallet_id, (run_status) => {
if (run_status) {
runWallets++;
} else {
if (wallet_index === data.length - 1 && runWallets === 0) {
this.ngZone.run(() => {
this.router.navigate(['/']);
});
}
}
});
} else {
if (wallet_index === data.length - 1 && openWallets === 0) {
this.ngZone.run(() => {
this.router.navigate(['/']);
});
}
}
});
});
} else {
this.ngZone.run(() => {
this.router.navigate(['/']);
});
}
}
});
}
}
2019-01-09 15:31:41 +02:00
ngOnDestroy() {
this.queryRouting.unsubscribe();
}
2018-12-27 18:50:45 +03:00
}