在服务器端登录用户帐户 [英] Login user account on server side

查看:53
本文介绍了在服务器端登录用户帐户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在服务器端有一个名为 connectServer 的方法:

Let say I have a method called connectServer on the server side:

Meteor.methods({
   connectServer: function(data) {
         <check if connection has valid data.accessToken from RESTful end point>
         if (valid) {
              var userId = Accounts.createUser({"email": data.email});
              this.setUserId(userId);
         }
   }
});

此方法的问题在于它似乎不会在服务器上触发任何登录连接"操作.我目前正在使用 meteor-user-status 并且没有调用事件 UserStatus.events.on("connectionLogin", function(fields) { ... })this.setUserId(userId) 更新时.有什么办法可以在服务器上手动触发登录连接"操作?谢谢.

The problem with this method is that it doesn't seem to trigger any 'login connection' actions on server. I'm currently using meteor-user-status and the event UserStatus.events.on("connectionLogin", function(fields) { ... }) is not called when this.setUserId(userId) has updated. Is there any way I can manually trigger a 'login connection' action on server? Thanks.

注意:我没有使用 Meteor 的客户端,所以我想在服务器端这样做.

Note: I'm not using Meteor's client, so I would like to do this on the server side.

推荐答案

code,你可以发出一个 connectionLogin 事件:

Looking at the code, you could emit a connectionLogin event:

UserStatus.events.emit("connectionLogin", {
    userId: userId
    connectionId: connection.id
    ipAddr: connection.clientAddress
    userAgent: connection.httpHeaders['user-agent']
    loginTime: date
});

这似乎是一件可以做的事情,但请记住,如果包更新并且某些内容发生更改,您的代码可能会在您不注意的情况下崩溃.

Seems like an OK thing to do, but bear in mind that if the package is updated and something changes, your code might break without you even noticing.

下一个需要插入的地方是 Accounts 包(因为 meteor-user-status 使用 Accounts.onLogin()> 方法),但是我研究了它并找不到一种简单的方法来做到这一点.

The next place to stick your finger into would be the Accounts package (since meteor-user-status uses the Accounts.onLogin() method), however I looked into it and couldn't find an easy way to do that.

您的最后一个选择是在客户端登录用户.您可以做的是生成一个令牌并允许客户端使用此令牌登录.例如:

Your last option is to log the user in client-side. What you could do is generate a token and allow the client to log in with this token. E.g:

// Server method
Meteor.methods({
   connectServer: function(data) {
         <check if connection has valid acess token>
         if (valid) {
              var userId = Accounts.createUser({"email": data.email});
              var stampedLoginToken = Accounts._generateStampedLoginToken();
              Accounts._insertLoginToken(userId, stampedLoginToken);
              return stampedLoginToken;
         }
   }
});
// Client
Meteor.call('connectServer', function(error, result){
    if(!error) Meteor.loginWithToken(result.token);
});

这篇关于在服务器端登录用户帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