I'm trying to monitor an api response to know if the user is currently logged in or not. To do so, I'm sending a get request to /api/status along with the http-only cookie. the endpoint should respond with my userid if I am currently logged in. If am logged in, angular should not show the login/register links. My code is as follows:
export class HeaderComponent implements OnInit {
authService = inject(AuthService)
isLoggedIn$ = new BehaviorSubject<Boolean>(false);
authenticationStatus!: Observable<HttpResponse<object>>;
constructor() {
this.authenticationStatus = this.authService.authStatus()
}
ngOnInit(): void {
this.authenticationStatus.subscribe({
next: (res: any) => {
if (res.body) {
const userId = res.body.message;
console.log("User ID: ", userId);
this.isLoggedIn$.next(true)
console.log(this.isLoggedIn$.value);
} else this.isLoggedIn$.next(false)
},
error: (err) => {
console.log(err)
this.isLoggedIn$.next(false)
}
});
}
}
authStatus() : Observable<HttpResponse>{
return this.http.get(${apiUrls.authServiceApi}/status,
{ observe: 'response', withCredentials: true });
}
then in the template, I use:
<ng-container *ngIf="!isLoggedIn$.value">
<span
[routerLink]="['/login']"
routerLinkActive="router-link-active"
class="text-xl cursor-pointer text-gray-200 font-bold m-3 hover:underline hover:text-indigo-200"
>Login</span
>
<span
[routerLink]="['/register']"
routerLinkActive="router-link-active"
class="text-xl cursor-pointer text-gray-200 font-bold m-3 hover:underline hover:text-indigo-200"
>Register</span
>
</ng-container>
The problem is that the login/register links don't disappear after logging in till I refresh the page. Any ideas?
I'm expecting the isLoggedIn variable to be updated.
You have
isLoggedIn$.valuein your template. I suspect .value on your observable. I dont think it returns the last emitted value.You can use async pipe to subscribe to an observable.