比较来自世界各地的卖家的域名和 IT 服务价格

如何安装 cookie 在 React Native 通过 Expo?

我很难强制cookie工作,我在服务器端使用它们来检查用户,捕获它们 req.cookies.

以下是我现在在文件中安装它的方式 Login.js 在 React Native:


import Cookies from "universal-cookie";

const cookies = new Cookies//;
cookies.set/"token", token, {
expires: 7, // 7 days
path: "/"
//,secure: true // If served over HTTPS
}/;


当我打电话时,它很棒
cookies.get/"token"/

在本页。 但是,当我导入时,配置 const 并打电话 get 在另一个页面上,标记不会出现......

此外,当我制作这样的样本时:


fetch/"[url=http://192.168.0.115:3000/myTransactions/"]http://192.168.0.115:3000/myTransactions/"[/url], {
credentials: "same-origin",
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
}
}/


服务器未收到任何文件。 cookie. 我甚至改变了凭据 "使能够".

我用 expo 和我的 terminal 上 Windows, 要开始它而不使用 android studio 或者 xcode. 我不确定它是可能的。

有什么想法吗!

谢谢
已邀请:

石油百科

赞同来自:

几件事......
react-native-keychain... 比饼干更安全!
https://github.com/oblador/react-native-keychain
其次,你尝试过 AsyncStorage? 从本质上讲,这是内置的反应性自然 "localStorage" 年。 我认为这就是你要找的东西。

https://facebook.github.io/rea ... .html

// to set
AsyncStorage.setItem/'@app:session', token/;

// to get
AsyncStorage.getItem/'@app:session'/.then/token => {
// use token
}/;


UPDATES 为了 OP
如果您的设置如下,当您刚刚通过标题发送令牌值时,您可以以任何格式存储此信息......最安全/方便的。
在这个例子中 auth 任何这些参数都可以选择....


localStorage.set/'token', 38573875ihdkhai/
createCookie/'ppkcookie' 7asdfasdf/;

export const userFetchRequest = // => /dispatch, getState/ => {
let {auth} = getState//;
return superagent.get/`${__API_URL__}/user/me`/
.set/'Authorization', `Bearer ${auth}`/
.then/res => {
dispatch/userSet/res.body//;
return res;
}/;
};

龙天

赞同来自:

您可以使用文件。 cookie 或者只是保持令牌

localStorage

对于Web应用程序。

但对于申请 React Native 使用 AsyncStorage

它是

将是最好的解决方案。

关于

AsyncStorage

:

AsyncStorage简单,未加密,异步,永久性
应用程序的键存储系统和值为全局应用程序。 它应该使用
反而 LocalStorage.

上 iOS, AsyncStorage 支持存储小值的机器代码
在单独文件中的序列化字典和大值中。 在这一点
Android, AsyncStorage 将使用 RocksDB, 或者 SQLite 取决于什么
可用的。

而且, AsyncStorage 它很简单 API:


const TOKEN = "TOKEN";

// save token
AsyncStorage.setItem/TOKEN, token/;

// get token
AsyncStorage.getItem/TOKEN/.then/token => {
// token value could be used
}/;


如果你担心

安全 AsyncStorage, 您可以找到更多详细信息
https://coderoad.ru/39028755/
.

要回复问题请先登录注册