在服务器端登录用户帐户

alw*_*lwc 4 meteor

假设我connectServer在服务器端调用了一个方法:

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);
         }
   }
});
Run Code Online (Sandbox Code Playgroud)

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

注意:我没有使用Meteor的客户端,所以我想在服务器端执行此操作.

Osk*_*kar 8

查看代码,您可以发出一个connectionLogin事件:

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

看起来好像要做的事情,但请记住,如果软件包更新后发生了变化,您的代码可能会在您没有注意到的情况下中断.

手指插入的下一个位置是Accounts包(因为meteor-user-status使用Accounts.onLogin()方法),但是我调查了它并且找不到一个简单的方法来做到这一点.

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

// 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);
});
Run Code Online (Sandbox Code Playgroud)