In an attempt to secure my Angular application's routes, I decided to create a canActivate method that would check the user's status. My backend is based on Firebase, and user authentication, login, and sign up functionalities are all handled through Firebase. Here is the code snippet that I came up with:
constructor (private router: Router, private auth: AngularFireAuth) {
this.y = false;
this.auth.authState.subscribe(x => {
if(x == null){
console.log("null if condition") // for tracking program flow
this.y = false;
}
else{
console.log("else condition") // for tracking program flow
this.y= true;
}
console.log("inside fun",this.y); // for tracking program flow
})
}
canActivate(route:any, state: RouterStateSnapshot) {
console.log("canActivate", this.y); // for tracking program flow
if (this.y)
{ return true}
else
{
this.router.navigate(['/login'], {queryParams: {returnUrl: state.url}});
return false;
}
}
I need some assistance in understanding a situation where even though the y value changes within the subscription block when the user is logged in, it reverts back to its initialized value once outside of it. Any help would be greatly appreciated.