From cd3015266d4dfe24a70e26d8ce0f021851f1884f Mon Sep 17 00:00:00 2001
From: Lukas Kimpel <lukas.kimpel@mni.thm.de>
Date: Wed, 14 Mar 2018 13:06:15 +0100
Subject: [PATCH] Add api connection for password reset Add api connection for
 register

---
 src/app/authentication.service.ts             | 23 +++++++++++++++----
 .../password-reset.component.ts               |  9 +++-----
 src/app/register/register.component.ts        | 11 ++++-----
 3 files changed, 25 insertions(+), 18 deletions(-)

diff --git a/src/app/authentication.service.ts b/src/app/authentication.service.ts
index 74f2dcd89..7d987594c 100644
--- a/src/app/authentication.service.ts
+++ b/src/app/authentication.service.ts
@@ -13,12 +13,15 @@ export class AuthenticationService {
   private readonly STORAGE_KEY: string = 'USER';
   private user: User;
   private apiBaseUrl = 'https://arsnova-staging.mni.thm.de/api';
+  private apiV2Url = 'https://arsnova-staging.mni.thm.de/v2';
   private apiAuthUrl = '/auth';
   private apiLoginUrl = '/login';
+  private apiUserUrl = '/user';
+  private apiRegisterUrl = '/register';
   private apiRegisteredUrl = '/registered';
+  private apiResetPasswordUrl = '/resetpassword';
   private httpOptions = {
-    headers: new HttpHeaders({
-    })
+    headers: new HttpHeaders({})
   };
 
   constructor(private dataStoreService: DataStoreService,
@@ -44,12 +47,22 @@ export class AuthenticationService {
     return this.http.post<ClientAuthentication>(connectionUrl, null, this.httpOptions);
   }
 
-  register(email: string, password: string): Observable<boolean> {
-    return of(true);
+  register(email: string, password: string): Observable<ClientAuthentication> {
+    const connectionUrl: string = this.apiBaseUrl + this.apiRegisterUrl;
+
+    return this.http.post<ClientAuthentication>(connectionUrl, {
+      loginId: email,
+      password: password
+    }, this.httpOptions);
   }
 
   resetPassword(email: string): Observable<boolean> {
-    return of(true);
+    const connectionUrl: string = this.apiV2Url + this.apiUserUrl + email + this.apiResetPasswordUrl;
+
+    return this.http.post<boolean>(connectionUrl, {
+      key: null,
+      password: null
+    }, this.httpOptions);
   }
 
   logout() {
diff --git a/src/app/password-reset/password-reset.component.ts b/src/app/password-reset/password-reset.component.ts
index 36e2a9c27..ec6b9b85b 100644
--- a/src/app/password-reset/password-reset.component.ts
+++ b/src/app/password-reset/password-reset.component.ts
@@ -38,12 +38,9 @@ export class PasswordResetComponent implements OnInit {
 
     if (!this.usernameFormControl.hasError('required') && !this.usernameFormControl.hasError('email')) {
       this.authenticationService.resetPassword(username).subscribe(result => {
-        if (result) {
-          this.notificationService.show('Password was reset. Please check your mail!');
-          this.dialogRef.close();
-        } else {
-          this.notificationService.show('Could not reset your password. Is your email address correct?');
-        }
+        // ToDo: Check what /resetpassword returns and check for potential errors
+        this.notificationService.show('Password was reset. Please check your mail!');
+        this.dialogRef.close();
       });
     } else {
       this.notificationService.show('Please fit the requirements shown above.');
diff --git a/src/app/register/register.component.ts b/src/app/register/register.component.ts
index 85fd4fe2f..854da7a19 100644
--- a/src/app/register/register.component.ts
+++ b/src/app/register/register.component.ts
@@ -55,17 +55,14 @@ export class RegisterComponent implements OnInit {
   ngOnInit() {
   }
 
-  register(username: string, password1: string, password2: string): void {
+  register(username: string, password1: string): void {
     if (!this.usernameFormControl.hasError('required') && !this.usernameFormControl.hasError('email') &&
       !this.password1FormControl.hasError('required') &&
       !this.password2FormControl.hasError('required') && !this.password2FormControl.hasError('passwordIsEqual')) {
       this.authenticationService.register(username, password1).subscribe(result => {
-        if (result) {
-          this.notificationService.show('Successfully registered. Please check your mail!');
-          this.dialogRef.close();
-        } else {
-          this.notificationService.show('Oops! Something went wrong on our side...');
-        }
+        // ToDo: Check what /api/register returns and check for potential errors
+        this.notificationService.show('Successfully registered. Please check your mail!');
+        this.dialogRef.close();
       });
     } else {
       this.notificationService.show('Please fit the requirements shown above.');
-- 
GitLab