首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >阿波罗图形to将头设置为不工作的创作中间件

阿波罗图形to将头设置为不工作的创作中间件
EN

Stack Overflow用户
提问于 2019-09-01 15:19:24
回答 4查看 4.4K关注 0票数 5

我使用的是react本机和阿波罗客户端,如果我试图通过存储在AsyncStorage中的jwt设置报头,它似乎不起作用。

其他不需要头的解析器工作得很好。我的代码如下。

代码语言:javascript
复制
import { ApolloClient } from "apollo-client";
import { InMemoryCache } from "apollo-cache-inmemory";
import { ApolloLink } from "apollo-link";
import { createHttpLink } from "apollo-link-http";
import AsyncStorage from "@react-native-community/async-storage";

const cache = new InMemoryCache();

const getToken = async () => {
  const token = await AsyncStorage.getItem("jwt");

  if (token) {
    return token;
  } else {
    return null;
  }
};

const httpLink = new createHttpLink({
  uri: ""
});

const authLink = new ApolloLink((operation, forward) => {
  operation.setContext({
    headers: {
      "X-JWT": getToken()
      // this is where I set headers by getToken function
      // If I change function getToken to real token in string type, it works then
    }
  });

  return forward(operation);
});

const client = new ApolloClient({
  cache: cache,
  link: authLink.concat(httpLink)
});

export default client;

就像我在代码中注释的那样,调用getToken函数并不像我所期望的那样工作。我认为我应该有更多的异步知识,等待,但我不明白什么是真正的问题。

我从控制台获得的错误消息是jwt malformed。请告诉我如何解决这个问题。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2019-09-01 16:01:58

尝试直接使用setContext

代码语言:javascript
复制
import { ApolloClient } from "apollo-client";
import { createHttpLink } from "apollo-link-http";
import { setContext } from "apollo-link-context";
import { InMemoryCache } from "apollo-cache-inmemory";
import AsyncStorage from "@react-native-community/async-storage";

const httpLink = createHttpLink();

const authLink = setContext(async (_, { headers }) => {
  const token = await AsyncStorage.getItem("jwt");

  return {
    headers: {
      ...headers,
      "X-JWT": token || null
    }
  };
});

const client = new ApolloClient({
  link: authLink.concat(httpLink),
  cache: new InMemoryCache()
});

export default client;
票数 16
EN

Stack Overflow用户

发布于 2021-05-05 06:17:52

代码语言:javascript
复制
    import { setContext } from '@apollo/client/link/context';
    
    // import getJwtToken // return string
    
    const authMiddleware = setContext(async (operation) =>{
        const token = await getJwtToken();
        return {
          headers: {
            authorization: token || null,
          },
        };
    });
    
    
    const link = ApolloLink.from([
      authMiddleware,
      // ...
      
    ]);
票数 4
EN

Stack Overflow用户

发布于 2019-09-01 15:56:38

问题是你没有等待承诺。这意味着它应该是await getToken()。为了使其工作,ApolloLink函数也应该是async

这会稍微降低性能,因为在每次请求时,它都会从AsyncStore读取令牌。我会这样做的:

代码语言:javascript
复制
import { ApolloClient } from "apollo-client";
import { InMemoryCache } from "apollo-cache-inmemory";
import { ApolloLink } from "apollo-link";
import { createHttpLink } from "apollo-link-http";
import AsyncStorage from "@react-native-community/async-storage";

const cache = new InMemoryCache();

let token
const getToken = async () => {
  if(token) return token;

  token = await AsyncStorage.getItem("jwt");

  return token || null;
};

const httpLink = new createHttpLink({
  uri: ""
});

const authLink = new ApolloLink(async (operation, forward) => {
  operation.setContext({
    headers: {
      "X-JWT": await getToken()
      // this is where I set headers by getToken function
      // If I change function getToken to real token in string type, it works then
    }
  });

  return forward(operation);
});

const client = new ApolloClient({
  cache: cache,
  link: authLink.concat(httpLink)
});

export default client;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57747225

复制
相关文章

相似问题

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