Using Angular router, when user navigates to a certain page of the SPA, I want to check if user is authenticated, by calling a service in ngOninit
in order to display the sign-in or sign-out button as the case may be. But ngOninit
doesn't fire, and only fires when I navigate away from the page in question. What could be the cause of that, am I missing lifecycle hooks lessons?
export class HomeComponent implements OnInit {
loggedIn? = false;
constructor(private authservice: AuthenticationService) {}
ngOnInit(): void {
this.authservice.loadUserCredentials()
.subscribe({
next: res => {
console.log('res ',res)
this.loggedIn = res;
}
})
}
```