简体   繁体   English

带有 Expo 的 React Native Mobile 应用程序中的 Cookies 问题

[英]Cookies issues in React Native Mobile app with Expo

I'm creating this Native Mobile app that basically is hosting a Web App in Webviews, my task is to build the login page in Native as the clients wants to use biometric logins.我正在创建这个 Native Mobile 应用程序,它基本上在 Webviews 中托管一个 Web 应用程序,我的任务是在 Native 中构建登录页面,因为客户想要使用生物识别登录。 The thing is I was able to create the login page setup routings and make the requests to login endpoint.问题是我能够创建登录页面设置路由并向登录端点发出请求。 The thing is that login request returns authentication as a cookie.问题是登录请求将身份验证作为 cookie 返回。

async function login(event) {
const response = await fetch(`https://xxxxx.xxxx.xx.xxx/api/v1/login.json`, {
  method: 'POST',
  headers: {
      'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    username: userName,
    password: password,
    remember_device: true,
    workgroup_code: 'XXXX',
    accessor_id: null,
    origin: 'xxx'
  })
});
if (response.ok) {
  const data = await response.json();
  // console.log(JSON.stringify(data));
  authObj = data;
  if (authObj.restrictions.find(x => x === 'verify_device')) {
    navigation.navigate('Device');
  }
} else {
  alert(`Bad username or password.
  Need help logging in?
  Call X-XXX-XXX-XXXX`);
}

} }

As you can see, sometimes I need to send the successful login to a verification screen that loads the Web App in a webview:如您所见,有时我需要将成功登录发送到验证屏幕,该屏幕在 webview 中加载 Web 应用程序:

<WebView 
  javaScriptEnabled={true}
  style={styles.container}
  source={{ uri: 'https://xxxx-staging.xxx.com/#/login/restriction/verify_device' }}
  sharedCookiesEnabled={true}
  injectedJavaScriptBeforeContentLoaded={runFirst}
  injectedJavaScript={runLast}
  onMessage={(event) => {
    console.log('event: ', event);
  }}
/>

runFirst set some values in localstorage that works fine: runFirst在 localstorage 中设置一些可以正常工作的值:

const runFirst = `
window.localStorage.setItem('workgroup_code', ${authObj.workgroup_code});
window.localStorage.setItem('accessor_id', ${authObj.mpv_device_id});
true; // note: this is required, or you'll sometimes get silent failures

`; `;

and runLast is just some example code of my tests: runLast只是我的测试的一些示例代码:

const runLast = `
alert('Me');
alert(window.document.cookies);
true;

`; `;

The thing is for this page in the Webview to work correctly it needs to have a set of cookies like this:问题是 Webview 中的这个页面要正常工作,它需要有一组 cookies 像这样:

_xxx_web_session_staging that comes in the login request above, How can I pass the request cookies to the Webview page??上面login请求中的_xxx_web_session_staging ,如何将请求cookies传递到Webview页面?

So you Know I tried CookieManager but I couldn't find a way to make it work inside my Expo app and neither understand how to pass the values to the WebView.所以你知道我试过 CookieManager 但我找不到让它在我的 Expo 应用程序中工作的方法,也不知道如何将值传递给 WebView。

Also as you can see I'm using sharedCookiesEnabled={true} I don't know if I'm missing something.另外,正如您所看到的,我正在使用sharedCookiesEnabled={true}我不知道我是否遗漏了什么。

I would really appreciate any help or advice on this.我真的很感激任何帮助或建议。

I solved this issue parsing myself the response from the fetch request:我通过解析获取请求的响应解决了这个问题:

    loginCookie = JSON.stringify(response.headers.map['set-cookie']);
and then setting the cookie value manually:

const runFirst = `
    window.document.cookie = ${loginCookie};
    true; // note: this is required, or you'll sometimes get silent failures
  `;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM