Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] how to change state in responseHandler? #146

Open
divan opened this issue Jun 3, 2017 · 4 comments
Open

[Question] how to change state in responseHandler? #146

divan opened this issue Jun 3, 2017 · 4 comments
Labels

Comments

@divan
Copy link

divan commented Jun 3, 2017

Hi,

I'm trying to implement straightforward thing - catch "Unauthorized" error and redirect user to the "/login" page. How do I do that with redux-api?

I assume the way to go is to implement responseHandlerbut it's unclear how to access state from within it.

Any suggestions? Thanks in advance.

@jakeaaron
Copy link

I think there is an example of exactly what you're trying to do here: https://github.com/lexich/redux-api/blob/master/docs/AuthorizationJWT.md

@lexich
Copy link
Owner

lexich commented Jun 3, 2017

@divan if you use solutions like redux-router postfetch option can help you for concrete endpoint.
if you want to define global interceptor - useresponseHandler

reduxApi({ ... }).use("responseHandler",
  (err, data)=> {
      if (isNeedRedirect(err)) {
        store.dispatch(redirectTo(''));
        // or 
        window.location.href = '.....';
       // or you router api
      }
  });

But you should be careful, because this method catch all requests, and not at all bad responses need to been redirected.

@lexich lexich added the question label Jun 4, 2017
@divan
Copy link
Author

divan commented Jun 6, 2017

@jakeaaron thanks, I've checked this example, but it's not clear. What is check_auth endpoint? Will this request will be happening before each other API request?

If I understood it correctly, I think my approach is slightly different - if any of normal API requests is getting "Unuathorized" error from backend, simply dispatch logout action (which will handle token local storage and router redirection).

@lexich is it a right way to do redirect using window.location? I also currently don't see clearly how do I get access to store and dispatch from within reduxApi(). I'm still new to redux, so maybe it's really obvious, but I can't see anything similar in examples.

And, to be clear, I'm not trying to invent anything on my own. I'm trying to go as "standard" as possible. And I suppose, this functionality is a very basic one.

@lexich
Copy link
Owner

lexich commented Jun 6, 2017

@divan it depends. If you use react-router, you should use it redirect methods

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants