简体   繁体   English

如何在 React Native 中使用 Expo 设置 cookie?

[英]How to set cookie in React Native with Expo?

I am having a hard time getting cookies to work, on the server side I use them to verify the user by grabbing them from req.cookies.我很难让 cookie 工作,在服务器端,我使用它们通过从 req.cookies 中获取它们来验证用户。

Here is how I currently set it on the Login.js file in React Native:这是我目前在 React Native 中的 Login.js 文件中设置它的方式:

import Cookies from "universal-cookie";

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

This works fine when I call a cookies.get("token") on this page.当我在此页面上调用cookies.get("token")时,这工作正常。 However, when I import, setup the const, and call get on another page the token does not show up...但是,当我导入、设置常量并在另一个页面上调用 get 时,令牌没有显示......

Also, when I make the fetch like this:另外,当我像这样进行提取时:

fetch("http://192.168.0.115:3000/myTransactions/", {
      credentials: "same-origin",
      method: "POST",
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json"
      }
    })

The server does not receive any cookies.服务器不会收到任何 cookie。 I even changed credentials to say include.我什至更改了凭据以说包含。

I am using expo and my terminal on Windows to run it without having to use android studio or xcode.我在 Windows 上使用 expo 和我的终端来运行它,而无需使用 android studio 或 xcode。 I am not sure if that is a possible issue.我不确定这是否是一个可能的问题。

Any thoughts!有什么想法吗!

Thanks谢谢

A couple things... react-native-keychain... safer than cookies!有几件事... react-native-keychain ... 比 cookie 更安全! https://github.com/oblador/react-native-keychain https://github.com/oblador/react-native-keychain

Second, have you tried AsyncStorage?其次,您是否尝试过 AsyncStorage? This is React-natives built in "localStorage" in essence.这本质上是内置在“localStorage”中的 React-native。 I think it's what you're looking for.我想这就是你要找的。

https://facebook.github.io/react-native/docs/asyncstorage.html https://facebook.github.io/react-native/docs/asyncstorage.html

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

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

UPDATES for OP If your setup looks like this, where you simply are sending the token value through as headers, you can store this information in whatever format is... safest/most convenient. OP 的更新如果您的设置看起来像这样,您只是将令牌值作为标头发送,您可以以任何格式存储此信息......最安全/最方便。 In this example, auth could be either of these options fetched....在这个例子中, 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;
    });
};

You can use cookies or just store a token in a localStorage for the web app.您可以使用 cookie 或仅将令牌存储在 web 应用程序的localStorage中。

But for the React Native app usage of AsyncStorage would be the better solution.但是对于 React Native 应用程序,使用AsyncStorage将是更好的解决方案。

About AsyncStorage :关于异步存储

AsyncStorage is a simple, unencrypted, asynchronous, persistent, key-value storage system that is global to the app. AsyncStorage 是一个简单的、未加密的、异步的、持久的、键值存储系统,对应用程序是全局的。 It should be used instead of LocalStorage.应该使用它代替 LocalStorage。

On iOS, AsyncStorage is backed by native code that stores small values in a serialized dictionary and larger values in separate files.在 iOS 上,AsyncStorage 由本机代码支持,该代码将小值存储在序列化字典中,并将较大值存储在单独的文件中。 On Android, AsyncStorage will use either RocksDB or SQLite based on what is available.在 Android 上,AsyncStorage 将根据可用内容使用 RocksDB 或 SQLite。

Also, AsyncStorage has quite simple API:此外,AsyncStorage 有非常简单的 API:

const TOKEN = "TOKEN";

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

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

If you are bothering about AsyncStorage security you can find more information by the link .如果您担心AsyncStorage安全性,您可以通过链接找到更多信息。

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

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