2020-11-19 18:55:40 +01:00
|
|
|
import * as API from "../api"
|
2021-01-07 15:53:56 +01:00
|
|
|
import { writable, get } from "svelte/store"
|
2020-12-11 15:24:19 +01:00
|
|
|
import { initialise } from "./initialise"
|
|
|
|
import { routeStore } from "./routes"
|
2021-01-07 15:53:56 +01:00
|
|
|
import { builderStore } from "./builder"
|
2021-01-28 15:29:35 +01:00
|
|
|
import { TableNames } from "../constants"
|
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
|
|
|
|
2020-12-11 15:24:19 +01: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("/")
|
|
|
|
}
|
2021-01-28 15:29:35 +01:00
|
|
|
|
|
|
|
// Logs a user in
|
2020-12-04 13:22:45 +01:00
|
|
|
const logIn = async ({ email, password }) => {
|
|
|
|
const user = await API.logIn({ email, password })
|
2020-11-11 13:25:50 +01:00
|
|
|
if (!user.error) {
|
2021-01-28 15:29:35 +01:00
|
|
|
store.set(user)
|
2020-12-11 15:24:19 +01:00
|
|
|
await initialise()
|
|
|
|
goToDefaultRoute()
|
2020-11-11 13:25:50 +01:00
|
|
|
}
|
|
|
|
}
|
2021-01-28 15:29:35 +01:00
|
|
|
|
|
|
|
// Logs a user out
|
2020-12-11 15:24:19 +01:00
|
|
|
const logOut = async () => {
|
2021-01-28 15:29:35 +01:00
|
|
|
store.set(null)
|
2021-01-07 15:53:56 +01:00
|
|
|
const appId = get(builderStore).appId
|
2020-11-12 13:24:45 +01:00
|
|
|
if (appId) {
|
|
|
|
for (let environment of ["local", "cloud"]) {
|
|
|
|
window.document.cookie = `budibase:${appId}:${environment}=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;`
|
|
|
|
}
|
|
|
|
}
|
2020-12-11 15:24:19 +01:00
|
|
|
await initialise()
|
|
|
|
goToDefaultRoute()
|
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 () => {
|
|
|
|
// 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 {
|
|
|
|
if (get(store) == null) {
|
|
|
|
const user = await API.fetchSelf()
|
|
|
|
if (user) {
|
|
|
|
store.set(user)
|
|
|
|
} else {
|
|
|
|
await logOut()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
await logOut()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-17 13:08:24 +01:00
|
|
|
return {
|
|
|
|
subscribe: store.subscribe,
|
2021-01-28 15:29:35 +01:00
|
|
|
actions: { logIn, logOut, fetchUser },
|
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()
|