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