【问题标题】:Make Redux thunk calls synchronous for refreshing tokens使 Redux thunk 调用同步以刷新令牌
【发布时间】:2020-09-21 08:25:10
【问题描述】:

将 redux 与 thunk 中间件集成。在访问令牌到期时,将调用刷新令牌 api,并在其成功时再次调用由于令牌过期而未成功的第一个 api。

刷新令牌 api 被调用并返回,因为它是异步的。并且在刷新令牌成功响应之前立即调用编辑api。如何使其同步,以便仅在收到刷新令牌的响应后调用 api

export function editClothDetails(data, id) {
return  function(dispatch, getState) {
    dispatch({ type: actions.EDIT_CLOTH_REQUEST });
     fetch(BASE_URL + EDIT_CLOTH_URL + `/${id}`, {
        method: "PUT",
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
            'Authorization': 'Bearer ' + getState().Auth.accessToken
        },
        body: JSON.stringify({ ...data })
    })
    .then(result => checkHttpStatus(result))
    .then(result => checkForError(result))
    .then(jsonResponse => {
        dispatch({
            type: actions.EDIT_CLOTH_SUCCESS,
            payload: jsonResponse
        });
    })
    .catch((error) => {
        if(error.message === "Invalid token") {
            //what should be the right way to make these dispatches synchronous
            dispatch(refreshToken());
            dispatch(editClothDetails(data, id)); //setTimeout(()=> dispatch(editClothDetails(data, id)), 100);
        }
        console.error("There is an error in editing cloth details !! " + error.message);
        dispatch({
            type: actions.EDIT_CLOTH_FAILED,
            payload: error.message
        });
    });
};
}

export function refreshToken() {
    return (dispatch, getState) => {
        dispatch({ type: actions.REFRESH_TOKEN_REQUEST });
        fetch(BASE_URL + '/token', {
            method: "GET",
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
                'authorization': 'Bearer ' + getState().Auth.refreshToken
            },
        })
        .then(result => checkHttpStatus(result))
        .then(result => checkForError(result))
        .then(jsonResponse => {
            storeLocally(constants.APP_NAME, jsonResponse);
            dispatch({
                type: actions.REFRESH_TOKEN_REQUEST_SUCCESS,
                payload: jsonResponse
            });
        })
        .catch((err) => {
            console.error("There is an error refreshing token !!" + err.message);
            dispatch({
                type: actions.REFRESH_TOKEN_REQUEST_FAILED,
                payload: err.message
            });
        });
    };
}

【问题讨论】:

    标签: reactjs redux redux-thunk synchronous refresh-token


    【解决方案1】:

    你必须在这里使用 async-await ...

    export function editClothDetails(data, id) {
    return  async function(dispatch, getState) {      // -> see here
    
        .catch((error) => {
            if(error.message === "Invalid token") {
                await dispatch(refreshToken());                 //--> see here
                dispatch(editClothDetails(data, id)); 
            }
     
     // your other code
          
    };
    }
    
    export async function refreshToken() {.  /// ---> see here
        return async (dispatch, getState) => {
            dispatch({ type: actions.REFRESH_TOKEN_REQUEST });
            /// your other code
        };
    }
    

    【讨论】:

    • 抛出错误说等待在函数之外
    • 如果你能创建一个沙盒,我可以为你测试一下
    猜你喜欢
    • 2018-11-10
    • 2016-08-25
    • 2019-02-03
    • 2018-01-20
    • 2021-12-11
    • 2018-03-27
    • 2021-01-10
    • 2017-10-23
    • 2019-01-21
    相关资源
    最近更新 更多