bitwarden-estensione-browser/src/app/accounts/register.component.ts

83 lines
3.3 KiB
TypeScript
Raw Normal View History

2018-01-31 20:19:21 +01:00
import * as template from './register.component.html';
2018-02-08 16:37:54 +01:00
import { Component } from '@angular/core';
2018-01-31 20:19:21 +01:00
import { Router } from '@angular/router';
import { ToasterService } from 'angular2-toaster';
2018-02-08 16:37:54 +01:00
import { Angulartics2 } from 'angulartics2';
2018-01-31 20:19:21 +01:00
import { RegisterRequest } from 'jslib/models/request/registerRequest';
import { ApiService } from 'jslib/abstractions/api.service';
import { AuthService } from 'jslib/abstractions/auth.service';
import { CryptoService } from 'jslib/abstractions/crypto.service';
import { I18nService } from 'jslib/abstractions/i18n.service';
@Component({
selector: 'app-register',
template: template,
})
export class RegisterComponent {
email: string = '';
masterPassword: string = '';
confirmMasterPassword: string = '';
hint: string = '';
showPassword: boolean = false;
2018-01-31 20:19:21 +01:00
formPromise: Promise<any>;
constructor(private authService: AuthService, private router: Router, private analytics: Angulartics2,
private toasterService: ToasterService, private i18nService: I18nService,
private cryptoService: CryptoService, private apiService: ApiService) { }
async submit() {
if (this.email == null || this.email === '') {
this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'),
this.i18nService.t('emailRequired'));
return;
}
if (this.email.indexOf('@') === -1) {
this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'),
this.i18nService.t('invalidEmail'));
return;
}
if (this.masterPassword == null || this.masterPassword === '') {
this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'),
this.i18nService.t('masterPassRequired'));
return;
}
if (this.masterPassword.length < 8) {
this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'),
this.i18nService.t('masterPassLength'));
return;
}
if (this.masterPassword !== this.confirmMasterPassword) {
this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'),
this.i18nService.t('masterPassDoesntMatch'));
return;
}
try {
this.formPromise = this.register();
await this.formPromise;
this.analytics.eventTrack.next({ action: 'Registered' });
this.toasterService.popAsync('success', null, this.i18nService.t('newAccountCreated'));
this.router.navigate(['login']);
} catch { }
}
2018-02-24 20:41:10 +01:00
togglePassword(confirmField: boolean) {
this.analytics.eventTrack.next({ action: 'Toggled Master Password on Register' });
this.showPassword = !this.showPassword;
document.getElementById(confirmField ? 'masterPasswordRetype' : 'masterPassword').focus();
}
2018-01-31 20:19:21 +01:00
private async register() {
this.email = this.email.toLowerCase();
const key = this.cryptoService.makeKey(this.masterPassword, this.email);
const encKey = await this.cryptoService.makeEncKey(key);
const hashedPassword = await this.cryptoService.hashPassword(this.masterPassword, key);
const request = new RegisterRequest(this.email, hashedPassword, this.hint, encKey.encryptedString);
await this.apiService.postRegister(request);
}
}