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.
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);
});
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);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With