From b80a93d6d5eda97cc8ed38296b9e93a7da005db7 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Thu, 4 Nov 2021 14:03:18 +0100 Subject: [PATCH] cookie based approach --- packages/auth/src/constants.js | 1 + .../components/start/CreateAppModal.svelte | 2 +- .../builder/src/pages/builder/_layout.svelte | 7 ++++++- .../src/pages/builder/auth/_layout.svelte | 6 +----- .../pages/builder/portal/apps/index.svelte | 7 ++++--- packages/builder/src/stores/portal/auth.js | 21 +++++++------------ .../worker/src/api/controllers/global/auth.js | 11 ++++++++++ packages/worker/src/api/routes/global/auth.js | 2 ++ 8 files changed, 33 insertions(+), 24 deletions(-) diff --git a/packages/auth/src/constants.js b/packages/auth/src/constants.js index 4b4aef5a42..9892275bec 100644 --- a/packages/auth/src/constants.js +++ b/packages/auth/src/constants.js @@ -6,6 +6,7 @@ exports.UserStatus = { exports.Cookies = { CurrentApp: "budibase:currentapp", Auth: "budibase:auth", + Init: "budibase:init", OIDC_CONFIG: "budibase:oidc:config", } diff --git a/packages/builder/src/components/start/CreateAppModal.svelte b/packages/builder/src/components/start/CreateAppModal.svelte index d25acf2495..1db5e46261 100644 --- a/packages/builder/src/components/start/CreateAppModal.svelte +++ b/packages/builder/src/components/start/CreateAppModal.svelte @@ -139,7 +139,7 @@ } const userResp = await api.post(`/api/users/metadata/self`, user) await userResp.json() - auth.resetInitTemplate() + await auth.setInitInfo({}) $goto(`/builder/app/${appJson.instance._id}`) } catch (error) { console.error(error) diff --git a/packages/builder/src/pages/builder/_layout.svelte b/packages/builder/src/pages/builder/_layout.svelte index 39c93f0fb0..9ef171d7c7 100644 --- a/packages/builder/src/pages/builder/_layout.svelte +++ b/packages/builder/src/pages/builder/_layout.svelte @@ -1,5 +1,5 @@ diff --git a/packages/builder/src/pages/builder/portal/apps/index.svelte b/packages/builder/src/pages/builder/portal/apps/index.svelte index 94198caeab..f9aac6e0c1 100644 --- a/packages/builder/src/pages/builder/portal/apps/index.svelte +++ b/packages/builder/src/pages/builder/portal/apps/index.svelte @@ -201,9 +201,10 @@ await apps.load() loaded = true // if the portal is loaded from an external URL with a template param - const templateKey = $auth.initTemplate - if (templateKey) { - createAppFromTemplateUrl(templateKey) + const initInfo = await auth.getInitInfo() + console.log(initInfo) + if (initInfo.init_template) { + createAppFromTemplateUrl(initInfo.init_template) } }) diff --git a/packages/builder/src/stores/portal/auth.js b/packages/builder/src/stores/portal/auth.js index 9723b64ffc..47ebbfe7ef 100644 --- a/packages/builder/src/stores/portal/auth.js +++ b/packages/builder/src/stores/portal/auth.js @@ -33,7 +33,6 @@ export function createAuthStore() { user: $store.user, tenantId: $store.tenantId, tenantSet: $store.tenantSet, - initTemplate: $store.initTemplate, loaded: $store.loaded, initials, isAdmin, @@ -81,28 +80,22 @@ export function createAuthStore() { } } - function updateInitTemplate(template) { - auth.update(store => { - store.initTemplate = template - return store - }) - } - return { subscribe: store.subscribe, - resetInitTemplate: () => updateInitTemplate(null), setOrganisation: setOrganisation, + getInitInfo: async () => { + const response = await api.get(`/api/global/auth/init`) + return await response.json() + }, + setInitInfo: async info => { + await api.post(`/api/global/auth/init`, info) + }, checkQueryString: async () => { const urlParams = new URLSearchParams(window.location.search) if (urlParams.has("tenantId")) { const tenantId = urlParams.get("tenantId") await setOrganisation(tenantId) } - - // set the template to create an app from - if (urlParams.has("template")) { - updateInitTemplate(urlParams.get("template")) - } }, setOrg: async tenantId => { await setOrganisation(tenantId) diff --git a/packages/worker/src/api/controllers/global/auth.js b/packages/worker/src/api/controllers/global/auth.js index 50b4ec969b..e111619041 100644 --- a/packages/worker/src/api/controllers/global/auth.js +++ b/packages/worker/src/api/controllers/global/auth.js @@ -77,6 +77,17 @@ exports.authenticate = async (ctx, next) => { })(ctx, next) } +exports.setInitInfo = ctx => { + const initInfo = ctx.request.body + setCookie(ctx, initInfo, Cookies.Init) + ctx.status = 200 +} + +exports.getInitInfo = ctx => { + const initInfo = getCookie(ctx, Cookies.Init) + ctx.body = initInfo +} + /** * Reset the user password, used as part of a forgotten password flow. */ diff --git a/packages/worker/src/api/routes/global/auth.js b/packages/worker/src/api/routes/global/auth.js index 9cd77ce153..7baad60ecd 100644 --- a/packages/worker/src/api/routes/global/auth.js +++ b/packages/worker/src/api/routes/global/auth.js @@ -56,6 +56,8 @@ router authController.resetUpdate ) .post("/api/global/auth/logout", authController.logout) + .post("/api/global/auth/init", authController.setInitInfo) + .get("/api/global/auth/init", authController.getInitInfo) .get( "/api/global/auth/:tenantId/google", updateTenant,