2020-11-19 18:55:40 +01:00
|
|
|
import * as API from "../api"
|
2020-11-25 10:50:51 +01:00
|
|
|
import { getAppId } from "../utils/getAppId"
|
2020-11-17 13:08:24 +01:00
|
|
|
import { writable } from "svelte/store"
|
2020-11-11 13:25:50 +01:00
|
|
|
|
2020-11-18 20:18:18 +01:00
|
|
|
const createAuthStore = () => {
|
2020-11-17 13:08:24 +01:00
|
|
|
const store = writable("")
|
2020-11-11 13:25:50 +01:00
|
|
|
|
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) {
|
2020-11-12 13:24:45 +01:00
|
|
|
store.set(user.token)
|
2020-11-19 19:39:22 +01:00
|
|
|
location.reload()
|
2020-11-11 13:25:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
const logOut = () => {
|
2020-11-17 13:08:24 +01:00
|
|
|
store.set("")
|
2020-11-13 16:42:32 +01:00
|
|
|
const appId = getAppId()
|
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-11-19 19:39:22 +01:00
|
|
|
location.reload()
|
2020-11-11 13:25:50 +01:00
|
|
|
}
|
|
|
|
|
2020-11-17 13:08:24 +01:00
|
|
|
return {
|
|
|
|
subscribe: store.subscribe,
|
|
|
|
actions: { logIn, 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()
|