首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当使用jwt-刷新-链接时,我有一个类型记录错误。

当使用jwt-刷新-链接时,我有一个类型记录错误。
EN

Stack Overflow用户
提问于 2020-09-30 09:19:00
回答 1查看 506关注 0票数 0

这是我第一次尝试打字。我希望youtube用户auth教程,这是处理令牌刷新-链接。我遇到了一个问题,但教程视频没有这个问题,我不知道如何解决它。

以下是错误日志:

代码语言:javascript
复制
H:/jwt-auth/web/src/index.tsx
TypeScript error in H:/jwt-auth/web/src/index.tsx(48,5):
Type 'TokenRefreshLink<string>' is not assignable to type 'ApolloLink'.
Types of property 'split' are incompatible.
Type '(test: (op: import("H:/jwt-auth/web/node_modules/@apollo/client/link/core/types").Operation) => boolean, left: import("H:/jwt-auth/web/node_modules/@apollo/client/link/core/ApolloLink").ApolloLink | import("H:/jwt-auth/web/node_modules/@apollo/client/link/core/types").RequestHandler, right?: import("H:/jwt-auth/web/...' is not assignable to type '(test: (op: import("H:/jwt-auth/web/node_modules/apollo-link/lib/types").Operation) => boolean, left: import("H:/jwt-auth/web/node_modules/apollo-link/lib/link").ApolloLink | import("H:/jwt-auth/web/node_modules/apollo-link/lib/types").RequestHandler, right?: import("H:/jwt-auth/web/node_modules/apollo-link/lib/link...'.
          Types of parameters 'test' and 'test' are incompatible.
            Types of parameters 'op' and 'op' are incompatible.
              Property 'toKey' is missing in type 'import("H:/jwt-auth/web/node_modules/@apollo/client/link/core/types").Operation' but required in type 'import("H:/jwt-auth/web/node_modules/apollo-link/lib/types").Operation'.  TS2322
  46 | const client = new ApolloClient({
    47 |   link: ApolloLink.from([
  > 48 |     new TokenRefreshLink({
       |     ^
    49 |       accessTokenField: "accessToken",
    50 |       isTokenValidOrUndefined: () => {
    51 |         const token = getAccessToken();

这是我的密码:

代码语言:javascript
复制
 const client = new ApolloClient({
      link: ApolloLink.from([
        new TokenRefreshLink({
          accessTokenField: "accessToken",
          isTokenValidOrUndefined: () => {
            const token = getAccessToken();

            if (!token) {
              return true;
            }

            try {
              const { exp } = jwtDecode(token);
              if (Date.now() >= exp * 1000) {
                return false;
              } else {
                return true;
              }
            } catch {
              return false;
            }
          },
          fetchAccessToken: () => {
            return fetch("http://localhost:4000/refresh_token", {
              method: "POST",
              credentials: "include",
            });
          },
          handleFetch: (accessToken) => {
            return setAccessToken(accessToken);
          },
          handleError: (err) => {
            console.warn("Your refresh token is invalid.Try ro relogin");
            console.error(err);
          },
        }),
        onError(({ graphQLErrors, networkError }) => {
          console.log(graphQLErrors);
          console.log(networkError);
        }),
        requestLink,
        new HttpLink({
          uri: "http://localhost:4000/graphql",
          credentials: "include",
        }),
      ]),
      cache,
    });
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-11 11:52:18

几个小时前,当我尝试使用“阿波罗-链接-令牌-刷新”时,我得到了同样的信息。

我在找到解决方案的时候偶然发现了这篇文章。我相信你现在可能已经解决了这个问题。

为了其他可能遇到同样问题的人的利益,我能够通过将TokenRefreshLink从ApolloLink中取出,声明一个类型为any的变量并将TokenRefreshLink传递给它来解决这个问题。稍后,再次将该变量调用到ApolloLink中。

代码语言:javascript
复制
  const tokenRefreshLink: any = new TokenRefreshLink({
  accessTokenField: "accessToken",
  isTokenValidOrUndefined: () => {
    const token = getAccessToken();

    if (!token) {
      return true;
    }

    try {
      const { expiration }: any = jwtDecode(token);
      if (Date.now() >= expiration * 1000) {
        return false;
      } else {
        return true;
      }
    } catch (error) {
      return false;
    }
  },
  fetchAccessToken: () => {
    return fetch("http://<your_endpoint>/<refresh_token_path>", {
      method: 'POST',
      credentials: "include"
    });
  },
  handleFetch: accessToken => {
    // set the access token
    setAccessToken(accessToken);
  },
  handleError: err => {
    console.warn('Your refresh token is invalid. Try to relogin');
    console.error(err);
  }
});

const client = new ApolloClient([

  link: ApolloLink.from([
    tokenRefreshLink,
    onError(({ graphQLErrors, networkError }) => {
      console.log(graphQLErrors)
      console.log(networkError)
    }),
    requestLink,
    new HttpLink({
      uri: "http://<your_endpoint>",
      credentials: "include"
    })
  ]),
  cache
])
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64134528

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档