budibase/packages/component-sdk/src/store/auth.js

38 lines
691 B
JavaScript
Raw Normal View History

2020-11-11 13:25:50 +01:00
import { localStorageStore } from "../../../builder/src/builderStore/store/localStorage"
import * as api from "../api"
const initialState = {
user: null,
}
export const createAuthStore = () => {
const store = localStorageStore("bb-app-auth", initialState)
/**
* Logs a user in.
*/
const logIn = async ({ username, password }) => {
const user = await api.logIn({ username, password })
if (!user.error) {
store.update(state => {
state.user = user
return state
})
}
}
/**
* Logs a user out.
*/
const logOut = () => {
store.update(() => initialState)
}
store.actions = {
logIn,
logOut,
}
return store
}