canActivate is added on admin Page.
In the home page we have link admin.
The problem here is on the first click , Though I'm admin I'm not routed to the page But on the second click, I'm routed.
Why is this happening ?
dataservice.ts
getUsersData() : Observable<Object[]> {
return this.http.get<Object[]>("https://holder.com/users")
}
authservice
admin : boolean
constructor(private dataService : DataService) { }
isAdmin() {
this.dataService.getusersData().subscribe(data => {
if(data.includes("Joseph")) {
return this.admin = true ;
}
else {
return this.admin = false ;
}
})
return this.admin
}
authguardservice
canActivate(route : ActivatedRouteSnapshot,
state : RouterStateSnapshot) : Observable<boolean> | Promise<boolean> | boolean {
const isAdmin = this.authService.isAdmin()
if(isAdmin) {
return true
}
else {
this.router.navigate(['/'])
}
}