I've encountered an issue with my canActivate method. After signing in on my website, I set a boolean variable to true. However, when I refresh the page, the value resets back to false. It is crucial for me to maintain this value even after a page refresh.
Here are the relevant codes:
export class AuthService{
loggedin = false;
isAuthenticated(){
return this.loggedin;
}
login(){
this.loggedin = true;
}
logout(){
this.loggedin = false;
}
}
and the canActivate method:
@Injectable()
export class AuthGuard implements CanActivate{
constructor(private authService: AuthService, private router: Router){}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean{
if(this.authService.isAuthenticated()){
return true;
}else{
this.router.navigate(['/']);
}
}
}
After logging in, I call the login function to set the value to true. Unfortunately, upon reloading the page, the value reverts back to false. The only way I found to change this value is by calling the logout method. Can you help me understand why this happens and how can I fix it?