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

23 lines
539 B
JavaScript
Raw Normal View History

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
api.setState(USER_STATE_PATH, user)
localStorage.setItem("budibase:user", JSON.stringify(user))
}