Appropriate way to re-login to remote server? (1.3)

So I have a DDP connection for my users. After reconnection, it seems like the user is always logged out. So I tried this.

import {DDP} from 'meteor/ddp';
import {Meteor} from 'meteor/meteor';
import {Accounts} from 'meteor/accounts-base';

// some other garbage omitted
export const Remote = DDP.connect(Meteor.settings.public.remoteUrl);
Accounts.connection = Remote;

Accounts.onLogin(function () {
  Meteor._localStorage.setItem('login token', Accounts._storedLoginToken());

Remote.onReconnect = function () {
  if (Meteor.isClient) {
    const token = Meteor._localStorage.getItem('login token');
    if (token) {

export default Remote;

Is this the appropriate way to manage it? It seems this.userId is not set in publications on my current server

I’m doing something similar, and decided to create a Meteor.method for this.

In this method, I’m validating the token, and use this.setUserId(userId) to set the userId on the server. In the method callback (on the client), I use Meteor.connection.setUserId(userId); to fix the userId on the client.

Simplified version:

// server
  onReconnect: ({ token, userId }) => {
    // validate token

// client
Remote.onReconnect = () => {
  const token = Meteor._localStorage.getItem('login token');
  if (token) {'onReconnect', { token, userId }, (error, result) => {
      // if all okay

Although, I’m using it to restore an old session and not to restore a remote session. But I assume it works the same?

1 Like

that actually solves two of my problems; thank you sir!