2020-11-19 18:55:40 +01:00
|
|
|
import * as API from "../api"
|
2021-06-29 18:41:02 +02:00
|
|
|
import { writable } from "svelte/store"
|
2020-11-11 13:25:50 +01:00
|
|
|
|
2020-11-18 20:18:18 +01:00
|
|
|
const createAuthStore = () => {
|
2021-01-28 15:29:35 +01:00
|
|
|
const store = writable(null)
|
2020-11-11 13:25:50 +01:00
|
|
|
|
2021-01-28 15:29:35 +01:00
|
|
|
// Fetches the user object if someone is logged in and has reloaded the page
|
|
|
|
const fetchUser = async () => {
|
2021-06-29 18:41:02 +02:00
|
|
|
const user = await API.fetchSelf()
|
|
|
|
store.set(user)
|
2021-01-28 15:29:35 +01:00
|
|
|
}
|
|
|
|
|
2021-07-25 13:07:25 +02:00
|
|
|
const logOut = async () => {
|
|
|
|
window.document.cookie = `budibase:auth=; budibase:currentapp=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;`
|
2021-07-25 14:47:28 +02:00
|
|
|
window.location = "/builder/auth/login"
|
2021-07-25 13:07:25 +02:00
|
|
|
}
|
|
|
|
|
2020-11-17 13:08:24 +01:00
|
|
|
return {
|
|
|
|
subscribe: store.subscribe,
|
2021-07-25 13:07:25 +02:00
|
|
|
actions: { fetchUser, logOut },
|
2020-11-11 13:25:50 +01:00
|
|
|
}
|
2020-11-17 13:08:24 +01:00
|
|
|
}
|
2020-11-11 13:25:50 +01:00
|
|
|
|
2020-11-18 20:18:18 +01:00
|
|
|
export const authStore = createAuthStore()
|