1
0
Fork 0
mirror of synced 2024-09-20 03:08:18 +12:00
budibase/packages/client/src/store/auth.js

38 lines
1 KiB
JavaScript
Raw Normal View History

import * as API from "../api"
import { writable } from "svelte/store"
import { initialise } from "./initialise"
import { routeStore } from "./routes"
2020-11-12 01:25:50 +13:00
const createAuthStore = () => {
const store = writable(null)
2020-11-12 01:25:50 +13:00
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)
}
2021-07-25 23:07:25 +12:00
const logOut = async () => {
store.set(null)
2021-07-25 23:07:25 +12:00
window.document.cookie = `budibase:auth=; budibase:currentapp=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;`
await initialise()
goToDefaultRoute()
2021-07-25 23:07:25 +12:00
}
return {
subscribe: store.subscribe,
2021-07-25 23:07:25 +12:00
actions: { fetchUser, logOut },
2020-11-12 01:25:50 +13:00
}
}
2020-11-12 01:25:50 +13:00
export const authStore = createAuthStore()