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 = {