22

I'm unable to selectively display links on my nav-bar. Based on who logs in (user or admin), I want to change which link shows on my nav-bar.

Below is the code for one such instance where the user/admin logs out.

In navbar.component.html -

<li *ngIf="authService.userLoggedIn()== true && authService.adminLoggedIn() == false" 
       [routerLinkActive]="['active']" [routerLinkActiveOptions]="{exact:true}"> 
  <a (click)="onUserLogoutClick()" href="#">Logout</a>
</li>

<li *ngIf="(authService.adminLoggedIn() == true) && (authService.userLoggedIn() == false)" 
      [routerLinkActive]="['active']" [routerLinkActiveOptions]="{exact:true}">
   <a (click)="onAdminLogoutClick()" href="#">Logout</a>
</li>

Both authService.adminLoggedIn() and authService.userLoggedIn() return tokenNotExpired;

Below is the relevant code in the navbar.component.ts -

 onUserLogoutClick() {
   this.authService.userLogout();
   this.flashMessage.show('You are now logged out', {cssClass: 'alert-success', timeout: 3000});
   this.router.navigate(['/login']);
   return false;   
 }

 onAdminLogoutClick() {
   this.authService.adminLogout();
   this.flashMessage.show('Administrator now logged out', {cssClass: 'alert-success', timeout: 3000});
   this.router.navigate(['/admin']);
   return false;   
 }

The authService.adminLogout() and authService.userLogout() just clears the token stored in local storage.

I apologize in advance if the mistake that I've made is silly. I'm new to Angular.

5
  • 1
    revise your post to be more meaning ful Commented Mar 25, 2017 at 21:10
  • how are you identifying if admin logged in or user logged in Commented Mar 25, 2017 at 21:13
  • Can you please explain whether authService.adminLoggedIn() and authService.userLoggedIn() return tokenNotExpired but you are testing on boolean true? May I also ask whether you maybe prefer to make the logic as part of the JS code rather than in the template? It does not seem to have a template impact. Commented Mar 25, 2017 at 21:19
  • I have different components for admin and user login on the navbar of my landing page. Commented Mar 25, 2017 at 21:20
  • @BenDadsetan, I referred to github.com/auth0/angular2-jwt to learn about angular2-jwt In the section of Checking Authentication to Hide/Show Elements and Handle Routing they also check the condition in *ngIf, I even tried without testing it to boolean value but it still didn't work. Link - github.com/auth0/… Commented Mar 25, 2017 at 21:23

3 Answers 3

27

You can move these multiline conditions and complex conditions to your component method as below

showLogout(){
    if(this.authService.userLoggedIn()== true && this.authService.adminLoggedIn() == false)
        return true;
    else
        return false;
}

Also, as the angular4 has *ngIf and else you can use it as

 <div *ngIf="showLogout();then userLogout else adminlogout"></div>

<ng-template #userLogout><a (click)="onUserLogoutClick()" href="#">Logout</a></li></ng-template>
<ng-template #adminlogout><a (click)="onAdminLogoutClick()" href="#">Logout</a></li></ng-template>
Sign up to request clarification or add additional context in comments.

7 Comments

Why not simplify the showLogout method? return this.authService.userLoggedIn() && !this.authService.adminLoggedIn().
@Aravind, Your solution works, but when nobody is logged in, the nav-bar is still showing the logout link for (onAdminLogoutClick). The tokens that I generate for admin have the ID of 'admin_token', and the tokens for the user have the ID of 'user_token'. So when I call authService.adminLoggedIn() it returns tokenNotExpired('admin_token') and for authService.userLoggedIn() it returns tokenNotExpired('user_token'). This is how I determine whether the user or the administrator is logged in.
You need to check if the token is available in your method
So is every thing is fine or u need more help
It's working now. I made some edits. Before checking the method that you suggested (showLogout()), I added another method (clientLoggedIn()) to check if someone (either user or admin) is logged in or not. The HTML code - <div *ngIf="clientLoggedIn()"> <div *ngIf="showLogout();then userLogout else adminLogout"></div> <ng-template.... </ng-template> </div> clientLoggedIn(){ if(localStorage.getItem('user_token')=== null && localStorage.getItem('admin_token')=== null) return false else return true Thanks, @Aravind.
|
4

authService.userLoggedIn() or authService.adminLoggedIn() in your expression wouldn't keep your template informed about who is logged in as your function is invoked only once.

Try make them getter in your service:

  get userLoggedIn(): boolean {
    return this.who.user; // your logic
  }

Then in your template:

<li *ngIf="authService.userLoggedIn && !authService.adminLoggedIn"...

Comments

1

I think should be create a boolean properties in component and using it. In stead of writing function or complex expression in template. Function in template reduce performance. Complex expression make it not readble and maintainable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.