2

I'd like to use Ngrx for OAuth2 services and I need to get current store state without using subscribe.

This is my OAuth function for getting token from localStorage:

private oAuth2() {
    const token = JSON.parse(localStorage.getItem('oAuthToken'));
    if (token && token.access_token) {
        const headers = new Headers({ 'Authorization': 'Bearer ' + token.access_token });
        return new RequestOptions({ headers: headers });
    }
}

Is it possible to use Redux store instead of localStorage in this case?

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
Jakub
  • 104
  • 1
  • 4

1 Answers1

3

I really don't see why you want to get the state without subscribing to it as this is the core concept of Ngrx and you can't get a value from a RxJS Subject unless you subscribe to it:

The two pillars of @ngrx/store, the store and dispatcher, both extend RxJS Subjects. Subjects are both Observables and Observers, meaning you can subscribe to a Subject, but can also subscribe a Subject to a source. At a high-level Subjects can be thought of as messengers, or proxies.

I hope this explains to you that the answer is NO. You can't get current Ngrx state without subscribing to it!

halfer
  • 19,824
  • 17
  • 99
  • 186
Hamed Baatour
  • 6,664
  • 3
  • 35
  • 47
  • Hamed, can you add a link to this question to show where the official quote is from? Readers might find that interesting and useful. – halfer Apr 20 '20 at 17:23