[英]React Native how to get cookie
我有一個基於 cookie 的身份驗證系統,但我不明白如何從 react native/expo 獲取 cookie。 我使用js-cookie
庫來管理 cookie。
請求方法:
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
}
當我consoloe.log(Cookies.get("XSRF-TOKEN"))
它顯示undefined
。
這是我的快遞后端:
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.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.