0

I'm going this direction because i've another guard who is validating the user on the token previously stored. This is what i was doing in previous version of rxjs, now in the latest you cant map on the subject.

canActivate(next: ActivatedRouteSnapshot,
           state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
  return this.authservice.behaviorsubject().map(x => {
    if (x) {
      return true;
    } else {
      return false;
    }
  })
}

I need any guidence how to get this thing working. I've tried somting like this but wont return anything since never subscribed to it (and also its not a Observable its a AnonymusSubject when i console.log it)

canActivate(next: ActivatedRouteSnapshot,
           state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
  return this.authservice.behaviorsubject().pipe(
    map(x => {
      if (x) {
        return true;
      } else {
        return false;
      }
    })
  )
}

Also tried converting it into observable with Observable.create() but wont work. I know im missing something. Heeeeelp :)

Igor
  • 247
  • 1
  • 7
  • 16
  • Hi it seems you don't subscribe to it to get your value back – t3__rry May 17 '18 at 07:51
  • 1
    @t3__rry you don't need to subscribe, look at the allowed return types. Or look at this post: https://stackoverflow.com/questions/37948068/angular-2-routing-canactivate-work-with-observable – J. Pinxten May 17 '18 at 13:59

1 Answers1

1

return your behaviorSubject as an observable in your authservice:

private _authed: BehaviorSubject<boolean> = new BehaviorSubject(null);

behaviorsubject():Observable<boolean> {
   return this._authed.asObservable()
}

Also a small comment: CanActivate works with observables, so if your behaviorsubject function returns a Observable with type boolean, you don't need to map it

J. Pinxten
  • 307
  • 3
  • 7