budibase/packages/client/src/store/auth.js

33 lines
790 B
JavaScript
Raw Normal View History

import * as API from "../api"
import { getAppId } from "../utils/getAppId"
import { writable } from "svelte/store"
2020-11-11 13:25:50 +01:00
const createAuthStore = () => {
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) {
store.set(user.token)
location.reload()
2020-11-11 13:25:50 +01:00
}
}
const logOut = () => {
store.set("")
const appId = getAppId()
if (appId) {
for (let environment of ["local", "cloud"]) {
window.document.cookie = `budibase:${appId}:${environment}=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;`
}
}
location.reload()
2020-11-11 13:25:50 +01:00
}
return {
subscribe: store.subscribe,
actions: { logIn, logOut },
2020-11-11 13:25:50 +01:00
}
}
2020-11-11 13:25:50 +01:00
export const authStore = createAuthStore()