简体   繁体   English

如何从Meteor中的服务器获取浏览器cookie以进行会话处理?

[英]How can I get a browser cookie from the server in Meteor for session handling?

I am currently re-writing a PHP+Mongodb application in Meteor. 我目前正在Meteor中重写一个PHP + Mongodb应用程序。

In the application, a session cookie that contains only a unique identifier is used. 在应用程序中,使用仅包含唯一标识符的会话cookie。 The server gets the browser's cookie and uses its value to load data from a collection. 服务器获取浏览器的cookie并使用其值从集合中加载数据。 This is useful for knowing the client's current state. 这对于了解客户端的当前状态非常有用。 Using Meteor I need to be able to get the value of the browser cookie from the server code. 使用Meteor我需要能够从服务器代码中获取浏览器cookie的值。 How can I accomplish this? 我怎么能做到这一点?

In PHP, one might do it like so: 在PHP中,人们可能会这样做:

if(isset($_COOKIE["cookie_name"])) {
    //there is a browser cookie set with a name "cookie_name", 
    //and now I can act on that cookie's value, straight from the server
    echo $_COOKIE["cookie_name"];
}

I'm not sure if meteor's Session is what I'm looking for mostly because: 我不确定meteor的Session是否是我正在寻找的主要是因为:

  • It doesn't seem to persist between page reloads (it creates a fresh session each reload) 它似乎不会在页面重新加载之间持续存在(它会在每次重新加载时创建一个新的会话)

  • There must be a way to disconnect the session by simply deleting the browser cookie 必须有一种方法可以通过简单地删除浏览器cookie来断开会话

