Merge pull request #4105 from Budibase/fix/log-out-action

Fix log out action
This commit is contained in:
Andrew Kingston 2022-01-19 14:54:20 +00:00 committed by GitHub
commit 6fcf0de0e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 72 additions and 7 deletions

View File

@ -1,13 +1,38 @@
<script>
import { Body } from "@budibase/bbui"
import { Label, Body, Layout } from "@budibase/bbui"
import DrawerBindableInput from "components/common/bindings/DrawerBindableInput.svelte"
export let parameters
export let bindings = []
</script>
<div class="root">
<Body size="S">This action doesn't require any additional settings.</Body>
<Layout noPadding gap="M">
<Body size="S">
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.
</Body>
<div class="content">
<Label small>Redirect URL</Label>
<DrawerBindableInput
title="Return URL"
value={parameters.redirectUrl}
on:change={value => (parameters.redirectUrl = value.detail)}
{bindings}
/>
</div>
</Layout>
</div>
<style>
.root {
max-width: 400px;
margin: 0 auto;
}
.content {
display: grid;
align-items: center;
gap: var(--spacing-m);
grid-template-columns: auto 1fr;
}
</style>

View File

@ -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
*/

View File

@ -11,8 +11,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 {

View File

@ -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,

View File

@ -112,8 +112,20 @@ const refreshDataProviderHandler = async (action, context) => {
)
}
const logoutHandler = async () => {
const logoutHandler = async action => {
await authStore.actions.logOut()
let redirectUrl = "/builder/auth/login"
let internal = false
if (action.parameters.redirectUrl) {
internal = action.parameters.redirectUrl?.startsWith("/")
redirectUrl = routeStore.actions.createFullURL(
action.parameters.redirectUrl
)
}
window.location.href = redirectUrl
if (internal) {
window.location.reload()
}
}
const clearFormHandler = async (action, context) => {

View File

@ -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." }
}