Update log out handling to work better, and add support for navigating to a return URL
This commit is contained in:
parent
8be175843a
commit
f6396649b5
|
@ -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
|
* Fetches the currently logged in user object
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import * as API from "../api"
|
import * as API from "../api"
|
||||||
import { writable } from "svelte/store"
|
import { writable } from "svelte/store"
|
||||||
|
import { initialise } from "./initialise.js"
|
||||||
|
|
||||||
const createAuthStore = () => {
|
const createAuthStore = () => {
|
||||||
const store = writable(null)
|
const store = writable(null)
|
||||||
|
@ -11,8 +12,14 @@ const createAuthStore = () => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const logOut = async () => {
|
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.document.cookie = `budibase:auth=; budibase:currentapp=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;`
|
||||||
window.location = "/builder/auth/login"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
|
@ -18,8 +18,8 @@ const createRouteStore = () => {
|
||||||
const fetchRoutes = async () => {
|
const fetchRoutes = async () => {
|
||||||
const routeConfig = await API.fetchRoutes()
|
const routeConfig = await API.fetchRoutes()
|
||||||
let routes = []
|
let routes = []
|
||||||
Object.values(routeConfig.routes).forEach(route => {
|
Object.values(routeConfig.routes || {}).forEach(route => {
|
||||||
Object.entries(route.subpaths).forEach(([path, config]) => {
|
Object.entries(route.subpaths || {}).forEach(([path, config]) => {
|
||||||
routes.push({
|
routes.push({
|
||||||
path,
|
path,
|
||||||
screenId: config.screenId,
|
screenId: config.screenId,
|
||||||
|
@ -83,12 +83,23 @@ const createRouteStore = () => {
|
||||||
const setRouterLoaded = () => {
|
const setRouterLoaded = () => {
|
||||||
store.update(state => ({ ...state, routerLoaded: true }))
|
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 {
|
return {
|
||||||
subscribe: store.subscribe,
|
subscribe: store.subscribe,
|
||||||
actions: {
|
actions: {
|
||||||
fetchRoutes,
|
fetchRoutes,
|
||||||
navigate,
|
navigate,
|
||||||
|
createFullURL,
|
||||||
setRouteParams,
|
setRouteParams,
|
||||||
setQueryParams,
|
setQueryParams,
|
||||||
setActiveRoute,
|
setActiveRoute,
|
||||||
|
|
|
@ -112,8 +112,18 @@ const refreshDataProviderHandler = async (action, context) => {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const logoutHandler = async () => {
|
const logoutHandler = async action => {
|
||||||
await authStore.actions.logOut()
|
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) => {
|
const clearFormHandler = async (action, context) => {
|
||||||
|
|
Loading…
Reference in New Issue