diff --git a/packages/auth/src/db/utils.js b/packages/auth/src/db/utils.js index 259f24406c..1eaf9ddfb6 100644 --- a/packages/auth/src/db/utils.js +++ b/packages/auth/src/db/utils.js @@ -2,6 +2,7 @@ const { newid } = require("../hashing") const Replication = require("./Replication") const { getDB } = require("./index") const { DEFAULT_TENANT_ID } = require("../constants") +const env = require("../environment") const UNICODE_MAX = "\ufff0" const SEPARATOR = "_" @@ -82,6 +83,9 @@ exports.getGlobalDB = tenantId => { if (tenantId && tenantId !== DEFAULT_TENANT_ID) { dbName = `${tenantId}${SEPARATOR}${dbName}` } + if (env.MULTI_TENANCY && tenantId == null) { + throw "Cannot create global DB without tenantId" + } return getDB(dbName) } @@ -210,7 +214,7 @@ exports.getDeployedAppID = appId => { * @return {Promise} returns the app information document stored in each app database. */ exports.getAllApps = async (CouchDB, { tenantId, dev, all } = {}) => { - if (!tenantId) { + if (!env.MULTI_TENANCY && !tenantId) { tenantId = DEFAULT_TENANT_ID } let allDbs = await CouchDB.allDbs() diff --git a/packages/auth/src/environment.js b/packages/auth/src/environment.js index 355843d02d..e12918f3ac 100644 --- a/packages/auth/src/environment.js +++ b/packages/auth/src/environment.js @@ -16,6 +16,7 @@ module.exports = { MINIO_SECRET_KEY: process.env.MINIO_SECRET_KEY, MINIO_URL: process.env.MINIO_URL, INTERNAL_API_KEY: process.env.INTERNAL_API_KEY, + MULTI_TENANCY: process.env.MULTI_TENANCY, isTest, _set(key, value) { process.env[key] = value diff --git a/packages/auth/src/utils.js b/packages/auth/src/utils.js index 00a3b55a21..dd03f132a1 100644 --- a/packages/auth/src/utils.js +++ b/packages/auth/src/utils.js @@ -10,6 +10,7 @@ const { createUserEmailView } = require("./db/views") const { getDB } = require("./db") const { getGlobalDB } = require("./db/utils") const { DEFAULT_TENANT_ID, Headers } = require("./constants") +const env = require("./environment") const APP_PREFIX = DocumentTypes.APP + SEPARATOR @@ -104,7 +105,7 @@ exports.isClient = ctx => { exports.lookupTenantId = async userId => { const db = getDB(StaticDatabases.PLATFORM_INFO.name) - let tenantId = DEFAULT_TENANT_ID + let tenantId = env.MULTI_TENANCY ? DEFAULT_TENANT_ID : null try { const doc = await db.get(userId) if (doc && doc.tenantId) { diff --git a/packages/builder/src/pages/builder/_layout.svelte b/packages/builder/src/pages/builder/_layout.svelte index ce883ee541..9ea21317a7 100644 --- a/packages/builder/src/pages/builder/_layout.svelte +++ b/packages/builder/src/pages/builder/_layout.svelte @@ -16,9 +16,10 @@ // Force creation of an admin user if one doesn't exist $: { - if (loaded && multiTenancyEnabled && !tenantSet) { + const apiReady = $admin.loaded && $auth.loaded + if (loaded && apiReady && multiTenancyEnabled && !tenantSet) { $redirect("./auth/org") - } else if (loaded && !hasAdminUser) { + } else if (loaded && apiReady && !hasAdminUser) { $redirect("./admin") } } diff --git a/packages/builder/src/pages/builder/auth/org.svelte b/packages/builder/src/pages/builder/auth/org.svelte index 0e0fd27c78..7e26924423 100644 --- a/packages/builder/src/pages/builder/auth/org.svelte +++ b/packages/builder/src/pages/builder/auth/org.svelte @@ -27,6 +27,8 @@ auth.checkQueryString() if (!multiTenancyEnabled) { $goto("../") + } else { + admin.unload() } }) diff --git a/packages/builder/src/stores/portal/admin.js b/packages/builder/src/stores/portal/admin.js index 96e67ea84b..cec869eb02 100644 --- a/packages/builder/src/stores/portal/admin.js +++ b/packages/builder/src/stores/portal/admin.js @@ -3,7 +3,9 @@ import api from "builderStore/api" import { auth } from "stores/portal" export function createAdminStore() { - const admin = writable({}) + const admin = writable({ + loaded: false, + }) async function init() { try { @@ -20,13 +22,14 @@ export function createAdminStore() { 0 ) + await multiTenancyEnabled() admin.update(store => { + store.loaded = true store.checklist = json store.onboardingProgress = (stepsComplete / onboardingSteps.length) * 100 return store }) - await multiTenancyEnabled() } catch (err) { admin.update(store => { store.checklist = null @@ -51,9 +54,17 @@ export function createAdminStore() { return enabled } + function unload() { + admin.update(store => { + store.loaded = false + return store + }) + } + return { subscribe: admin.subscribe, init, + unload, } } diff --git a/packages/builder/src/stores/portal/auth.js b/packages/builder/src/stores/portal/auth.js index aa856d4a29..b8eec0730e 100644 --- a/packages/builder/src/stores/portal/auth.js +++ b/packages/builder/src/stores/portal/auth.js @@ -6,6 +6,7 @@ export function createAuthStore() { user: null, tenantId: "default", tenantSet: false, + loaded: false, }) const store = derived(auth, $store => { let initials = null @@ -30,6 +31,7 @@ export function createAuthStore() { user: $store.user, tenantId: $store.tenantId, tenantSet: $store.tenantSet, + loaded: $store.loaded, initials, isAdmin, isBuilder, @@ -38,6 +40,7 @@ export function createAuthStore() { function setUser(user) { auth.update(store => { + store.loaded = true store.user = user if (user) { store.tenantId = user.tenantId || "default"