简体   繁体   English

React Native 如何获取 cookie

[英]React Native how to get cookie

I have a cookie-based auth system, and I don't understand how to get cookie from react native/expo.我有一个基于 cookie 的身份验证系统,但我不明白如何从 react native/expo 获取 cookie。 I use js-cookie library for managing cookie.我使用js-cookie库来管理 cookie。

The request method:请求方法:

async  postData(url = '', data) {
      // Default options are marked with *
      const response = await fetch(url, {
        method: 'POST', // *GET, POST, PUT, DELETE, etc.
        mode: 'cors', // no-cors, *cors, same-origin
        cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
        credentials: 'same-origin', // include, *same-origin, omit
        headers: {
          'Content-Type': 'application/json',
          "CSRF-Token": Cookies.get("XSRF-TOKEN")
        },
        redirect: 'follow', // manual, *follow, error
        referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
        body: JSON.stringify({data}) // body data type must match "Content-Type" header
      });

      return response.json(); // parses JSON response into native JavaScript objects
}

when I consoloe.log(Cookies.get("XSRF-TOKEN")) it shows undefined .当我consoloe.log(Cookies.get("XSRF-TOKEN"))它显示undefined

This is my express backend:这是我的快递后端:

app.use(csrf({cookie: true}))
app.use("*", (req, res, next)=>{
    res.cookie("XSRF-TOKEN", req.csrfToken());
    next();
})

To get cookies in an expo project, you will first have to eject and then you can access the cookies for Android and iOS separately, as expo does not have any module that can help you get cookies. To get cookies in an expo project, you will first have to eject and then you can access the cookies for Android and iOS separately, as expo does not have any module that can help you get cookies.

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

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