【问题标题】:"Error: Error decoding signature" and "Variable '$token' is never used in operation 'VerifyToken'."“错误:错误解码签名”和“变量 '$token' 从未在操作 'VerifyToken' 中使用。”
【发布时间】:2021-08-24 15:42:53
【问题描述】:

工具: Django + Next.js + Django-GraphQL-JWT

什么有效: 我可以登录用户并获得一个 JWT 令牌,除了将令牌保存在 localStorage 中以供稍后检索和验证之外,我还保存了该令牌。

什么不起作用: 我可以成功检索 localStorage 令牌,但是当我尝试使用库来验证服务器上的令牌时,我收到此错误:

[GraphQL error]: Message: Error decoding signature, Location: [object Object], Path: verifyToken

验证码:

const [authToken, setAuthToken] = useState(null);
const [localToken, setLocalToken] = useState();

  useEffect(() => {
    setLocalToken(localStorage.getItem("token"));
  }, []);

...
const verifyToken = async () => {
    const client = createApolloClient();
    const data = await client.mutate({
      mutation: verifyMutation,
      variables: { token: localToken },
    });
    if (data) {
      setAuthToken(data.data.tokenAuth.token);
    }
    return data;
  };
...

变异:

export const verifyMutation = gql`
  mutation VerifyToken($token: String!) {
    verifyToken(token: $token) {
      payload
    }
  }
`;

schema.py:

class Mutation(graphene.ObjectType):
    token_auth = graphql_jwt.ObtainJSONWebToken.Field()
    verify_token = graphql_jwt.Verify.Field()
    refresh_token = graphql_jwt.Refresh.Field()
    revoke_token = graphql_jwt.Revoke.Field()

当我在 GraphQL 中手动尝试时会发生以下情况:

如果我的变异包含令牌:

mutation VerifyToken($token: String!) {
  verifyToken(token: "token_string_here") {
    payload
  }
}

返回:

{
  "errors": [
    {
      "message": "Variable '$token' is never used in operation 'VerifyToken'.",
      "locations": [
        {
          "line": 1,
          "column": 22
        }
      ],
      "path": null
    }
  ]
}

但是,如果我不包含这样的令牌:

mutation VerifyToken {
  verifyToken {
    payload
  }
}

返回:

{
  "data": {
    "verifyToken": {
      "payload": {
        "username": "myname",
        "exp": 1623076467,
        "origIat": 1623076167
      }
    }
  }
}

我尝试过的其他方法: 我发现一些关于 SECRET_KEY 是解码问题的参考,但我已经在我的设置中设置了它,但问题没有任何改善。我还没有找到任何其他似乎可以在这里工作的解决方案。

我也尝试过使用自定义 JWT_VERIFY 模块,但这并没有帮助我解决问题。

【问题讨论】:

    标签: django graphql next.js django-graphql-jwt


    【解决方案1】:

    哇,那好吧。所以在我使用从另一个教程中获取的这段代码之前,登录部分最初对我不起作用:

    localStorage.setItem("token", JSON.stringify(data.data.tokenAuth.token));

    我没有意识到令牌被设置为不同的格式,因此与 JWT 中令牌的外观不匹配。

    将代码更改为以下内容即可解决问题!希望这对其他人有帮助:

    localStorage.setItem("token", data.data.tokenAuth.token);

    【讨论】:

      猜你喜欢
      • 2022-01-02
      • 2019-02-14
      • 1970-01-01
      • 2020-02-19
      • 2012-06-02
      • 1970-01-01
      • 1970-01-01
      • 2017-10-11
      • 1970-01-01
      相关资源
      最近更新 更多