我正在构建一个带有Django-Rest-Framework
后端的应用程序,并使用我正在使用的前端dj-rest-auth
进行身份验证,并且正在使用(github链接-> API-Sauce )进行我的api调用。建立在 axios 之上Simple JWT
React-Native
api-sauce
API-Sauce
这是我的基本 clientAPI 代码,它Django-Rest-Framework
来自我的 React-Native 应用程序
import { create } from "apisauce";
import authStorage from "../auth/authStorage";
const baseURL = "http://192.168.0.109:8000/api";
const apiClient = create({
baseURL: baseURL,
});
apiClient.addAsyncRequestTransform(async (request) => {
const authToken = await authStorage.getToken();
if (!authToken) return;
request.headers["Authorization"] =
"Bearer " + JSON.parse(authToken).access_token;
});
export default apiClient;
只要令牌处于活动状态,这就会完美地工作。令牌过期后,要刷新我的令牌,我必须执行以下操作
Target URL - http://192.168.0.109:8000/api/token/refresh/
Header - "Content-Type" : "application/json"
data - {"refresh":"refresh_token"}
// refresh_token is obtained from authStorage in the above code-snippet like so
//JSON.parse(authToken).refresh_token
如何有效地将刷新令牌的代码添加到现有代码中,从而使我的 API 调用自动刷新令牌?