mirror of
https://github.com/bitwarden/browser
synced 2025-12-16 08:13:42 +00:00
share components with jslib
This commit is contained in:
78
src/angular/components/register.component.ts
Normal file
78
src/angular/components/register.component.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import { Router } from '@angular/router';
|
||||
|
||||
import { ToasterService } from 'angular2-toaster';
|
||||
import { Angulartics2 } from 'angulartics2';
|
||||
|
||||
import { RegisterRequest } from '../../models/request/registerRequest';
|
||||
|
||||
import { ApiService } from '../../abstractions/api.service';
|
||||
import { AuthService } from '../../abstractions/auth.service';
|
||||
import { CryptoService } from '../../abstractions/crypto.service';
|
||||
import { I18nService } from '../../abstractions/i18n.service';
|
||||
|
||||
export class RegisterComponent {
|
||||
email: string = '';
|
||||
masterPassword: string = '';
|
||||
confirmMasterPassword: string = '';
|
||||
hint: string = '';
|
||||
showPassword: boolean = false;
|
||||
formPromise: Promise<any>;
|
||||
|
||||
protected successRoute = 'login';
|
||||
|
||||
constructor(protected authService: AuthService, protected router: Router,
|
||||
protected analytics: Angulartics2, protected toasterService: ToasterService,
|
||||
protected i18nService: I18nService, protected cryptoService: CryptoService,
|
||||
protected 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([this.successRoute]);
|
||||
} catch { }
|
||||
}
|
||||
|
||||
togglePassword(confirmField: boolean) {
|
||||
this.analytics.eventTrack.next({ action: 'Toggled Master Password on Register' });
|
||||
this.showPassword = !this.showPassword;
|
||||
document.getElementById(confirmField ? 'masterPasswordRetype' : 'masterPassword').focus();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user