How to write interceptor in react-redux?

你。 提交于 2019-12-25 08:47:36

问题


I have to pass token, received from server, after login to every api. so my question is how to write an interceptor which will append token on every api call. I am using 'request-promise' to call api.

Thanks!


回答1:


I would create a wrapper (interface) function for your 'request-promise' calls:

export default class RequestInterface {
    constructor(accessToken) {
        this.accessToken = accessToken;
    }

    request({
        method,
        uri,
        body,
    }) {
        return rp({
            method,
            uri,
            body,
            qs: {
                access_token: this.accessToken,
            },
        });
    }
}

This way on every RequestInterface.request() call, you'll have your credentials there.

(I didn't test this but that's the basic idea)



来源:https://stackoverflow.com/questions/40205707/how-to-write-interceptor-in-react-redux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!