Skip to content

Commit

Permalink
notify navbar component about login/logout using eventemiiter
Browse files Browse the repository at this point in the history
  • Loading branch information
asifpy committed Feb 5, 2018
1 parent 93502f7 commit 6b4bcd6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

<img width="500" src="https://github.com/asifpy/landlord/blob/master/client/src/assets/images/dashboard.png" border="0" />
17 changes: 13 additions & 4 deletions client/src/app/core/navbar/navbar.component.ts
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
3 changes: 3 additions & 0 deletions client/src/app/core/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { environment } from '../../../environments/environment';
export class AuthService {

private loginUrl = `${environment.apiUrl}auth/login/`;
@Output() authChanged: EventEmitter<boolean> = new EventEmitter<boolean>();

constructor(private http: HttpClient) {}

Expand All @@ -23,13 +24,15 @@ 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'];
});
}

logout() {
localStorage.removeItem('token');
this.authChanged.emit(false);
}

public isLoggedIn() {
Expand Down

0 comments on commit 6b4bcd6

Please sign in to comment.