budibase/packages/builder/src/stores/portal/auth.js

162 lines
4.0 KiB
JavaScript
Raw Normal View History

import { derived, writable, get } from "svelte/store"
import { API } from "api"
import { admin } from "stores/portal"
import analytics from "analytics"
2021-04-11 12:35:55 +02:00
export function createAuthStore() {
const auth = writable({
user: null,
2022-09-23 12:40:19 +02:00
accountPortalAccess: false,
tenantId: "default",
tenantSet: false,
loaded: false,
2022-01-13 15:07:49 +01:00
postLogout: false,
})
const store = derived(auth, $store => {
return {
user: $store.user,
2022-09-23 12:40:19 +02:00
accountPortalAccess: $store.accountPortalAccess,
tenantId: $store.tenantId,
tenantSet: $store.tenantSet,
loaded: $store.loaded,
2022-01-13 15:07:49 +01:00
postLogout: $store.postLogout,
isSSO: !!$store.user?.provider,
}
})
2021-04-11 12:35:55 +02:00
function setUser(user) {
auth.update(store => {
store.loaded = true
store.user = user
2022-09-23 12:40:19 +02:00
store.accountPortalAccess = user?.accountPortalAccess
if (user) {
store.tenantId = user.tenantId || "default"
store.tenantSet = true
}
return store
})
if (user) {
analytics
.activate()
.then(() => {
analytics.identify(user._id)
})
.catch(() => {
// This request may fail due to browser extensions blocking requests
// containing the word analytics, so we don't want to spam users with
// an error here.
})
}
}
async function setOrganisation(tenantId) {
const prevId = get(store).tenantId
auth.update(store => {
store.tenantId = tenantId
store.tenantSet = !!tenantId
return store
})
if (prevId !== tenantId) {
// re-init admin after setting org
await admin.init()
}
}
async function setInitInfo(info) {
await API.setInitInfo(info)
auth.update(store => {
2021-11-10 12:12:33 +01:00
store.initInfo = info
return store
})
2021-11-10 12:12:33 +01:00
return info
}
function setPostLogout() {
2022-01-13 15:07:49 +01:00
auth.update(store => {
store.postLogout = true
return store
})
}
async function getInitInfo() {
const info = await API.getInitInfo()
auth.update(store => {
store.initInfo = info
return store
})
return info
}
const actions = {
checkQueryString: async () => {
const urlParams = new URLSearchParams(window.location.search)
if (urlParams.has("tenantId")) {
const tenantId = urlParams.get("tenantId")
await setOrganisation(tenantId)
}
},
setOrg: async tenantId => {
await setOrganisation(tenantId)
},
getSelf: async () => {
// We need to catch this locally as we never want this to fail, even
// though normally we never want to swallow API errors at the store level.
// We're either logged in or we aren't.
// We also need to always update the loaded flag.
try {
const user = await API.fetchBuilderSelf()
setUser(user)
} catch (error) {
setUser(null)
}
},
2021-05-04 12:32:22 +02:00
login: async creds => {
const tenantId = get(store).tenantId
2024-11-27 12:02:16 +01:00
await API.logIn(tenantId, creds.username, creds.password)
await actions.getSelf()
2021-04-11 12:35:55 +02:00
},
logout: async () => {
await API.logOut()
2022-11-11 12:26:36 +01:00
setPostLogout()
setUser(null)
await setInitInfo({})
2021-04-12 11:47:48 +02:00
},
updateSelf: async fields => {
await API.updateSelf({ ...fields })
// Refetch to enrich after update.
try {
const user = await API.fetchBuilderSelf()
setUser(user)
} catch (error) {
setUser(null)
}
},
forgotPassword: async email => {
const tenantId = get(store).tenantId
2024-11-27 12:02:16 +01:00
await API.requestForgotPassword(tenantId, email)
},
resetPassword: async (password, resetCode) => {
const tenantId = get(store).tenantId
2024-11-27 12:02:16 +01:00
await API.resetPassword(tenantId, password, resetCode)
},
2022-02-09 21:30:52 +01:00
generateAPIKey: async () => {
2022-02-28 19:39:05 +01:00
return API.generateAPIKey()
2022-02-09 21:30:52 +01:00
},
fetchAPIKey: async () => {
const info = await API.fetchDeveloperInfo()
2022-02-28 19:39:05 +01:00
return info?.apiKey
2022-02-09 21:30:52 +01:00
},
2021-04-11 12:35:55 +02:00
}
return {
subscribe: store.subscribe,
setOrganisation,
getInitInfo,
setInitInfo,
...actions,
}
2021-04-11 12:35:55 +02:00
}
export const auth = createAuthStore()