I'm having trouble setting up an auth guard for one of my routes because I am unsure how to implement it with an observable.
I am using ngrx/store
to store my token. In the guard, I retrieve it using this.store.select('auth')
, which returns an object like this (if the user is logged in):
{
token: 'atokenstring',
isAuthenticated: true,
isPending: false
}
This is what the guard code looks like:
export class AuthGuardService implements CanActivate {
constructor(private router: Router, private store: Store<IStore>) {}
canActivate(): Observable<any> {
return this.store.select('auth').let((state: Observable<IAuthStorage>) => state.filter((auth: IAuthStorage) => !auth.isPending && auth.isAuthenticated)).map(
(auth: IAuthStorage) => {
if (!auth.isAuthenticated) {
return this.router.navigateByUrl('admin/login');
}
else {
return true;
}
}
);
}
}
The issue seems to be that the guard is returning an observable instead of a boolean value, preventing the route from rendering even when the else
block returns true
.
How can I modify the guard to return a boolean value instead of an observable?