A
A
Alexander Lemyagov2019-01-25 02:59:08
React
Alexander Lemyagov, 2019-01-25 02:59:08

How to call transition to another page outside of action?

I use Axios for requests. Created an interceptor for the response from the server. If the server returns a 401 error, I need to take the refresh token and send it to the server to refresh the access token.
Here is the interceptor:

Axios.interceptors.response.use(function (response) {
    console.log(response);
    if(isResponseOK(response))
        return response;

    const originalRequest = response.config;

    Axios.post(REFRESH_TOKENS, {
        refreshToken: AppStore.retrieve("refreshToken")
    })
        .then(function (response) {
            
        })
        .catch(function (error) {

        });

}, function (error) {
    console.log(error);
    return Promise.reject(error);
});

And here is the difficulty. If the refresh token check fails, I need to redirect the user to the authorization page. How to do it? given that there is no store, dispatch, etc. In general, those things that are used in action, etc. How to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alprtv, 2019-01-25
@Tsyklop

store, dispatch can be obtained from the file in which configureStore() is called, for this you need to export it.
For example like this:

import React from 'react';
import { Provider } from 'react-redux';
import configureStore from '../store/configureStore';
import CoreLayout from './CoreLayout';

export const store = configureStore();

const App = () => (
  <Provider store={store}>
    <CoreLayout />
  </Provider>
);

export default App;

After that, in api.js, you can call actions:
import { store } from '../../containers/App';
store.dispatch(signOutAction());

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question