From 5b3b1d82d4a6677bb1a825ea70eb18324021efc5 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Wed, 19 Jan 2022 11:22:04 +0000 Subject: [PATCH 1/6] Add return URL setting to log out button action --- .../ButtonActionEditor/actions/LogOut.svelte | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/packages/builder/src/components/design/PropertiesPanel/PropertyControls/ButtonActionEditor/actions/LogOut.svelte b/packages/builder/src/components/design/PropertiesPanel/PropertyControls/ButtonActionEditor/actions/LogOut.svelte index 3434d63480..e36946ee0a 100644 --- a/packages/builder/src/components/design/PropertiesPanel/PropertyControls/ButtonActionEditor/actions/LogOut.svelte +++ b/packages/builder/src/components/design/PropertiesPanel/PropertyControls/ButtonActionEditor/actions/LogOut.svelte @@ -1,13 +1,38 @@
- This action doesn't require any additional settings. + + + Please enter what URL you would like to be redirected to after logging + out. If you dont' enter a value, you'll be redirected to the login screen. + +
+ + (parameters.returnUrl = value.detail)} + {bindings} + /> +
+
From 8b976bed52ca4e8e6ca8ae72f6713f6456e8b9f0 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Wed, 19 Jan 2022 11:22:27 +0000 Subject: [PATCH 2/6] Update log out handling to work better, and add support for navigating to a return URL --- packages/client/src/api/auth.js | 9 +++++++++ packages/client/src/stores/auth.js | 9 ++++++++- packages/client/src/stores/routes.js | 15 +++++++++++++-- packages/client/src/utils/buttonActions.js | 12 +++++++++++- 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/packages/client/src/api/auth.js b/packages/client/src/api/auth.js index 68ca5dbc80..9ac09f5571 100644 --- a/packages/client/src/api/auth.js +++ b/packages/client/src/api/auth.js @@ -18,6 +18,15 @@ export const logIn = async ({ email, password }) => { }) } +/** + * Logs the user out and invaidates their session. + */ +export const logOut = async () => { + return await API.post({ + url: "/api/global/auth/logout", + }) +} + /** * Fetches the currently logged in user object */ diff --git a/packages/client/src/stores/auth.js b/packages/client/src/stores/auth.js index 1fa4ae17b0..f50f7a52c1 100644 --- a/packages/client/src/stores/auth.js +++ b/packages/client/src/stores/auth.js @@ -1,5 +1,6 @@ import * as API from "../api" import { writable } from "svelte/store" +import { initialise } from "./initialise.js" const createAuthStore = () => { const store = writable(null) @@ -11,8 +12,14 @@ const createAuthStore = () => { } const logOut = async () => { + try { + await API.logOut() + } catch (error) { + // Do nothing + } + + // Manually destroy cookie to be sure window.document.cookie = `budibase:auth=; budibase:currentapp=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;` - window.location = "/builder/auth/login" } return { diff --git a/packages/client/src/stores/routes.js b/packages/client/src/stores/routes.js index 1d5dca1645..d50677493b 100644 --- a/packages/client/src/stores/routes.js +++ b/packages/client/src/stores/routes.js @@ -18,8 +18,8 @@ const createRouteStore = () => { const fetchRoutes = async () => { const routeConfig = await API.fetchRoutes() let routes = [] - Object.values(routeConfig.routes).forEach(route => { - Object.entries(route.subpaths).forEach(([path, config]) => { + Object.values(routeConfig.routes || {}).forEach(route => { + Object.entries(route.subpaths || {}).forEach(([path, config]) => { routes.push({ path, screenId: config.screenId, @@ -83,12 +83,23 @@ const createRouteStore = () => { const setRouterLoaded = () => { store.update(state => ({ ...state, routerLoaded: true })) } + const createFullURL = relativeURL => { + if (!relativeURL?.startsWith("/")) { + return relativeURL + } + if (!window.location.href.includes("#")) { + return `${window.location.href}#${relativeURL}` + } + const base = window.location.href.split("#")[0] + return `${base}#${relativeURL}` + } return { subscribe: store.subscribe, actions: { fetchRoutes, navigate, + createFullURL, setRouteParams, setQueryParams, setActiveRoute, diff --git a/packages/client/src/utils/buttonActions.js b/packages/client/src/utils/buttonActions.js index 6b4dd4235a..1ab3b26976 100644 --- a/packages/client/src/utils/buttonActions.js +++ b/packages/client/src/utils/buttonActions.js @@ -112,8 +112,18 @@ const refreshDataProviderHandler = async (action, context) => { ) } -const logoutHandler = async () => { +const logoutHandler = async action => { await authStore.actions.logOut() + let returnUrl = "/builder/auth/login" + let internal = false + if (action.parameters.returnUrl) { + internal = action.parameters.returnUrl?.startsWith("/") + returnUrl = routeStore.actions.createFullURL(action.parameters.returnUrl) + } + window.location.href = returnUrl + if (internal) { + window.location.reload() + } } const clearFormHandler = async (action, context) => { From 4e7d5e7d65133e8e25ddb6f6a1ac82dac180c206 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Wed, 19 Jan 2022 11:22:44 +0000 Subject: [PATCH 3/6] Fix server crash when trying to log out and already logged out --- packages/worker/src/api/controllers/global/auth.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/worker/src/api/controllers/global/auth.js b/packages/worker/src/api/controllers/global/auth.js index 2ba12194ca..44ee99aee7 100644 --- a/packages/worker/src/api/controllers/global/auth.js +++ b/packages/worker/src/api/controllers/global/auth.js @@ -141,7 +141,9 @@ exports.resetUpdate = async ctx => { } exports.logout = async ctx => { - await platformLogout({ ctx, userId: ctx.user._id }) + if (ctx.user && ctx.user._id) { + await platformLogout({ ctx, userId: ctx.user._id }) + } ctx.body = { message: "User logged out." } } From ca35a5b768da06b7d87bb5c433a3be13bc6c9354 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Wed, 19 Jan 2022 11:32:56 +0000 Subject: [PATCH 4/6] Lint --- packages/client/src/stores/auth.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/client/src/stores/auth.js b/packages/client/src/stores/auth.js index f50f7a52c1..9cd2613e24 100644 --- a/packages/client/src/stores/auth.js +++ b/packages/client/src/stores/auth.js @@ -1,6 +1,5 @@ import * as API from "../api" import { writable } from "svelte/store" -import { initialise } from "./initialise.js" const createAuthStore = () => { const store = writable(null) From d6bbe73db0faf81d248f4be2a442e9d3294d6978 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Wed, 19 Jan 2022 12:45:15 +0000 Subject: [PATCH 5/6] Update log out action text --- .../PropertyControls/ButtonActionEditor/actions/LogOut.svelte | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/builder/src/components/design/PropertiesPanel/PropertyControls/ButtonActionEditor/actions/LogOut.svelte b/packages/builder/src/components/design/PropertiesPanel/PropertyControls/ButtonActionEditor/actions/LogOut.svelte index e36946ee0a..37dac8ebc0 100644 --- a/packages/builder/src/components/design/PropertiesPanel/PropertyControls/ButtonActionEditor/actions/LogOut.svelte +++ b/packages/builder/src/components/design/PropertiesPanel/PropertyControls/ButtonActionEditor/actions/LogOut.svelte @@ -9,8 +9,8 @@
- Please enter what URL you would like to be redirected to after logging - out. If you dont' enter a value, you'll be redirected to the login screen. + Please enter the URL you would like to be redirected to after logging out. + If you don't enter a value, you'll be redirected to the login screen.
From e0a3911851fee45b13378ae6c42a3d939bdbb82f Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Wed, 19 Jan 2022 12:50:07 +0000 Subject: [PATCH 6/6] Rename return URL to redirect URL in log out action --- .../ButtonActionEditor/actions/LogOut.svelte | 6 +++--- packages/client/src/utils/buttonActions.js | 12 +++++++----- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/packages/builder/src/components/design/PropertiesPanel/PropertyControls/ButtonActionEditor/actions/LogOut.svelte b/packages/builder/src/components/design/PropertiesPanel/PropertyControls/ButtonActionEditor/actions/LogOut.svelte index 37dac8ebc0..f0606d86b3 100644 --- a/packages/builder/src/components/design/PropertiesPanel/PropertyControls/ButtonActionEditor/actions/LogOut.svelte +++ b/packages/builder/src/components/design/PropertiesPanel/PropertyControls/ButtonActionEditor/actions/LogOut.svelte @@ -13,11 +13,11 @@ If you don't enter a value, you'll be redirected to the login screen.
- + (parameters.returnUrl = value.detail)} + value={parameters.redirectUrl} + on:change={value => (parameters.redirectUrl = value.detail)} {bindings} />
diff --git a/packages/client/src/utils/buttonActions.js b/packages/client/src/utils/buttonActions.js index 1ab3b26976..2ef324d23c 100644 --- a/packages/client/src/utils/buttonActions.js +++ b/packages/client/src/utils/buttonActions.js @@ -114,13 +114,15 @@ const refreshDataProviderHandler = async (action, context) => { const logoutHandler = async action => { await authStore.actions.logOut() - let returnUrl = "/builder/auth/login" + let redirectUrl = "/builder/auth/login" let internal = false - if (action.parameters.returnUrl) { - internal = action.parameters.returnUrl?.startsWith("/") - returnUrl = routeStore.actions.createFullURL(action.parameters.returnUrl) + if (action.parameters.redirectUrl) { + internal = action.parameters.redirectUrl?.startsWith("/") + redirectUrl = routeStore.actions.createFullURL( + action.parameters.redirectUrl + ) } - window.location.href = returnUrl + window.location.href = redirectUrl if (internal) { window.location.reload() }