I need to wait for user information about the user before starting any protected component. I use a similar solution for lastWhisper, implementing canActivateChild and setting in routes:
path: 'app', component: FullLayoutComponent, data: {title: 'Home'}, canActivateChild: [CanActivateChildTeam], CanActivateChildTeam Service: import {Injectable, OnDestroy} from "@angular/core"; import {CanActivateChild} from "@angular/router"; import {Meteor} from 'meteor/meteor'; import {Router} from '@angular/router'; import {Observable} from "rxjs/Rx"; import {MeteorObservable} from 'meteor-rxjs'; import {Subscription} from 'rxjs/Subscription'; @Injectable() export class CanActivateChildTeam implements CanActivateChild, OnDestroy { allow:Observable<boolean>; private _userSub:Subscription; constructor(private router:Router) { } canActivateChild(route:any, state:any):Observable<boolean> | Promise<boolean> | boolean { return this.getUser(); } getUser() { if (this.allow) { this.allow = new Observable(observer => { observer.next(true); observer.complete(); }); return this.allow; } if (Meteor.userId()) { this.allow = new Observable(observer => { if (this._userSub) this._userSub.unsubscribe(); this._userSub = MeteorObservable.subscribe('user', Meteor.userId()).subscribe( () => { observer.next(true); observer.complete(); } ); }); return this.allow; } else { this.router.navigate(['/']); } } ngOnDestroy():void { if (this._userSub) this._userSub.unsubscribe(); } }
source share