From 88c0a635a09ae7b320ea8a8a59a50f9f359990b4 Mon Sep 17 00:00:00 2001 From: Michael Drury Date: Sun, 25 Jul 2021 12:07:25 +0100 Subject: [PATCH 1/5] Adding back logout functionality. --- packages/client/src/store/auth.js | 20 +++++++++++++++++++- packages/client/src/utils/buttonActions.js | 7 ++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/packages/client/src/store/auth.js b/packages/client/src/store/auth.js index 604da7f9ad..5feb161b97 100644 --- a/packages/client/src/store/auth.js +++ b/packages/client/src/store/auth.js @@ -1,18 +1,36 @@ import * as API from "../api" import { writable } from "svelte/store" +import { initialise } from "./initialise" const createAuthStore = () => { const store = writable(null) + const goToDefaultRoute = () => { + // Setting the active route forces an update of the active screen ID, + // even if we're on the same URL + routeStore.actions.setActiveRoute("/") + + // Navigating updates the URL to reflect this route + routeStore.actions.navigate("/") + } + + // Fetches the user object if someone is logged in and has reloaded the page const fetchUser = async () => { const user = await API.fetchSelf() store.set(user) } + const logOut = async () => { + store.set(null) + window.document.cookie = `budibase:auth=; budibase:currentapp=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;` + await initialise() + goToDefaultRoute() + } + return { subscribe: store.subscribe, - actions: { fetchUser }, + actions: { fetchUser, logOut }, } } diff --git a/packages/client/src/utils/buttonActions.js b/packages/client/src/utils/buttonActions.js index af4826a1c4..8c62f266e8 100644 --- a/packages/client/src/utils/buttonActions.js +++ b/packages/client/src/utils/buttonActions.js @@ -1,5 +1,5 @@ import { get } from "svelte/store" -import { routeStore, builderStore, confirmationStore } from "../store" +import { routeStore, builderStore, confirmationStore, authStore } from "../store" import { saveRow, deleteRow, executeQuery, triggerAutomation } from "../api" import { ActionTypes } from "../constants" @@ -77,6 +77,10 @@ const refreshDatasourceHandler = async (action, context) => { ) } +const logoutHandler = async () => { + await authStore.actions.logOut() +} + const handlerMap = { ["Save Row"]: saveRowHandler, ["Delete Row"]: deleteRowHandler, @@ -85,6 +89,7 @@ const handlerMap = { ["Trigger Automation"]: triggerAutomationHandler, ["Validate Form"]: validateFormHandler, ["Refresh Datasource"]: refreshDatasourceHandler, + ["Log Out"]: logoutHandler, } const confirmTextMap = { From f32b4f94e7dd2304a2ea92929f27a3a25091ee90 Mon Sep 17 00:00:00 2001 From: Michael Drury Date: Sun, 25 Jul 2021 12:09:50 +0100 Subject: [PATCH 2/5] Linting. --- packages/client/src/store/auth.js | 2 +- packages/client/src/utils/buttonActions.js | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/client/src/store/auth.js b/packages/client/src/store/auth.js index 5feb161b97..86e4500282 100644 --- a/packages/client/src/store/auth.js +++ b/packages/client/src/store/auth.js @@ -1,6 +1,7 @@ import * as API from "../api" import { writable } from "svelte/store" import { initialise } from "./initialise" +import { routeStore } from "./routes" const createAuthStore = () => { const store = writable(null) @@ -14,7 +15,6 @@ const createAuthStore = () => { routeStore.actions.navigate("/") } - // Fetches the user object if someone is logged in and has reloaded the page const fetchUser = async () => { const user = await API.fetchSelf() diff --git a/packages/client/src/utils/buttonActions.js b/packages/client/src/utils/buttonActions.js index 8c62f266e8..98ba9dc214 100644 --- a/packages/client/src/utils/buttonActions.js +++ b/packages/client/src/utils/buttonActions.js @@ -1,5 +1,10 @@ import { get } from "svelte/store" -import { routeStore, builderStore, confirmationStore, authStore } from "../store" +import { + routeStore, + builderStore, + confirmationStore, + authStore, +} from "../store" import { saveRow, deleteRow, executeQuery, triggerAutomation } from "../api" import { ActionTypes } from "../constants" From e74d9a73127f03a8879ea5ebb36929ca36b73163 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Sun, 25 Jul 2021 13:29:27 +0100 Subject: [PATCH 3/5] Reload location on client logout for full reset, avoiding error when public pages exist --- packages/client/src/store/auth.js | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/packages/client/src/store/auth.js b/packages/client/src/store/auth.js index 86e4500282..beaeb6007c 100644 --- a/packages/client/src/store/auth.js +++ b/packages/client/src/store/auth.js @@ -1,20 +1,9 @@ import * as API from "../api" import { writable } from "svelte/store" -import { initialise } from "./initialise" -import { routeStore } from "./routes" const createAuthStore = () => { const store = writable(null) - const goToDefaultRoute = () => { - // Setting the active route forces an update of the active screen ID, - // even if we're on the same URL - routeStore.actions.setActiveRoute("/") - - // Navigating updates the URL to reflect this route - routeStore.actions.navigate("/") - } - // Fetches the user object if someone is logged in and has reloaded the page const fetchUser = async () => { const user = await API.fetchSelf() @@ -22,10 +11,8 @@ const createAuthStore = () => { } const logOut = async () => { - store.set(null) window.document.cookie = `budibase:auth=; budibase:currentapp=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;` - await initialise() - goToDefaultRoute() + window.location.reload() } return { From 95d05c8a8e4f02cb2f2fa562176cfc0c46f31696 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Sun, 25 Jul 2021 13:43:07 +0100 Subject: [PATCH 4/5] Fix error with refreshing app package so that page reload on logout can be prevented --- packages/client/src/store/app.js | 11 +++++++++-- packages/client/src/store/auth.js | 15 ++++++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/packages/client/src/store/app.js b/packages/client/src/store/app.js index eb5a259a25..0cabaec4ab 100644 --- a/packages/client/src/store/app.js +++ b/packages/client/src/store/app.js @@ -6,8 +6,15 @@ const createAppStore = () => { // Fetches the app definition including screens, layouts and theme const fetchAppDefinition = async () => { - const appDefinition = await API.fetchAppPackage(get(store).appId) - store.set(appDefinition) + const appId = get(store)?.appId + if (!appId) { + throw "Cannot fetch app definition without app ID set" + } + const appDefinition = await API.fetchAppPackage(appId) + store.set({ + ...appDefinition, + appId: appDefinition?.application?.appId, + }) } // Sets the initial app ID diff --git a/packages/client/src/store/auth.js b/packages/client/src/store/auth.js index beaeb6007c..86e4500282 100644 --- a/packages/client/src/store/auth.js +++ b/packages/client/src/store/auth.js @@ -1,9 +1,20 @@ import * as API from "../api" import { writable } from "svelte/store" +import { initialise } from "./initialise" +import { routeStore } from "./routes" const createAuthStore = () => { const store = writable(null) + const goToDefaultRoute = () => { + // Setting the active route forces an update of the active screen ID, + // even if we're on the same URL + routeStore.actions.setActiveRoute("/") + + // Navigating updates the URL to reflect this route + routeStore.actions.navigate("/") + } + // Fetches the user object if someone is logged in and has reloaded the page const fetchUser = async () => { const user = await API.fetchSelf() @@ -11,8 +22,10 @@ const createAuthStore = () => { } const logOut = async () => { + store.set(null) window.document.cookie = `budibase:auth=; budibase:currentapp=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;` - window.location.reload() + await initialise() + goToDefaultRoute() } return { From c9eda114a8df08bd359637bdd7bc19e98e4aa5bc Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Sun, 25 Jul 2021 13:47:28 +0100 Subject: [PATCH 5/5] Redirect to login screen on client logout to prevent viewing public pages if they exist --- packages/client/src/store/auth.js | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/packages/client/src/store/auth.js b/packages/client/src/store/auth.js index 86e4500282..1fa4ae17b0 100644 --- a/packages/client/src/store/auth.js +++ b/packages/client/src/store/auth.js @@ -1,20 +1,9 @@ import * as API from "../api" import { writable } from "svelte/store" -import { initialise } from "./initialise" -import { routeStore } from "./routes" const createAuthStore = () => { const store = writable(null) - const goToDefaultRoute = () => { - // Setting the active route forces an update of the active screen ID, - // even if we're on the same URL - routeStore.actions.setActiveRoute("/") - - // Navigating updates the URL to reflect this route - routeStore.actions.navigate("/") - } - // Fetches the user object if someone is logged in and has reloaded the page const fetchUser = async () => { const user = await API.fetchSelf() @@ -22,10 +11,8 @@ const createAuthStore = () => { } const logOut = async () => { - store.set(null) window.document.cookie = `budibase:auth=; budibase:currentapp=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;` - await initialise() - goToDefaultRoute() + window.location = "/builder/auth/login" } return {