diff --git a/README.md b/README.md index 6f1f018..1e1513d 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,6 @@ cd client Make sure you have the [Angular CLI](https://github.com/angular/angular-cli#installation) installed globally, then run `npm install` to resolve all dependencies (might take a minute). Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The app will automatically reload if you change any of the source files. -Configure the `apiUrl` with your running backend service in ./src/environments/environment.ts +Configure the `apiUrl` with your running backend service in client/src/environments/environment.ts diff --git a/client/src/app/core/navbar/navbar.component.ts b/client/src/app/core/navbar/navbar.component.ts index 992060f..ae16544 100644 --- a/client/src/app/core/navbar/navbar.component.ts +++ b/client/src/app/core/navbar/navbar.component.ts @@ -1,25 +1,34 @@ import { Component, OnInit, OnDestroy } from '@angular/core'; import { Router } from '@angular/router'; +import { Subscription } from 'rxjs/Subscription'; + import { AuthService } from '../services/auth.service'; @Component({ selector: 'app-navbar', templateUrl: './navbar.component.html' }) -export class NavbarComponent implements OnInit { +export class NavbarComponent implements OnInit, OnDestroy { isCollapsed: boolean; loginLogoutText = 'Login'; + sub: Subscription; constructor( private router: Router, private authservice: AuthService) { } ngOnInit() { - if (this.authservice.isLoggedIn) { - this.loginLogoutText = 'Logout'; - } + this.sub = this.authservice.authChanged + .subscribe((loggedIn: boolean) => { + this.setLoginLogoutText(); + }, + (err: any) => console.log(err)); + } + + ngOnDestroy() { + this.sub.unsubscribe(); } loginOrOut() { diff --git a/client/src/app/core/services/auth.service.ts b/client/src/app/core/services/auth.service.ts index f905927..1cc295e 100644 --- a/client/src/app/core/services/auth.service.ts +++ b/client/src/app/core/services/auth.service.ts @@ -13,6 +13,7 @@ import { environment } from '../../../environments/environment'; export class AuthService { private loginUrl = `${environment.apiUrl}auth/login/`; + @Output() authChanged: EventEmitter = new EventEmitter(); constructor(private http: HttpClient) {} @@ -23,6 +24,7 @@ export class AuthService { if (user && user['token']) { // store user details and jwt token in local storage to keep user logged in between page refreshes localStorage.setItem('token', user['token']); + this.authChanged.emit(true); } return user['token']; }); @@ -30,6 +32,7 @@ export class AuthService { logout() { localStorage.removeItem('token'); + this.authChanged.emit(false); } public isLoggedIn() {