We’re doing what @gdc3 is doing, which is that we have the user’s app and the admin’s app, connected to the same database. I searched high and low and wanted to do it without a package (many didn’t seem well-maintained), and many of the articles I found were incomplete. Here’s where I landed.
Early in the lifecycle we import /imports/api/ddp-connect.js
. That file looks like:
import { Meteor } from 'meteor/meteor';
import { Accounts } from 'meteor/accounts-base';
import { DDP } from 'meteor/ddp-client'
var remoteUrl;
if(Meteor.isProduction){
remoteUrl = 'https://subdomain.example.com';
} else {
remoteUrl = 'http://localhost:3000';
}
Meteor.remoteConnection = DDP.connect(remoteUrl);
Accounts.connection = Meteor.remoteConnection;
Meteor.users = new Mongo.Collection('users', {
connection: Meteor.remoteConnection
});
if(Meteor.isClient){
var loggingIn = false;
Accounts.onLogin(() => {
var loginToken = Accounts._storedLoginToken();
// Use the accounts loginToken to login
if (loginToken) {
if(!loggingIn){
loggingIn = true; // prevents multiple attempts from this re-running
Meteor.loginWithToken(loginToken, function(loginErr, result){
if (loginErr) {
console.log(loginErr.message);
} else {
if (result.type === 'resume') {
FlowRouter.go('/main');
}
}
});
}
} else {
console.log('no log in token');
}
});
}
export var remote = Meteor.remoteConnection;
As for collections, those look like:
import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { remote } from './ddp-connect.js';
export var someCollection = new Mongo.Collection('someCollection', {
connection: remote
});
Two other important bits: 1.) we have no defined Methods on the Admin app. We just call the main app’s Methods:
Meteor.remoteConnection.call('theOtherServerMethod', function(err, res){
// stuff
});
And 2.) the secondary app is started by:
MONGO_URL=mongodb://127.0.0.1:3001/meteor meteor --port 4000
I think the difference here compared to your setup is that logging into the main app does not auto-authenticate you into the Admin app. We wanted extra authentication steps for Admin, so that was fine for us. But this approach allowed us to fully use the “two apps one db” scenario.
(p.s. If anyone sees anything insecure about this approach, I’d love to be made aware.)