无法刷新JWT令牌“签名已过期”。

时间:2019-05-10 19:13:43

标签: django reactjs django-rest-framework jwt django-rest-framework-jwt

我正在为我的SPA使用django-rest-framework-jwt和react-redux。

需要刷新令牌,该令牌在5分钟内到期。 刷新在5分钟内有效。 在它不起作用后,控制台显示此错误:

POST http://localhost:8000/auth/api-token-refresh/ 400 (Bad Request)
createError.js:17 Uncaught (in promise) Error: Request failed with status code 400
    at createError (createError.js:17)
    at settle (settle.js:19)
    at XMLHttpRequest.handleLoad (xhr.js:78)

邮递员展示:

{
    "non_field_errors": [
        "Signature has expired."
    ]
}

有中间件的代码

import axios from "axios";
import * as urls from "../helpers/url";
import { authUpdateToken } from "../actions/auth";

const jwthunk = ({ dispatch, getState }: any) => (next: any) => (action: any) => {
  if (typeof action === 'function') {
    if (getState().auth && getState().auth.token) {
      const currentToken = getState().auth.token;

      verifyToken(currentToken)
        .then((tokenVerified: any) => {
          refreshToken(tokenVerified, dispatch)
        })
        .catch(() => {
          refreshToken(currentToken, dispatch)
        })
    } else {
      console.log('Not Auth');
    }
  }
  return next(action);
}

export default jwthunk;

const verifyToken = async (token: any) => {
  const body = { token };
  let verifiedToken = '';

  await axios.post('http://localhost:8000/auth/api-token-verify/', body)
    .then(({ data: { code, expires, token } }: any) => {
      verifiedToken = token;
    });

  return verifiedToken;
}

const refreshToken = async (token: any, dispatch: any) => {
  const body = { token }

  await axios.post('http://localhost:8000/auth/api-token-refresh/', body)
    .then((response: any) => {
      dispatch(authUpdateToken({ token }));
    })
}

django-rest-framework-jwt发送不带刷新令牌的唯一令牌

1 个答案:

答案 0 :(得分:0)

我认为那里的图书馆有缺陷。 您无法刷新过期的令牌。

解决方案

1)切换到其他库

2)您需要在应用程序(前端)中运行一个计时器,并在到期前每5分钟请求一次访问令牌。 (这不是理想的方法)