From c248844a24ea677b96378b482e7b71dc84023f71 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 16 Jul 2021 18:24:32 +0100 Subject: [PATCH] Adding organisation page. --- packages/auth/src/db/utils.js | 3 +- .../builder/src/pages/builder/_layout.svelte | 2 +- .../src/pages/builder/auth/index.svelte | 3 +- .../src/pages/builder/auth/login.svelte | 3 - .../builder/src/pages/builder/auth/org.svelte | 52 ++++------------- packages/builder/src/stores/portal/admin.js | 4 +- packages/builder/src/stores/portal/auth.js | 58 ++++++++++++------- .../src/api/controllers/admin/configs.js | 1 - .../worker/src/api/controllers/admin/users.js | 2 +- 9 files changed, 54 insertions(+), 74 deletions(-) diff --git a/packages/auth/src/db/utils.js b/packages/auth/src/db/utils.js index fd2395c8f5..6285649045 100644 --- a/packages/auth/src/db/utils.js +++ b/packages/auth/src/db/utils.js @@ -210,7 +210,7 @@ exports.getAllApps = async ({ tenantId, dev, all } = {}) => { } else { const response = await Promise.allSettled(appPromises) const apps = response - .filter(result => result.status === "fulfilled" ) + .filter(result => result.status === "fulfilled") .map(({ value }) => value) .filter(app => { const appTenant = !app.tenantId ? DEFAULT_TENANT_ID : app.tenantId @@ -218,7 +218,6 @@ exports.getAllApps = async ({ tenantId, dev, all } = {}) => { }) if (!all) { return apps.filter(app => { - if (dev) { return isDevApp(app) } diff --git a/packages/builder/src/pages/builder/_layout.svelte b/packages/builder/src/pages/builder/_layout.svelte index 43a5d205d9..cde1d332b2 100644 --- a/packages/builder/src/pages/builder/_layout.svelte +++ b/packages/builder/src/pages/builder/_layout.svelte @@ -29,7 +29,7 @@ !$isActive("./invite") ) { const returnUrl = encodeURIComponent(window.location.pathname) - $redirect("./auth/login?", { returnUrl }) + $redirect("./auth?", { returnUrl }) } else if ($auth?.user?.forceResetPassword) { $redirect("./auth/reset") } diff --git a/packages/builder/src/pages/builder/auth/index.svelte b/packages/builder/src/pages/builder/auth/index.svelte index 12570aeeb5..d335c3dfba 100644 --- a/packages/builder/src/pages/builder/auth/index.svelte +++ b/packages/builder/src/pages/builder/auth/index.svelte @@ -1,4 +1,5 @@ diff --git a/packages/builder/src/pages/builder/auth/login.svelte b/packages/builder/src/pages/builder/auth/login.svelte index 88a87e1739..9fb984c73e 100644 --- a/packages/builder/src/pages/builder/auth/login.svelte +++ b/packages/builder/src/pages/builder/auth/login.svelte @@ -15,7 +15,6 @@ import Logo from "assets/bb-emblem.svg" import { onMount } from "svelte" - let tenantId = "" let username = "" let password = "" @@ -26,7 +25,6 @@ await auth.login({ username, password, - tenantId, }) notifications.success("Logged in successfully") if ($auth?.user?.forceResetPassword) { @@ -66,7 +64,6 @@ Sign in with email - import { - ActionButton, Body, Button, Divider, Heading, Input, Layout, - notifications, } from "@budibase/bbui" - import { goto, params } from "@roxi/routify" - import { auth, organisation } from "stores/portal" - import GoogleButton from "./_components/GoogleButton.svelte" + import { goto } from "@roxi/routify" + import { auth } from "stores/portal" import Logo from "assets/bb-emblem.svg" - import { onMount } from "svelte" let tenantId = "" - let username = "" - let password = "" - $: company = $organisation.company || "Budibase" - - async function login() { - try { - await auth.login({ - username, - password, - tenantId, - }) - notifications.success("Logged in successfully") - if ($auth?.user?.forceResetPassword) { - $goto("./reset") - } else { - if ($params["?returnUrl"]) { - window.location = decodeURIComponent($params["?returnUrl"]) - } else { - notifications.success("Logged in successfully") - $goto("../portal") - } - } - } catch (err) { - console.error(err) - notifications.error("Invalid credentials") - } + async function setOrg() { + auth.setOrg(tenantId) + $goto("./login") } function handleKeydown(evt) { - if (evt.key === "Enter") login() + if (evt.key === "Enter") setOrg() } - - onMount(async () => { - await organisation.init() - }) @@ -59,17 +28,16 @@
- logo - Sign in to {company} + logo + Set Budibase organisation - - Sign in with email + Set organisation - +
diff --git a/packages/builder/src/stores/portal/admin.js b/packages/builder/src/stores/portal/admin.js index dab56a9225..1902e17a29 100644 --- a/packages/builder/src/stores/portal/admin.js +++ b/packages/builder/src/stores/portal/admin.js @@ -8,7 +8,9 @@ export function createAdminStore() { async function init() { try { const tenantId = get(auth).tenantId - const response = await api.get(`/api/admin/configs/checklist?tenantId=${tenantId}`) + const response = await api.get( + `/api/admin/configs/checklist?tenantId=${tenantId}` + ) const json = await response.json() const onboardingSteps = Object.keys(json) diff --git a/packages/builder/src/stores/portal/auth.js b/packages/builder/src/stores/portal/auth.js index 56762532da..608a0d91b4 100644 --- a/packages/builder/src/stores/portal/auth.js +++ b/packages/builder/src/stores/portal/auth.js @@ -2,57 +2,71 @@ import { derived, writable, get } from "svelte/store" import api from "../../builderStore/api" export function createAuthStore() { - const user = writable(null) - const store = derived(user, $user => { + const auth = writable({ + user: null, + tenantId: "default", + }) + const store = derived(auth, $store => { let initials = null let isAdmin = false let isBuilder = false - let tenantId = "default" - if ($user) { - if ($user.firstName) { - initials = $user.firstName[0] - if ($user.lastName) { - initials += $user.lastName[0] + if ($store.user) { + const user = $store.user + if (user.firstName) { + initials = user.firstName[0] + if (user.lastName) { + initials += user.lastName[0] } - } else if ($user.email) { - initials = $user.email[0] + } else if (user.email) { + initials = user.email[0] } else { initials = "Unknown" } - isAdmin = !!$user.admin?.global - isBuilder = !!$user.builder?.global - tenantId = $user.tenantId || tenantId + isAdmin = !!user.admin?.global + isBuilder = !!user.builder?.global } return { - user: $user, + user: $store.user, + tenantId: $store.tenantId, initials, isAdmin, isBuilder, - tenantId, } }) + function setUser(user) { + auth.update(store => { + store.user = user + return store + }) + } + return { subscribe: store.subscribe, + setOrg: tenantId => { + auth.update(store => { + store.tenantId = tenantId + return store + }) + }, checkAuth: async () => { const response = await api.get("/api/admin/users/self") if (response.status !== 200) { - user.set(null) + setUser(null) } else { const json = await response.json() - user.set(json) + setUser(json) } }, login: async creds => { - const tenantId = creds.tenantId || get(store).tenantId - delete creds.tenantId + const tenantId = get(store).tenantId const response = await api.post( `/api/admin/auth/${tenantId}/login`, creds ) const json = await response.json() if (response.status === 200) { - user.set(json.user) + setUser(json.user) } else { throw "Invalid credentials" } @@ -64,13 +78,13 @@ export function createAuthStore() { throw "Unable to create logout" } await response.json() - user.set(null) + setUser(null) }, updateSelf: async fields => { const newUser = { ...get(user), ...fields } const response = await api.post("/api/admin/users/self", newUser) if (response.status === 200) { - user.set(newUser) + setUser(newUser) } else { throw "Unable to update user details" } diff --git a/packages/worker/src/api/controllers/admin/configs.js b/packages/worker/src/api/controllers/admin/configs.js index 3135e6374c..9e1c2c3539 100644 --- a/packages/worker/src/api/controllers/admin/configs.js +++ b/packages/worker/src/api/controllers/admin/configs.js @@ -1,4 +1,3 @@ -const CouchDB = require("../../../db") const { generateConfigID, getConfigParams, diff --git a/packages/worker/src/api/controllers/admin/users.js b/packages/worker/src/api/controllers/admin/users.js index ba7cb97d46..a0e4e18e8d 100644 --- a/packages/worker/src/api/controllers/admin/users.js +++ b/packages/worker/src/api/controllers/admin/users.js @@ -5,7 +5,7 @@ const { getGlobalDBFromCtx, StaticDatabases, } = require("@budibase/auth/db") -const { hash, getGlobalUserByEmail, newid } = require("@budibase/auth").utils +const { hash, getGlobalUserByEmail } = require("@budibase/auth").utils const { UserStatus, EmailTemplatePurpose } = require("../../../constants") const { checkInviteCode } = require("../../../utilities/redis") const { sendEmail } = require("../../../utilities/email")