1

We recently upgraded from Angular 13.3 to Angular 14. Since then we're having issues when the user refreshes a page.

We have an HttpInterceptor which checks that we still have the user information.

It looks like the this.globalObservalStateService.getUser() no longer returns an observable after a refresh.

I've added several console.log statements. I also added a getUser().subscribe to see if maybe that will still return a value. And I added a 'showuser' as the first statement in the pipe which should execute for everything returned by the pipe. The pipe always returns only one observable.

The console.log in getUser always shows a value but all the other console.log indicate that the user object/observable is empty/undefined.

Has anyone had a similar issue after upgrading to Angular 14?

globalObservalStateService:
    private user = new BehaviorSubject<User>(null);

    getUser(): Observable<User> {
        console.log('getUser');
        console.log(this.user);
        return this.user.asObservable();
    }


Interceptor service:


  intercept(req: HttpRequest<any>, next: HttpHandler): any {
    console.log('intercept');

// added to investigate the issue
    this.globalObservalStateService.getUser()
    .subscribe(userResult => {
      if (userResult) {
        console.log('subscribe user is filled');
      } else {
        console.log('subscribe !user');
      }
    });

    return this.globalObservalStateService.getUser().pipe(
// map added to investigate the issue    
      map((x) => (x = this.showuser(x))),
      take(1),
      exhaustMap((user: User) => {

        if (!user) {
          console.log('map !user');
        } else  {
          console.log('map user filled');
        }
        
      etc.
      
   }
   
  // added to investigate the issue
  showuser(user: User) : User {
    if (!user) {
      console.log ('showuser: !user');
    } else {
      console.log('showuser user is filled');
    }
    return user;
  }
  • try removing the `take(1)`. – Jai Aug 25 '22 at 12:20
  • When I remove the take(1) I have issues even before refreshing the page. The popup for username/password keeps popping up, Also some pages are stuck in 'loading mode'. That could be due to the code in the exhaustMap. I will have a look at that. – Nelleke Rietvink Aug 25 '22 at 12:44

0 Answers0