0

我正在构建一个带有Django-Rest-Framework后端的应用程序,并使用我正在使用的前端dj-rest-auth进行身份验证,并且正在使用(github链接-> API-Sauce )进行我的api调用。建立在 axios 之上Simple JWTReact-Nativeapi-sauceAPI-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 调用自动刷新令牌?

4

0 回答 0