From c9a7cef4062404c8f03b02657242c8b084519ff0 Mon Sep 17 00:00:00 2001
From: Lukas Kimpel <lukas.kimpel@mni.thm.de>
Date: Mon, 19 Mar 2018 21:37:50 +0100
Subject: [PATCH] Fix login routes to have logic in AuthenticationService

---
 src/app/authentication.service.ts | 29 ++++++++++++++++++++++-------
 src/app/login/login.component.ts  | 11 ++---------
 2 files changed, 24 insertions(+), 16 deletions(-)

diff --git a/src/app/authentication.service.ts b/src/app/authentication.service.ts
index c196dfc51..2375fb9a6 100644
--- a/src/app/authentication.service.ts
+++ b/src/app/authentication.service.ts
@@ -6,8 +6,6 @@ import { UserRole } from './user-roles.enum';
 import { DataStoreService } from './data-store.service';
 import { HttpClient, HttpHeaders } from '@angular/common/http';
 import { ClientAuthentication } from './client-authentication';
-import { AuthProvider } from './auth-provider';
-import { ClientAuthentication } from './client-authentication';
 
 // TODO: connect to API
 @Injectable()
@@ -34,19 +32,19 @@ export class AuthenticationService {
     }
   }
 
-  login(email: string, password: string): Observable<ClientAuthentication> {
+  login(email: string, password: string, userRole: UserRole): Observable<boolean> {
     const connectionUrl: string = this.apiBaseUrl + this.apiAuthUrl + this.apiLoginUrl + this.apiRegisteredUrl;
 
-    return this.http.post<ClientAuthentication>(connectionUrl, {
+    return this.checkLogin(this.http.post<ClientAuthentication>(connectionUrl, {
       loginId: email,
       password: password
-    }, this.httpOptions);
+    }, this.httpOptions), userRole);
   }
 
-  guestLogin(): Observable<ClientAuthentication> {
+  guestLogin(): Observable<boolean> {
     const connectionUrl: string = this.apiBaseUrl + this.apiAuthUrl + this.apiLoginUrl + '/guest';
 
-    return this.http.post<ClientAuthentication>(connectionUrl, null, this.httpOptions);
+    return this.checkLogin(this.http.post<ClientAuthentication>(connectionUrl, null, this.httpOptions), UserRole.PARTICIPANT);
   }
 
   register(email: string, password: string): Observable<ClientAuthentication> {
@@ -94,4 +92,21 @@ export class AuthenticationService {
     return this.user.token;
   }
 
+  checkLogin(clientAuthentication: Observable<ClientAuthentication>, userRole: UserRole): Observable<boolean> {
+    return clientAuthentication.map(result => {
+      if (result) {
+        this.user = new User(
+          result.userId,
+          result.loginId,
+          result.authProvider,
+          result.token, userRole);
+        return true;
+      } else {
+        return false;
+      }
+    }).catch(() => {
+      return of(false);
+    });
+  }
+
 }
diff --git a/src/app/login/login.component.ts b/src/app/login/login.component.ts
index f4156f3e1..cc6bcb187 100644
--- a/src/app/login/login.component.ts
+++ b/src/app/login/login.component.ts
@@ -45,7 +45,7 @@ export class LoginComponent implements OnInit {
 
     if (!this.usernameFormControl.hasError('required') && !this.usernameFormControl.hasError('email') &&
       !this.passwordFormControl.hasError('required')) {
-      this.authenticationService.login(username, password).subscribe(loginSuccessful => this.checkLogin(loginSuccessful));
+      this.authenticationService.login(username, password, this.role).subscribe(loginSuccessful => this.checkLogin(loginSuccessful));
     } else {
       this.notificationService.show('Please fit the requirements shown above.');
     }
@@ -55,15 +55,8 @@ export class LoginComponent implements OnInit {
     this.authenticationService.guestLogin().subscribe(loginSuccessful => this.checkLogin(loginSuccessful));
   }
 
-  private checkLogin(loginSuccessful: ClientAuthentication) {
+  private checkLogin(loginSuccessful: boolean) {
     if (loginSuccessful) {
-      const user: User = new User(
-        loginSuccessful.userId,
-        loginSuccessful.loginId,
-        loginSuccessful.authProvider,
-        loginSuccessful.token,
-        this.role);
-      this.authenticationService.setUser(user);
       this.notificationService.show('Login successful!');
       if (this.role === UserRole.CREATOR) {
         this.router.navigate(['creator']);
-- 
GitLab