Angular 2 AuthGuard Service with redirect?

前端 未结 5 1158
南旧
南旧 2020-12-05 22:46

I have an application that I am building that implements CanActivate on the dashboard route. It works fine accept on page reload, I check a flag in the user service to see i

5条回答
  •  失恋的感觉
    2020-12-05 23:44

    I solved it like this and used it in my AuthGuard

    isLoggedIn(): Observable {
    return this.afAuth.authState
      .pipe(
        take(1),
        map(user => {
            return !!user;
          },
          () => {
            return false;
          }
        ),
        tap(loggedIn => {
            if (!loggedIn) {
              this.router.navigate(['/']);
            }
          }
        ));
    }
    

提交回复
热议问题