Using the OAuth2 Update Token in a React Redux Application

I have an application with OAuth2. It works fine, but I'm confused with refresh_tokenshere. My application uses a combination of React+ Redux.

I know that I need to check if mine has ended access_tokenand then request a new one using refresh_token. Good ... But when should I update it? After it 401happened or on time, when is any request APIthat needs authorization ready (immediately before sending)?

I also know how to use interceptors HTTPto get all requests APIbefore sending or detecting responses 401. The problem is that I am confused how to solve the problem in the thread Redux. How to β€œfreeze” a request for a token update time? Or how to repeat the request when I solve the problem in the answer 401?

+4
source share
1 answer

At the first authentication with the server, we will have {access_token, expires_in, refresh_token}. We will store access_token and expires_in in the session store and refresh_token in local storage.

, access_token ( / access_token), , ( 1 ), refresh_token access_token. , , .

:

function getToken() {
    const expiresIn = storage.getItem(KEY_EXPIRES_IN);
    const accessToken = storage.getItem(KEY_ACCESS_TOKEN);
    if (accessToken
        && (!expiresIn || moment.unix(Number(expiresIn)).diff(moment(),   'minute') > 1)) {
        return accessToken;
    }
    return refreshToken();
}

, .

P/S:

+3

All Articles