After a successful login, the token is generated and every component gets rendered except the top bar. I don't understand what's happening. Below is my code:
Auth service
private authStatusListener = new Subject<boolean>();
loginUser(authData) {
this.http.post<{ token: string }>(this.url + '/authenticate', authData).subscribe(response => {
const token = response.token;
if(token) {
this.isAuthenticated = true;
this.authStatusListener.next(true);
this.saveAuthData(token);
this.router.navigate(['/admin/dashboard']);
}
});
}
getAuthStatusListener() {
return this.authStatusListener.asObservable();
}
Navbar Component
userIsAuthenticated = false;
private authListenerSubs: Subscription;
ngOnInit() {
this.authListenerSubs = this.adminAuth.getAuthStatusListener().subscribe(isAuthenticated => {
this.userIsAuthenticated = isAuthenticated;
});
}
ngOnDestroy() {
this.authListenerSubs.unsubscribe();
}
Navbar HTML
<mat-toolbar color="primary" *ngIf="userIsAuthenticated">
<span><a [routerLink]="['/admin/dashboard']" class="tabs">Admin</a></span>
<span class="space-right"></span>
<ul>
<li><button mat-button [routerLink]="['/admin/dashboard']" routerLinkActive="highlight" class="tabs">Dashboard</button></li>
<li><button mat-button [routerLink]="['/admin/create_category']" routerLinkActive="highlight" class="tabs">Exam Category</button></li>
<li><button mat-button [routerLink]="['/admin/create_question_category']" routerLinkActive="highlight" class="tabs">Question Category</button></li>
<li><button mat-button [routerLink]="['/admin/create_section']" routerLinkActive="highlight" class="tabs">Section Marks</button></li>
<li><button mat-button [routerLink]="['/admin/exam']" routerLinkActive="highlight" class="tabs">Exam</button></li>
<li><button mat-button [routerLink]="['/admin/question']" routerLinkActive="highlight" class="tabs">Question</button></li>
<li><button mat-icon-button class="tabs" (click)="onLogout()"><mat-icon>power_settings_new</mat-icon></button></li>
</ul>
</mat-toolbar>
<main><router-outlet></router-outlet></main>
What am understanding is that userIsAuthenticated
is returning false
though in service am setting it to true
.
Please tell me way for modifying this code or any other way.
P.S. After Logout this navbar will not appear