budibase/packages/client/src/api/authenticate.js

29 lines
614 B
JavaScript
Raw Normal View History

2020-08-06 22:12:35 +02:00
import appStore from "../state/store"
export const USER_STATE_PATH = "_bbuser"
2019-09-26 06:40:58 +02:00
export const authenticate = api => async ({ username, password }) => {
if (!username) {
api.error("Authenticate: username not set")
return
}
2019-09-26 06:40:58 +02:00
if (!password) {
api.error("Authenticate: password not set")
return
}
2019-09-26 06:40:58 +02:00
const user = await api.post({
url: "/api/authenticate",
body: { username, password },
})
2019-09-26 06:40:58 +02:00
// set user even if error - so it is defined at least
2020-08-06 22:12:35 +02:00
appStore.update(s => {
s[USER_STATE_PATH] = user
return s
})
localStorage.setItem("budibase:user", JSON.stringify(user))
}