I am running an Angular 9 Universal application that uses NgRx for state management. I am also using ngrx-store-localstorage to persist the Store into the user's localStorage.
I am trying to check if user is loggedIn before accessing certain routes with NgRx inside a Guard:
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
if (isPlatformBrowser(this.platformId)) {
console.log('browser')
return this.store.select('user').pipe(
map(authUser => {
if (!authUser.id || !authUser.token) {
console.log(authUser)
this.router.navigate(['/login'])
return false
}
return authUser.id ? true : false;
}))
}
console.log('server')
this.router.navigate(['/login'])
return false
}
I am doing a platform check since my server does not have access to the store. But this creates unwanted behaviors since it checks twice and sometimes render the login page before rendering the guarded page.
Do you know a way to let my server know about the current State or an alternative way to verify if my user is authenticated?
private store: Store
and there is an@Injectable
annotation just above theexport
, please confirm – shuk