Skip to content

Instantly share code, notes, and snippets.

@paulomcnally
Last active March 6, 2019 13:31
Show Gist options
  • Save paulomcnally/0fea84f0c5ed7fce70fd26d134c65c5f to your computer and use it in GitHub Desktop.
Save paulomcnally/0fea84f0c5ed7fce70fd26d134c65c5f to your computer and use it in GitHub Desktop.
Create file server/boot/subdomain.js and edit server/model-config.json to add "tenant": true property on every model. Only work with loopback-connector-postgresql and schema.
'use strict';
var modelConfig = require('../model-config.json');
module.exports = function(app) {
app.use(function(req, res, next) {
// set tenant based subdomain or use public
var subdomain = (req.subdomains.length > 1) ? req.subdomains.pop() : req.subdomains[0];
var tenant = req.subdomains.pop() || 'public';
// we go through all the model names from model-config.json
Object.keys(modelConfig).forEach(function(modelName) {
// validate if model have property tenant
if (modelConfig[modelName].hasOwnProperty('tenant')) {
// set postgresql schema for model
app.models[modelName].definition.settings.postgresql.schema = tenant;
}
});
next();
});
};
@evrend
Copy link

evrend commented Jun 13, 2018

Hi, l want ask a question. User logged in and changed subdomain with an another tenant subdomain name. Your middleware will change tenant scheme with unauthorized scheme? am I wrong?

@RiccardoBadagliacco
Copy link

i have same problem. I have 2 tenant with model user. If a user is logged in a tenant, other users are dislodged in other tenants

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment