简体   繁体   English

解析node.js中的登录-登录成功,但是“没有当前用户”

[英]Parse Login in node.js - Login successful but 'There is no current user'

I'm having trouble interacting with my Parse data in node.js. 我无法与node.js中的解析数据进行交互。 I'm able to login successfully, but Parse.User.current() returns null. 我能够成功登录,但是Parse.User.current()返回null。 After running the below code, I'd like to query data that has ACL read/write only for that user. 运行以下代码后,我想查询仅对该用户具有ACL读/写操作的数据。 Currently, that query returns empty, but if I change that data to public read/write, I can see the results of the query output in the terminal. 当前,该查询返回空值,但是如果我将该数据更改为公共读/写,则可以在终端中看到查询输出的结果。

Here is my node.js code: 这是我的node.js代码:

Prompt.get([{
name: 'username', 
required: true}, {
name: 'password',
hidden: true}], function (err, result) {
    if (err) {
        console.log('Error: ' + err);
    } else {
        Parse.User.logIn(result.username, result.password, {
        success: function(user) {
            console.log('LOGGED IN');
            console.log(user);
            console.log(Parse.Session.current());
            console.log(Parse.User.current());

            ... (query happens below this)

And my console output: 和我的控制台输出:

prompt: username:  pablo
prompt: password:  
LOGGED IN
ParseUser { _objCount: 0, className: '_User', id: 'EXyg99egkv' }
ParsePromise {
  _resolved: false,
  _rejected: true,
  _resolvedCallbacks: [],
  _rejectedCallbacks: [],
  _error: 'There is no current user.' }
null

Thanks in advance. 提前致谢。

Is this not a usecase for Parse.User.become()? 这不是Parse.User.become()的用例吗? From the parse docs: 从解析文档中:

If you've created your own authentication routines, or otherwise logged in a user on the server side, you can now pass the session token to the client and use the become method. 如果您已经创建了自己的身份验证例程,或者以其他方式在服务器端登录了用户,则现在可以将会话令牌传递给客户端,并使用begin方法。 This method will ensure the session token is valid before setting the current user. 此方法将确保在设置当前用户之前会话令牌有效。

Parse.User.become("session-token-here").then(function (user) {
  // The current user is now set to user.
}, function (error) {
  // The token could not be validated.
});

I had similar problems and found this Parse blog that explains the issue: 我遇到了类似的问题,并找到了这个解释问题的Parse 博客

Also in Cloud Code, the concept of a method that returns the current user makes sense, as it does in JavaScript on a web page, because there's only one active request and only one user. 同样在Cloud Code中,返回当前用户的方法的概念很有意义,就像在网页上的JavaScript中一样,这是因为只有一个活动请求和一个用户。 However in a context like node.js, there can't be a global current user, which requires explicit passing of the session token. 但是,在诸如node.js之类的上下文中,不能有全局当前用户,这需要显式传递会话令牌。 Version 1.6 and higher of the Parse JavaScript SDK already requires this, so if you're at that version, you're safe in your library usage. 1.6版及更高版本的Parse JavaScript SDK已经需要此功能,因此,如果您使用的是该版本,则可以安全使用库。

You can execute queries with user credentials in a node.js environment like this: 您可以在node.js环境中使用用户凭据执行查询,如下所示:

query.find({ sessionToken: request.user.getSessionToken() }).then(function(data) {
// do stuff with data
}, function(error) {
// do stuff with error
});

If you wish to validate that token before using it, here's an explanation of how you could go about doing that: 如果您希望在使用令牌之前先对其进行验证,请按照以下说明进行操作:

one way would be to query for an object known to be only readable by the user. 一种方法是查询已知只能由用户读取的对象。 You could have a class that stores such objects, and have each one of them use an ACL that restricts read permissions to the user itself. 您可以拥有一个存储此类对象的类,并让每个对象使用ACL来限制对用户本身的读取权限。 If running a find query over this class returns 0 objects with a given sessionToken, you know it's not valid. 如果在此类上运行查找查询,则返回具有给定sessionToken的0个对象,这是无效的。 You can take it a step further and also compare the user object id to make sure it belongs to the right user. 您可以更进一步,还可以比较用户对象ID以确保它属于正确的用户。

Session tokens cannot be queried even with the master key. 即使使用主密钥也无法查询会话令牌。

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

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