Merge branch 'fix/per-app-login' of github.com:Budibase/budibase into fix/per-app-login
This commit is contained in:
commit
5e412fa497
|
@ -1,5 +1,5 @@
|
||||||
<script>
|
<script>
|
||||||
import { goto } from "@roxi/routify"
|
import { goto, params } from "@roxi/routify"
|
||||||
import {
|
import {
|
||||||
notifications,
|
notifications,
|
||||||
Input,
|
Input,
|
||||||
|
@ -22,8 +22,12 @@
|
||||||
username,
|
username,
|
||||||
password,
|
password,
|
||||||
})
|
})
|
||||||
notifications.success("Logged in successfully")
|
if ($params["?returnUrl"]) {
|
||||||
$goto("../portal")
|
window.location = decodeURIComponent($params["?returnUrl"])
|
||||||
|
} else {
|
||||||
|
notifications.success("Logged in successfully")
|
||||||
|
$goto("../portal")
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err)
|
console.error(err)
|
||||||
notifications.error("Invalid credentials")
|
notifications.error("Invalid credentials")
|
||||||
|
|
|
@ -28,7 +28,8 @@
|
||||||
!$isActive("./auth") &&
|
!$isActive("./auth") &&
|
||||||
!$isActive("./invite")
|
!$isActive("./invite")
|
||||||
) {
|
) {
|
||||||
$redirect("./auth/login")
|
const returnUrl = encodeURIComponent(window.location.pathname)
|
||||||
|
$redirect("./auth/login?", { returnUrl })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
<script>
|
||||||
|
import { auth } from "stores/portal"
|
||||||
|
import { onMount } from "svelte"
|
||||||
|
import { redirect } from "@roxi/routify"
|
||||||
|
|
||||||
|
// If already authenticated, redirect away from the auth section.
|
||||||
|
// Check this onMount rather than a reactive statement to avoid trumping
|
||||||
|
// the login return URL functionality.
|
||||||
|
onMount(() => {
|
||||||
|
if ($auth.user) {
|
||||||
|
$redirect("../")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
</script>
|
||||||
|
|
||||||
|
{#if !$auth.user}
|
||||||
|
<slot />
|
||||||
|
{/if}
|
|
@ -37,11 +37,13 @@
|
||||||
|
|
||||||
onMount(async () => {
|
onMount(async () => {
|
||||||
// Prevent non-builders from accessing the portal
|
// Prevent non-builders from accessing the portal
|
||||||
if (!$auth.user?.builder?.global) {
|
if ($auth.user) {
|
||||||
$redirect("../")
|
if (!$auth.user?.builder?.global) {
|
||||||
} else {
|
$redirect("../")
|
||||||
await organisation.init()
|
} else {
|
||||||
loaded = true
|
await organisation.init()
|
||||||
|
loaded = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
|
@ -10,6 +10,8 @@
|
||||||
initialise,
|
initialise,
|
||||||
screenStore,
|
screenStore,
|
||||||
authStore,
|
authStore,
|
||||||
|
routeStore,
|
||||||
|
builderStore,
|
||||||
} from "../store"
|
} from "../store"
|
||||||
import { TableNames, ActionTypes } from "../constants"
|
import { TableNames, ActionTypes } from "../constants"
|
||||||
|
|
||||||
|
@ -18,13 +20,13 @@
|
||||||
setContext("component", writable({}))
|
setContext("component", writable({}))
|
||||||
setContext("context", createContextStore())
|
setContext("context", createContextStore())
|
||||||
|
|
||||||
let loaded = false
|
let dataLoaded = false
|
||||||
|
|
||||||
// Load app config
|
// Load app config
|
||||||
onMount(async () => {
|
onMount(async () => {
|
||||||
await initialise()
|
await initialise()
|
||||||
await authStore.actions.fetchUser()
|
await authStore.actions.fetchUser()
|
||||||
loaded = true
|
dataLoaded = true
|
||||||
})
|
})
|
||||||
|
|
||||||
// Register this as a refreshable datasource so that user changes cause
|
// Register this as a refreshable datasource so that user changes cause
|
||||||
|
@ -36,9 +38,22 @@
|
||||||
metadata: { dataSource: { type: "table", tableId: TableNames.USERS } },
|
metadata: { dataSource: { type: "table", tableId: TableNames.USERS } },
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
|
// Redirect to home layout if no matching route
|
||||||
|
$: {
|
||||||
|
if (dataLoaded && $routeStore.routerLoaded && !$routeStore.activeRoute) {
|
||||||
|
if ($authStore) {
|
||||||
|
routeStore.actions.navigate("/")
|
||||||
|
} else {
|
||||||
|
const returnUrl = `${window.location.pathname}${window.location.hash}`
|
||||||
|
const encodedUrl = encodeURIComponent(returnUrl)
|
||||||
|
window.location = `/builder/auth/login?returnUrl=${encodedUrl}`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
{#if loaded && $screenStore.activeLayout}
|
{#if dataLoaded && $screenStore.activeLayout}
|
||||||
<div lang="en" dir="ltr" class="spectrum spectrum--medium spectrum--light">
|
<div lang="en" dir="ltr" class="spectrum spectrum--medium spectrum--light">
|
||||||
<Provider key="user" data={$authStore} {actions}>
|
<Provider key="user" data={$authStore} {actions}>
|
||||||
<Component definition={$screenStore.activeLayout.props} />
|
<Component definition={$screenStore.activeLayout.props} />
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
import Router from "svelte-spa-router"
|
import Router from "svelte-spa-router"
|
||||||
import { routeStore } from "../store"
|
import { routeStore } from "../store"
|
||||||
import Screen from "./Screen.svelte"
|
import Screen from "./Screen.svelte"
|
||||||
|
import { onMount } from "svelte"
|
||||||
|
|
||||||
const { styleable } = getContext("sdk")
|
const { styleable } = getContext("sdk")
|
||||||
const component = getContext("component")
|
const component = getContext("component")
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
<script>
|
<script>
|
||||||
import { fade } from "svelte/transition"
|
|
||||||
import { screenStore, routeStore } from "../store"
|
import { screenStore, routeStore } from "../store"
|
||||||
import Component from "./Component.svelte"
|
import Component from "./Component.svelte"
|
||||||
import Provider from "./Provider.svelte"
|
import Provider from "./Provider.svelte"
|
||||||
|
import { onMount } from "svelte"
|
||||||
|
|
||||||
// Keep route params up to date
|
// Keep route params up to date
|
||||||
export let params = {}
|
export let params = {}
|
||||||
|
@ -11,8 +11,12 @@
|
||||||
// Get the screen definition for the current route
|
// Get the screen definition for the current route
|
||||||
$: screenDefinition = $screenStore.activeScreen?.props
|
$: screenDefinition = $screenStore.activeScreen?.props
|
||||||
|
|
||||||
// Redirect to home layout if no matching route
|
// Mark the router as loaded whenever the screen mounts
|
||||||
$: screenDefinition == null && routeStore.actions.navigate("/")
|
onMount(() => {
|
||||||
|
if (!$routeStore.routerLoaded) {
|
||||||
|
routeStore.actions.setRouterLoaded()
|
||||||
|
}
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<!-- Ensure to fully remount when screen changes -->
|
<!-- Ensure to fully remount when screen changes -->
|
||||||
|
|
|
@ -8,6 +8,7 @@ const createRouteStore = () => {
|
||||||
routeParams: {},
|
routeParams: {},
|
||||||
activeRoute: null,
|
activeRoute: null,
|
||||||
routeSessionId: Math.random(),
|
routeSessionId: Math.random(),
|
||||||
|
routerLoaded: false,
|
||||||
}
|
}
|
||||||
const store = writable(initialState)
|
const store = writable(initialState)
|
||||||
|
|
||||||
|
@ -47,10 +48,19 @@ const createRouteStore = () => {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
const navigate = push
|
const navigate = push
|
||||||
|
const setRouterLoaded = () => {
|
||||||
|
store.update(state => ({ ...state, routerLoaded: true }))
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
subscribe: store.subscribe,
|
subscribe: store.subscribe,
|
||||||
actions: { fetchRoutes, navigate, setRouteParams, setActiveRoute },
|
actions: {
|
||||||
|
fetchRoutes,
|
||||||
|
navigate,
|
||||||
|
setRouteParams,
|
||||||
|
setActiveRoute,
|
||||||
|
setRouterLoaded,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,12 +18,11 @@ const createScreenStore = () => {
|
||||||
activeLayout = $builderStore.layout
|
activeLayout = $builderStore.layout
|
||||||
activeScreen = $builderStore.screen
|
activeScreen = $builderStore.screen
|
||||||
} else {
|
} else {
|
||||||
// Otherwise find the correct screen by matching the current route
|
activeLayout = { props: { _component: "screenslot" } }
|
||||||
|
|
||||||
|
// Find the correct screen by matching the current route
|
||||||
const { screens, layouts } = $config
|
const { screens, layouts } = $config
|
||||||
activeLayout = layouts[0]
|
if ($routeStore.activeRoute) {
|
||||||
if (screens.length === 1) {
|
|
||||||
activeScreen = screens[0]
|
|
||||||
} else if ($routeStore.activeRoute) {
|
|
||||||
activeScreen = screens.find(
|
activeScreen = screens.find(
|
||||||
screen => screen._id === $routeStore.activeRoute.screenId
|
screen => screen._id === $routeStore.activeRoute.screenId
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue