diff --git a/src/app/authentication.guard.ts b/src/app/authentication.guard.ts index cec297d1bb377e163c193e08af9f23227feccee4..89bd7879043efd0772e928b38d6b5c1d43b933c2 100644 --- a/src/app/authentication.guard.ts +++ b/src/app/authentication.guard.ts @@ -26,7 +26,7 @@ export class AuthenticationGuard implements CanActivate { // Allow access when user is logged in AND // the route doesn't require a specific role OR // the user's role is one of the required roles - if (user && (!requiredRoles || requiredRoles.includes(user.role))) { + if (user && (!requiredRoles || requiredRoles.includes(user.userRole))) { return true; } diff --git a/src/app/authentication.service.ts b/src/app/authentication.service.ts index 4080d7ecade69225bcf436f542f3055531b5355a..04a5bfa6365ae6956aa5a927b5f24617a715eb49 100644 --- a/src/app/authentication.service.ts +++ b/src/app/authentication.service.ts @@ -5,6 +5,7 @@ import { of } from 'rxjs/observable/of'; import { UserRole } from './user-roles.enum'; import { DataStoreService } from './data-store.service'; import { HttpClient, HttpHeaders } from '@angular/common/http'; +import { AuthProvider } from './auth-provider'; // TODO: connect to API // TODO: persist user data (shouldn't get lost on page refresh) @@ -28,7 +29,7 @@ export class AuthenticationService { } login(email: string, password: string, role: UserRole): Observable<boolean> { - this.user = new User(1, '', email, role, 'TOKEN'); + this.user = new User('userId1', 'loginId1', AuthProvider.ARSNOVA, 'TOKEN', role); // Store user data in local storage to retain the data when the user reloads the page this.dataStoreService.set(this.STORAGE_KEY, JSON.stringify(this.user)); @@ -36,11 +37,8 @@ export class AuthenticationService { } guestLogin() { - this.http.post<string>(this.apiBaseUrl + this.apiAuthUrl + this.apiLoginUrl + '/guest', this.httpHeaders).subscribe(token => { - if (token != null) { - this.user = new User(1337, '', '', UserRole.PARTICIPANT, token); - return of(true); - } + this.http.post<string>(this.apiBaseUrl + this.apiAuthUrl + this.apiLoginUrl + '/guest', this.httpHeaders).subscribe(result => { + this.user = new User(result['userId'], result['loginId'], result['authProvider'], result['token'], UserRole.PARTICIPANT); }); return of(false); } @@ -68,7 +66,7 @@ export class AuthenticationService { } getRole(): UserRole { - return this.isLoggedIn() ? this.user.role : undefined; + return this.isLoggedIn() ? this.user.userRole : undefined; } } diff --git a/src/app/user.ts b/src/app/user.ts index 4805dcc8b0cf8a33952f0846272bbd647a7b9c95..88885094ad742876bc0883a5fc4813648bea40d5 100644 --- a/src/app/user.ts +++ b/src/app/user.ts @@ -1,8 +1,18 @@ import { AuthProvider } from './auth-provider'; +import { UserRole } from './user-roles.enum'; export class User { userId: string; loginId: string; authProvider: AuthProvider; token: string; + userRole: UserRole; + + constructor(userId: string, loginId: string, authProvider: AuthProvider, token: string, userRole: UserRole) { + this.userId = userId; + this.loginId = loginId; + this.authProvider = authProvider; + this.token = token; + this.userRole = userRole; + } }