I'm attempting to customize the navigation bar to display "Sign In" and "Sign Up" buttons only when the user is not signed in, and show the message and profile navigation items when the user is signed in.
Below is the provided code snippet:
Token Service
...
SetToken(token) {
this.cookieService.set('chat_token', token);
}
GetToken() {
return this.cookieService.get('chat_token');
}
DeleteToken() {
this.cookieService.delete('chat_token');
}
GetPayload() {
const token = this.GetToken();
let payload;
if (token) {
payload = token.split('.')[1];
payload = JSON.parse(window.atob(payload));
}
return payload.data;
}
}
Authguard Service
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<boolean> | Promise<boolean> | boolean {
const token = this.tokenService.GetToken();
if (token) {
return true;
} else {
this.router.navigate(['/']);
return false;
}
}
}
Header.ts
constructor(public nav: HeaderService, private tokenService: TokenService,
private router: Router,
private usersService: UsersService,
private msgService: MessageService
) {
this.socket = io('http://localhost:3000');
}
ngOnInit() {
this.user = this.tokenService.GetPayload();
...
HTML
<nav *ngIf="nav.visible" class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">
<img src="../assets/images/close.png" class="img-fluid" style="width:50px; height:50px" alt="">
</a>
<button class="navbar-toggler" type="button"
(click)="toggleNavbar()">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse"
[ngClass]="{ 'show': navbarOpen }">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" routerLink="/home" >Item 1</a>
</li>
<li class="nav-item signin">
<a class="nav-link" routerLink="/sign-in" >Sign in</a>
</li>
<li class="nav-item signup">
<a class="nav-link" routerLink="/sign-up" >Sign Up</a>
</li>
<li class="nav-item dropdown ml-auto" appdropdown #r="appdropdown">
<a class="nav-link dropdown-toggle toolbar-image" href="" onclick="return false;" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="material-icons">forum</i>
<span>2</span>
</a>
<div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdownMenuLink">
<div class="sidebar-scroll" id="list-group">
<a class="dropdown-item chat" href="#">
<img src="http://emilcarlsson.se/assets/donnapaulsen.png" class="rounded-circle">
<div class='message'>
<h3 class="d-xs-none username">Jessica</h3>
<p class="d-xs-none letter">StanIsLove aaaaaaaaaaaaaaaaa.</p>
</div>
<p>17m ago</p>
</a>
</div>
</div>
</li>
<li class="nav-item dropdown ml-auto" appdropdown #r="appdropdown">
<a class="nav-link dropdown-toggle" href="" onclick="return false;" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<img src="http://emilcarlsson.se/assets/donnapaulsen.png" class="rounded-circle profile">
</a>
<div class="dropdown-menu dropdown-menu-right avatar" aria-labelledby="navbarDropdownMenuLink">
<div class="sidebar-scroll" id="list-group">
<a class="dropdown-item chat" href="#">
<span class="d-xs-none username">Profile</span>
</a>
<a class="dropdown-item chat" href="#">
<span class="d-xs-none username">Settings</span>
</a>
<a class="dropdown-item chat" href="#">
<span class="d-xs-none username">Logout</span>
</a>
</div>
</div>
</li>
</ul>
</div>
</nav>
I believe integrating this.tokenService.GetPayload() into the HTML and utilizing "ngIf" should work. However, I am uncertain. How can I address this issue to only display "Sign In" and "Sign Up" links for non-signed-in users, and show the last two drop-downs exclusively for signed-in individuals?