From cc0200c24521a5697e19336fe71c25c2ea066605 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lukas=20Mau=C3=9F?= <lukas.mauss@mni.thm.de>
Date: Tue, 22 Jan 2019 13:09:14 +0100
Subject: [PATCH] Add new formControl for checking if emails are matching

---
 .../_dialogs/register/register.component.ts   | 21 ++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/src/app/components/home/_dialogs/register/register.component.ts b/src/app/components/home/_dialogs/register/register.component.ts
index fb5869355..72bd99337 100644
--- a/src/app/components/home/_dialogs/register/register.component.ts
+++ b/src/app/components/home/_dialogs/register/register.component.ts
@@ -29,6 +29,23 @@ function validatePassword(password1: FormControl) {
   };
 }
 
+function validateEmail(email1: FormControl) {
+  return (formControl: FormControl) => {
+    const email1Value = email1.value;
+    const email2Value = formControl.value;
+
+    if (email1Value !== email2Value) {
+      return {
+        emailIsEqual: {
+          isEqual: false
+        }
+      };
+    } else {
+      return null;
+    }
+  };
+}
+
 @Component({
   selector: 'app-register',
   templateUrl: './register.component.html',
@@ -37,6 +54,7 @@ function validatePassword(password1: FormControl) {
 export class RegisterComponent implements OnInit {
 
   usernameFormControl = new FormControl('', [Validators.required, Validators.email]);
+  username2FormControl = new FormControl('', [Validators.required, validateEmail(this.usernameFormControl)]);
   password1FormControl = new FormControl('', [Validators.required]);
   password2FormControl = new FormControl('', [Validators.required, validatePassword(this.password1FormControl)]);
 
@@ -58,13 +76,14 @@ export class RegisterComponent implements OnInit {
 
   register(username: string, password: string): void {
     if (!this.usernameFormControl.hasError('required') && !this.usernameFormControl.hasError('email') &&
+      !this.username2FormControl.hasError('required') && !this.username2FormControl.hasError('emailIsEqual') &&
       !this.password1FormControl.hasError('required') &&
       !this.password2FormControl.hasError('required') && !this.password2FormControl.hasError('passwordIsEqual')) {
       this.authenticationService.register(username, password).subscribe(() => {
         this.translationService.get('register.register-successful').subscribe(message => {
           this.notificationService.show(message);
         });
-        this.dialogRef.close({username: username, password: password});
+        this.dialogRef.close({ username: username, password: password });
       });
     } else {
       this.translationService.get('register.register-unsuccessful').subscribe(message => {
-- 
GitLab