如何在带有react-relay-network-layer的react-native中使用cookie?

时间:2019-03-17 19:14:30

标签: node.js react-native cookies react-native-ios

我遇到的问题是不能能够在我的react-native网络层请求中包含cookie。我已经将fetchOpts.credentials设置为 include (我也尝试过 same-origin ,但这也不起作用)。我正在尝试在iOS模拟器上对此进行测试。

这是我在react-relay-network-layer env中使用的中间件。

// Grab cookie from store and set with cookiemanager
next => async req => {
  const cookieVal = await getCookie(); // RETURNS the Set-Cookie string (unformatted since response)

   // iOS requires a dict
  if (Platform.os === 'ios') {
    cookieToSet = { 'Set-Cookie': cookieVal }
  } else {
    cookieToSet = cookieVal;
  }

  console.log(cookieToSet); // This console.logs the expected result!

  // Note: The backend url is `http://localhost:5000`, however make graphql reqs to: `http://localhost:5000/graphql`
  const setCookieRes = await setCookieFromResponse('http://localhost:5000', cookieToSet);
  console.log(setCookieRes); // // This is ALWAYS undefined? Why though?

},

// Make the request & grab the Set-Cookie header if it's available
next => req =>
  next(req).then(async res => {
    const cookieVal = res.headers.get('set-cookie');
    if (cookieVal) {
      const splitCookieHeaders = cookieParser.splitCookiesString(cookieVal);
      const cookies = cookieParser.parse(splitCookieHeaders, {
        decodeValues: true,
        map: true,
      })
      let cookieToSet = '';

      // iOS requires a dict
      if (Platform.os === 'ios') {
        cookieToSet = { 'Set-Cookie': cookieVal }
      } else {
        cookieToSet = cookieVal;
      }

      const setCookieRes = await CookieManager.setFromResponse(
        'http://localhost:5000',
        cookieToSet
      );
      console.log(setCookieRes); // This is ALWAYS undefined?
    }
    After making another request the cookies aren't included?!
    ....

0 个答案:

没有答案