Adding organisation page.

This commit is contained in:
mike12345567 2021-07-16 18:24:32 +01:00
parent 037dce5016
commit c248844a24
9 changed files with 54 additions and 74 deletions

View File

@ -218,7 +218,6 @@ exports.getAllApps = async ({ tenantId, dev, all } = {}) => {
}) })
if (!all) { if (!all) {
return apps.filter(app => { return apps.filter(app => {
if (dev) { if (dev) {
return isDevApp(app) return isDevApp(app)
} }

View File

@ -29,7 +29,7 @@
!$isActive("./invite") !$isActive("./invite")
) { ) {
const returnUrl = encodeURIComponent(window.location.pathname) const returnUrl = encodeURIComponent(window.location.pathname)
$redirect("./auth/login?", { returnUrl }) $redirect("./auth?", { returnUrl })
} else if ($auth?.user?.forceResetPassword) { } else if ($auth?.user?.forceResetPassword) {
$redirect("./auth/reset") $redirect("./auth/reset")
} }

View File

@ -1,4 +1,5 @@
<script> <script>
import { redirect } from "@roxi/routify" import { redirect } from "@roxi/routify"
$redirect("./login") // TODO: need to check if the tenant is already set
$redirect("./org")
</script> </script>

View File

@ -15,7 +15,6 @@
import Logo from "assets/bb-emblem.svg" import Logo from "assets/bb-emblem.svg"
import { onMount } from "svelte" import { onMount } from "svelte"
let tenantId = ""
let username = "" let username = ""
let password = "" let password = ""
@ -26,7 +25,6 @@
await auth.login({ await auth.login({
username, username,
password, password,
tenantId,
}) })
notifications.success("Logged in successfully") notifications.success("Logged in successfully")
if ($auth?.user?.forceResetPassword) { if ($auth?.user?.forceResetPassword) {
@ -66,7 +64,6 @@
<Divider noGrid /> <Divider noGrid />
<Layout gap="XS" noPadding> <Layout gap="XS" noPadding>
<Body size="S" textAlign="center">Sign in with email</Body> <Body size="S" textAlign="center">Sign in with email</Body>
<Input label="Organisation" bind:value={tenantId} />
<Input label="Email" bind:value={username} /> <Input label="Email" bind:value={username} />
<Input <Input
label="Password" label="Password"

View File

@ -1,57 +1,26 @@
<script> <script>
import { import {
ActionButton,
Body, Body,
Button, Button,
Divider, Divider,
Heading, Heading,
Input, Input,
Layout, Layout,
notifications,
} from "@budibase/bbui" } from "@budibase/bbui"
import { goto, params } from "@roxi/routify" import { goto } from "@roxi/routify"
import { auth, organisation } from "stores/portal" import { auth } from "stores/portal"
import GoogleButton from "./_components/GoogleButton.svelte"
import Logo from "assets/bb-emblem.svg" import Logo from "assets/bb-emblem.svg"
import { onMount } from "svelte"
let tenantId = "" let tenantId = ""
let username = ""
let password = ""
$: company = $organisation.company || "Budibase" async function setOrg() {
auth.setOrg(tenantId)
async function login() { $goto("./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")
}
} }
function handleKeydown(evt) { function handleKeydown(evt) {
if (evt.key === "Enter") login() if (evt.key === "Enter") setOrg()
} }
onMount(async () => {
await organisation.init()
})
</script> </script>
<svelte:window on:keydown={handleKeydown} /> <svelte:window on:keydown={handleKeydown} />
@ -59,17 +28,16 @@
<div class="main"> <div class="main">
<Layout> <Layout>
<Layout noPadding justifyItems="center"> <Layout noPadding justifyItems="center">
<img alt="logo" src={$organisation.logoUrl || Logo} /> <img alt="logo" src={Logo} />
<Heading>Sign in to {company}</Heading> <Heading>Set Budibase organisation</Heading>
</Layout> </Layout>
<GoogleButton />
<Divider noGrid /> <Divider noGrid />
<Layout gap="XS" noPadding> <Layout gap="XS" noPadding>
<Body size="S" textAlign="center">Sign in with email</Body> <Body size="S" textAlign="center">Set organisation</Body>
<Input label="Organisation" bind:value={tenantId} /> <Input label="Organisation" bind:value={tenantId} />
</Layout> </Layout>
<Layout gap="XS" noPadding> <Layout gap="XS" noPadding>
<Button cta on:click={login}>Sign in to {company}</Button> <Button cta on:click={setOrg}>Set organisation</Button>
</Layout> </Layout>
</Layout> </Layout>
</div> </div>

View File

@ -8,7 +8,9 @@ export function createAdminStore() {
async function init() { async function init() {
try { try {
const tenantId = get(auth).tenantId 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 json = await response.json()
const onboardingSteps = Object.keys(json) const onboardingSteps = Object.keys(json)

View File

@ -2,57 +2,71 @@ import { derived, writable, get } from "svelte/store"
import api from "../../builderStore/api" import api from "../../builderStore/api"
export function createAuthStore() { export function createAuthStore() {
const user = writable(null) const auth = writable({
const store = derived(user, $user => { user: null,
tenantId: "default",
})
const store = derived(auth, $store => {
let initials = null let initials = null
let isAdmin = false let isAdmin = false
let isBuilder = false let isBuilder = false
let tenantId = "default" if ($store.user) {
if ($user) { const user = $store.user
if ($user.firstName) { if (user.firstName) {
initials = $user.firstName[0] initials = user.firstName[0]
if ($user.lastName) { if (user.lastName) {
initials += $user.lastName[0] initials += user.lastName[0]
} }
} else if ($user.email) { } else if (user.email) {
initials = $user.email[0] initials = user.email[0]
} else { } else {
initials = "Unknown" initials = "Unknown"
} }
isAdmin = !!$user.admin?.global isAdmin = !!user.admin?.global
isBuilder = !!$user.builder?.global isBuilder = !!user.builder?.global
tenantId = $user.tenantId || tenantId
} }
return { return {
user: $user, user: $store.user,
tenantId: $store.tenantId,
initials, initials,
isAdmin, isAdmin,
isBuilder, isBuilder,
tenantId,
} }
}) })
function setUser(user) {
auth.update(store => {
store.user = user
return store
})
}
return { return {
subscribe: store.subscribe, subscribe: store.subscribe,
setOrg: tenantId => {
auth.update(store => {
store.tenantId = tenantId
return store
})
},
checkAuth: async () => { checkAuth: async () => {
const response = await api.get("/api/admin/users/self") const response = await api.get("/api/admin/users/self")
if (response.status !== 200) { if (response.status !== 200) {
user.set(null) setUser(null)
} else { } else {
const json = await response.json() const json = await response.json()
user.set(json) setUser(json)
} }
}, },
login: async creds => { login: async creds => {
const tenantId = creds.tenantId || get(store).tenantId const tenantId = get(store).tenantId
delete creds.tenantId
const response = await api.post( const response = await api.post(
`/api/admin/auth/${tenantId}/login`, `/api/admin/auth/${tenantId}/login`,
creds creds
) )
const json = await response.json() const json = await response.json()
if (response.status === 200) { if (response.status === 200) {
user.set(json.user) setUser(json.user)
} else { } else {
throw "Invalid credentials" throw "Invalid credentials"
} }
@ -64,13 +78,13 @@ export function createAuthStore() {
throw "Unable to create logout" throw "Unable to create logout"
} }
await response.json() await response.json()
user.set(null) setUser(null)
}, },
updateSelf: async fields => { updateSelf: async fields => {
const newUser = { ...get(user), ...fields } const newUser = { ...get(user), ...fields }
const response = await api.post("/api/admin/users/self", newUser) const response = await api.post("/api/admin/users/self", newUser)
if (response.status === 200) { if (response.status === 200) {
user.set(newUser) setUser(newUser)
} else { } else {
throw "Unable to update user details" throw "Unable to update user details"
} }

View File

@ -1,4 +1,3 @@
const CouchDB = require("../../../db")
const { const {
generateConfigID, generateConfigID,
getConfigParams, getConfigParams,

View File

@ -5,7 +5,7 @@ const {
getGlobalDBFromCtx, getGlobalDBFromCtx,
StaticDatabases, StaticDatabases,
} = require("@budibase/auth/db") } = 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 { UserStatus, EmailTemplatePurpose } = require("../../../constants")
const { checkInviteCode } = require("../../../utilities/redis") const { checkInviteCode } = require("../../../utilities/redis")
const { sendEmail } = require("../../../utilities/email") const { sendEmail } = require("../../../utilities/email")