I'd like to handle this on the server because I want my sessions data to be private. 我想在服务器上处理这个,因为我希望我的会话数据是私有的。 Data about a session that isn't presented through a view (except for the session's unique identifier) must never be sent to the client. 不得通过视图呈现的会话数据(会话的唯一标识符除外)不得发送给客户端。

If I'm understanding correctly, you don't actually care about the cookie, you care about having user-specific data. 如果我理解正确,你实际上并不关心cookie,你关心拥有特定于用户的数据。

Comparison to PHP 与PHP的比较

Meteor clients communicate with the server via DDP which is an abstraction on top of http. Meteor客户端通过DDP与服务器进行通信,DDP是http之上的抽象。 Things like 'cookies' don't exist in the DDP level. DDP级别中不存在“cookies”之类的内容。 Rather, you have access to powerful constructs like sync'd database collections and built-in remote procedure calls. 相反,您可以访问强大的构造,如同步数据库集合和内置远程过程调用。

Meteor's Session object is a client-only concept that is designed for reactivity. Meteor的Session对象是一个仅用于客户端的概念,专为反应性而设计。 It is not persisted between client visits and the server does not have access to it. 它不会在客户端访问之间持久存在,并且服务器无权访问它。

The rough equivalent to PHP's SESSION is a Meteor Collection, which is actually more durable than PHP's SESSION because it is persisted to the database. 与PHP的SESSION相当的粗略是Meteor Collection,它实际上比PHP的SESSION更持久,因为它持久存储到数据库中。

User-specific data 用户特定数据

Tracking user-specific data like you want in Meteor can be broken down into two parts: 跟踪Meteor中您想要的特定用户数据可以分为两部分:

  1. authenticated users 经过身份验证的用
  2. anonymous users 匿名用户

Re: #1 - authenticated users 回复:#1 - 经过身份验证的用户

As @Tarang and @Cuberto have pointed out, the Meteor Accounts system (ex. accounts-password) has the concept of user-specific data built-in. 正如@Tarang和@Cuberto所指出的那样,Meteor Accounts系统(例如accounts-password)具有内置用户特定数据的概念。 It creates and manages the Meteor.users collection for you and provides the Meteor.user() function for getting an object specific to that user. 它为您创建和管理Meteor.users集合,并提供Meteor.user()函数以获取特定于该用户的对象。 It even has a built-in method for user-modifiable data in the profile field of the user object. 它甚至在用户对象的profile字段中具有用户可修改数据的内置方法。 The profile field is automatically published and is reactive as well (since Meteor.user() is reactive). profile字段会自动发布并且也是被动的(因为Meteor.user()是被动的)。

function doSomething () {
  var currentUser = Meteor.user(),
      profile;

  if (!currentUser) {
    // handle 'not authenticated' case
  } else {
    // already logged in
    profile = currentUser.profile || {name:'<not set>'};
    console.log('user ', profile.name, ' wants to doSomething');
  }
}

You can build your own authentication method but that seems like a recipe for disaster. 您可以构建自己的身份验证方法,但这似乎是灾难的一个方法。 Easier to write a script that converts from your existing DB structure to the Meteor Accounts structure and do it once in a big dump when you are ready to migrate your users over. 当您准备好迁移用户时,更容易编写从现有数据库结构转换为Meteor Accounts结构的脚本,并在大转储中执行一次。

So the Meteor convention is: 所以流星大会是:

  • User-specific data that the user should be able to modify goes in the user.profile field. 用户应该能够修改的用户特定数据位于user.profile字段中。 Ex. 防爆。 user.profile.firstname , user.profile.lastname user.profile.firstnameuser.profile.lastname
  • User-specific data that is restricted should go on the root user object. 受限制的用户特定数据应该位于root user对象上。
    Ex. 防爆。 The meteor-roles package stores user roles in a restricted, user.roles field. meteor-roles包将用户角色存储在受限制的user.roles字段中。

Here are the relevant docs: http://docs.meteor.com/#meteor_user 以下是相关文档: http//docs.meteor.com/#meteor_user

Re: #2 - anonymous users 回复:#2 - 匿名用户

Meteor Accounts does not track anonymous users so you will need to track them yourself. Meteor Accounts不会跟踪匿名用户,因此您需要自己跟踪它们。 You can use various methods to do this but the core is to store some identifying token on the client's machine in client code (either into localStorage or a cookie). 您可以使用各种方法来执行此操作,但核心是在客户端代码中将一些标识令牌存储在客户端代码中(存入localStorage或cookie)。

If you don't need to store user-specific data on the server and only want to change client-side stuff, such as what the users see, then you can do everything from the client. 如果您不需要在服务器上存储特定于用户的数据,并且只想更改客户端的内容,例如用户看到的内容,那么您可以从客户端执行所有操作。

If you need to store data on the server for anonymous users then you'll have to send the identifying token to the server along with each Meteor method call or database interaction (essentially what PHP does with the SESSION cookie). 如果您需要在服务器上为匿名用户存储数据,那么您必须将识别令牌与每个Meteor方法调用或数据库交互一起发送到服务器(实质上是PHP对SESSION cookie的作用)。 On the server, create a Collection called 'anonymousData' which will contain all of the user-specific info for your anonymous users, keyed by id token. 在服务器上,创建一个名为“anonymousData”的集合,该集合将包含匿名用户的所有用户特定信息,由id标记键入。 The server-side functions can query that Collection with the id token the client passes to retrieve user-specific info for that user. 服务器端函数可以使用客户端传递的id标记查询该Collection,以检索该用户的特定于用户的信息。

Keep in mind that if the user clears their cookies or deletes localStorage that data will be orphaned so some kind of a last-used check is important. 请记住,如果用户清除其cookie或删除localStorage,则数据将成为孤立状态,因此某些最后使用的检查很重要。

You would have to parse the headers out. 你必须解析标题。 Look for a package called ip on atmosphere. 在大气中寻找一个名为ip的包。 This is trickier than it sounds though. 这听起来比它听起来更棘手。

One thing you could do is instead of using cookie's use localStorage. 你可以做的一件事是使用localStorage而不是使用cookie。

Try localStorage 试试localStorage

localStorage.setItem("name", "value");

and to get a value: 并获得一个值:

localStorage.getItem("name");

Meteor already uses localStorage to store the user's logged in state & ID Meteor已经使用localStorage来存储用户的登录状态和ID

The cookie information is contained in the headers section of the response object of a HTTP call done with Meteor.http.call() called from the client using Meteor.call() . Cookie信息被包含在与做了HTTP调用的响应对象的报头部分Meteor.http.call()使用从客户端调用Meteor.call() set-cookie is an array containing any cookies sent over from the server. set-cookie是一个包含从服务器发送的任何cookie的数组。 Here's a screenshot of one of the results: 以下是其中一个结果的屏幕截图:

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

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