Login user account on server side

不打扰是莪最后的温柔 提交于 2020-01-01 12:09:19

问题


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);
         }
   }
});

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.

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


回答1:


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.

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);
});



回答2:


Simple process to authenticate user on server

find the user if exist from parameter email

var user = Meteor.users.findOne({
  'emails.address': data.email
  });

if the user is found

if (user) {
  //get paramter password
  var password = data.password;
  //authenticate user
  var result = Accounts._checkPassword(user, password);

Client call method

Meteor.call('loginUser', {email: "vxxxxx@xxxx.com", password: "123456"}, function (error, result) {
  if (!error) Meteor.loginWithToken(result.token);
});

Full code

// Server method
Meteor.methods({
  loginUser: function (data) {
    //find user if exist from paramter email
    var user = Meteor.users.findOne({
      'emails.address': data.email
    });

    //if user is found
    if (user) {
      //get paramter password
      var password = data.password;
      //authenticate user
      var result = Accounts._checkPassword(user, password);
      if (result.error) {
        return result.error;
      } else {
        return result;
      }
    } else {
      //if user is not found
      return {
        error: "user not found"
      }
    }
  }
});

Client call method

 Meteor.call('loginUser', {email: "vxxxxx@xxxx.com", password: "123456"}, function (error, result) {
      if (!error) Meteor.loginWithToken(result.token);
    });



回答3:


Can you add a callback to createUser and tell us what happens? I'm not sure if that will fix it but I'm pretty sure you need to set a password too, such as:

Accounts.createUser({
  email: data.email,
  password: data.password
  }, function(err, res){
    console.log(err, res);
  }
});

Otherwise I did not see your Accounts.sendEnrollmentEmail(userId, [email]) if you want to let users choose their passwords.



来源:https://stackoverflow.com/questions/34085553/login-user-account-on-server-side

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!