5
return this.auth.user.pipe(
      switchMap((user: IUser) => {
        return of(true);
      })
    );

My initial code was a bit more complex with some cases depending on the user data, but for testing purposes I've used the code above in a guard's canLoad and it does not activate.

There's TS/compilation errors whatsoever.

I've tried it both with Ivy and without.

I'm using Angular 8.3;

SebastianG
  • 8,563
  • 8
  • 47
  • 111
  • please write the error – Khashayar Pakkhesal Sep 13 '19 at 15:01
  • @KhashayarPakkhesal there's no error, the route doesn't activate. I've added a catchError operator just to be safe and there's no error either. I've checked to see if that code runs inside the switchMap and it does, if I log something on the console there it will show up when I try to activate the route. – SebastianG Sep 13 '19 at 15:03

1 Answers1

7

You have to use a take(1), because I'm sure the auth.user is a stream and does not complete. A guard needs to have a completing Observable:

return this.auth.user.pipe(
  take(1),
  switchMap((user: IUser) => of(!!user))
);

Perhaps you removed some code, but you can also just use map here:

return this.auth.user.pipe(
  take(1),
  map((user: IUser) => !!user)
);
Poul Kruijt
  • 69,713
  • 12
  • 145
  • 149