Angular data loss on reload using BehaviorSubject

Use SessionStorage or localStorage or cookies to store your data.

When you hit refresh copy your data in any of the above storage and on init copy it back into your variable. Check below example. Replace the sessionStorage to localStorage to store data in localStorage.

In AppComponent

    ngOnInit() {
    if (sessionStorage.getItem("user")) {
      this.data.changeUser(sessionStorage.getItem("user"));
    }
      }
@HostListener('window:beforeunload', ['$event'])
      unloadNotification($event: any) {
        sessionStorage.setItem("user", this.getUser());
      }

From your code:

private userSource = new BehaviorSubject({});
  currentUser = this.userSource.asObservable().pipe(distinctUntilChanged());

  constructor(private injector: Injector, private api: UtentiApiService) { }

  getUser() {
    return this.currentUser;
  }

Your userSource is a BehaviorSubject which contains nothing when you start your application (also on reload).

When you login a user you call:

changeUser(user: object) {
    this.userSource.next(user);
  }

After this call your BehavoirSubject contains a user. But you only do this during login process.

When you reload your page you need to fetch the access token from localstorage, and call next on the BehavoirSubject with the current user.

You can do that in the constructor of the dataService for instance.