Skip to content

Commit 9a7ecbd

Browse files
committed
Hides logout button when not logged in
1 parent bdb9296 commit 9a7ecbd

5 files changed

Lines changed: 31 additions & 14 deletions

File tree

src/app/authentication/service/authentication.service.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { HttpClient } from '@angular/common/http';
22
import { Injectable } from '@angular/core';
33
import { ApiResponse } from '@app/api/model/api-response';
44
import { environment } from '@environments/environment';
5-
import { Observable } from 'rxjs';
5+
import { BehaviorSubject, Observable } from 'rxjs';
66
import { map } from 'rxjs/operators';
77
import { LoginStatus } from '../model/login-status';
88
import { User } from '../model/user';
@@ -14,17 +14,24 @@ export class AuthenticationService {
1414

1515
private loginUrl = environment.apiUrl + "/login";
1616

17-
private user: User = new User();
18-
1917
private userKey = 'user';
2018

19+
private userSubject: BehaviorSubject<User>;
20+
21+
private user: Observable<User>;
22+
2123
constructor(
2224
private http: HttpClient
2325
) {
2426
const localUser = localStorage.getItem(this.userKey);
2527
if (localUser) {
26-
this.user = JSON.parse(localUser);
28+
const readUser = JSON.parse(localUser);
29+
this.userSubject = new BehaviorSubject<User>(readUser);
30+
} else {
31+
this.userSubject = new BehaviorSubject<User>(new User());
2732
}
33+
34+
this.user = this.userSubject.asObservable();
2835
}
2936

3037
public login(username: string, password: string): Observable<User> {
@@ -34,10 +41,15 @@ export class AuthenticationService {
3441
}
3542

3643
public logout() {
37-
this.user = new User();
44+
this.userSubject.next(new User());
45+
localStorage.removeItem(this.userKey);
3846
}
3947

4048
public getUser(): User {
49+
return this.userSubject.value;
50+
}
51+
52+
public getUserObservable(): Observable<User> {
4153
return this.user;
4254
}
4355

@@ -57,8 +69,8 @@ export class AuthenticationService {
5769
}
5870
}
5971

60-
this.user = loggedUser;
61-
return this.user;
72+
this.userSubject.next(loggedUser);
73+
return this.getUser();
6274
}
6375

6476
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
<button (click)="onLogout()" [disabled]="!isAbleToLogout()" type="button" class="btn btn-default" aria-label="Logout">
1+
<button (click)="onLogout()" type="button" class="btn btn-default" aria-label="Logout">
22
<fa-icon [icon]="logoutIcon"></fa-icon>
33
</button>

src/app/login/containers/logout-button/logout-button.component.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,4 @@ export class LogoutButtonComponent {
2424
this.router.navigate([this.loginUrl]);
2525
}
2626

27-
public isAbleToLogout(): boolean {
28-
return this.authenticationService.getUser().logged;
29-
}
30-
3127
}

src/app/navigation/navigation-menu/navigation-menu.component.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<nav id="navbar-main" class="navbar navbar-expand-md navbar-light bg-light">
22
<div class="container-fluid">
33
<div class="navbar-brand">{{title}}</div>
4-
<logout-button></logout-button>
4+
<ng-template [ngIf]="loggedIn">
5+
<logout-button></logout-button>
6+
</ng-template>
57
<ul class="nav navbar-nav ms-auto">
68
<navigation-dropdown [links]="links"></navigation-dropdown>
79
</ul>

src/app/navigation/navigation-menu/navigation-menu.component.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Component, Input } from '@angular/core';
2+
import { AuthenticationService } from '@app/authentication/service/authentication.service';
23
import { MenuLink } from '../model/menu-link';
34

45
@Component({
@@ -12,6 +13,12 @@ export class NavigationMenuComponent {
1213

1314
@Input() title: String = '';
1415

15-
constructor() { }
16+
public loggedIn: boolean = false;
17+
18+
constructor(
19+
private authenticationService: AuthenticationService
20+
) {
21+
this.authenticationService.getUserObservable().subscribe(u => { this.loggedIn = u.logged });
22+
}
1623

1724
}

0 commit comments

Comments
 (0)