Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Login user account on server side

Tags:

meteor

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.

like image 554
alwc Avatar asked Dec 04 '22 01:12

alwc


2 Answers

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);
});
like image 140
Oskar Avatar answered Feb 19 '23 20:02

Oskar


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: "[email protected]", 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: "[email protected]", password: "123456"}, function (error, result) {
      if (!error) Meteor.loginWithToken(result.token);
    });
like image 30
Vishal Wayachal Avatar answered Feb 19 '23 22:02

Vishal Wayachal