1
0
Fork 0
mirror of synced 2024-09-19 18:59:06 +12:00
budibase/packages/client/src/store/auth.js

33 lines
848 B
JavaScript
Raw Normal View History

import * as API from "../api"
import { writable, get } from "svelte/store"
import { builderStore } from "./builder"
import { TableNames } from "../constants"
2020-11-12 01:25:50 +13:00
const createAuthStore = () => {
const store = writable(null)
2020-11-12 01:25:50 +13:00
// Fetches the user object if someone is logged in and has reloaded the page
const fetchUser = async () => {
// Fetch the first user if inside the builder
if (get(builderStore).inBuilder) {
const users = await API.fetchTableData(TableNames.USERS)
if (!users.error && users[0] != null) {
store.set(users[0])
}
}
// Or fetch the current user from localstorage in a real app
else {
const user = await API.fetchSelf()
store.set(user)
}
}
return {
subscribe: store.subscribe,
actions: { fetchUser },
2020-11-12 01:25:50 +13:00
}
}
2020-11-12 01:25:50 +13:00
export const authStore = createAuthStore()