0

I am using ngrx/router.

I have a LoginGuard, when I open a page which needs to login, the LoginGuard runs before isSignedIn is set to true. So at that time isSignedIn is undefined.

@Injectable()
export class LoginGuard implements Guard {
  constructor(private _router: Router, private _userService: UserService) { }

  protectRoute({ route, params, isTerminal }: TraversalCandidate): Observable<boolean> {
    return this._userService.checkSignedIn()
      .map(isSignedIn => {
        if (!isSignedIn) {
          this._router.replace('/landing');
          return false;
        } else {
          return true;
        }
      }).first();
  }
}

I set isSignedIn to true in the beginning of the app, which is the earliest place in my mind.

class App implements OnInit {
  ngOnInit() {
    // I set isSignedIn in UserService to true here after I got user info from the server
  }
}

But maybe it is not early enough? So how can I set isSignedIn to true before Guard runs? Thanks

Hongbo Miao
  • 45,290
  • 60
  • 174
  • 267

1 Answers1

0

My app actually uses a service checking access in an async way.

@rtytgat gives a solution. And thanks!

Check with the Guard, if not logged in send them to 'logging in' page, and send them back when login has been confirmed.

Hongbo Miao
  • 45,290
  • 60
  • 174
  • 267