I faced the same problem not long ago. And here is my solution (based on information from this forum).
Maybe some meteor gurus can show better way to do this.
Meteor.startup(function () {
DDPConnection = DDP.connect('<backend server>');
Meteor.connection = DDPConnection;
Accounts.connection = Meteor.connection;
Meteor.users = new Meteor.Collection('users', {connection: DDPConnection});
Meteor.connection.subscribe('users');
Tracker.autorun(function () {
var token = Session.get('_storedLoginToken');
if(token)
Meteor.loginWithToken(token, function(err){
if(!err) console.log('loginWithToken ',token);
if(err) {
// Using for displaying login errors in app
Session.set('ddpErrors', err);
}
});
});
Tracker.autorun(function(){
var user = Meteor.user();
console.log('autorun.user');
if(user)
{
// using u2622:persistent-session
Session.setPersistent('_storedLoginToken', Accounts._storedLoginToken());
}
});
Some comments:
- i’m not redefining methods ([ ‘subscribe’, ‘call’, ‘apply’, ‘methods’, ‘status’, ‘reconnect’, ‘disconnect’, ‘onReconnect’ ])
- i’m using u2622:persistent-session to store login token. You can use _localStorage, but for me u2622:persistent-session syntax is more clear.
- run cordova app as: meteor run --mobile-server
Cons: I tried meteor 1.2-rc.4, and this hack not work anymore. So if you need solution “here and now”, you can use this, otherwise just wait for 1.2 documentation.
Login event:
Template.mainLayout.events({
'click .login-button': function (e, t) {
e.preventDefault();
var email = t.find('.login-email').value,
password = t.find('.login-password').value;
Meteor.loginWithPassword(email, password, function (e) {
if (e) {
console.log(e);
// Using for displaying login errors in app
Session.set('ddpErrors', e);
}
if (Meteor.user()) {
console.log(Meteor.user());
}
return;
});
return false;
}
});