2020-11-11 13:25:50 +01:00
|
|
|
import { localStorageStore } from "../../../builder/src/builderStore/store/localStorage"
|
|
|
|
import * as api from "../api"
|
2020-11-12 13:24:45 +01:00
|
|
|
import { getAppId } from "../utils"
|
2020-11-11 13:25:50 +01:00
|
|
|
|
2020-11-12 13:24:45 +01:00
|
|
|
const initialState = ""
|
2020-11-11 13:25:50 +01:00
|
|
|
|
|
|
|
export const createAuthStore = () => {
|
2020-11-12 13:24:45 +01:00
|
|
|
const store = localStorageStore("budibase:token", initialState)
|
2020-11-11 13:25:50 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Logs a user in.
|
|
|
|
*/
|
|
|
|
const logIn = async ({ username, password }) => {
|
|
|
|
const user = await api.logIn({ username, password })
|
|
|
|
if (!user.error) {
|
2020-11-12 13:24:45 +01:00
|
|
|
store.set(user.token)
|
2020-11-11 13:25:50 +01:00
|
|
|
}
|
2020-11-12 13:24:45 +01:00
|
|
|
return !user.error
|
2020-11-11 13:25:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Logs a user out.
|
|
|
|
*/
|
|
|
|
const logOut = () => {
|
2020-11-12 13:24:45 +01:00
|
|
|
store.set(initialState)
|
|
|
|
|
|
|
|
// Expire any cookies
|
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-11 13:25:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
store.actions = {
|
|
|
|
logIn,
|
|
|
|
logOut,
|
|
|
|
}
|
|
|
|
|
|
|
|
return store
|
|
|
|
}
|