2021-05-22 02:23:39 +12:00
|
|
|
import { derived, writable, get } from "svelte/store"
|
2022-01-25 03:32:27 +13:00
|
|
|
import { API } from "api"
|
2021-08-05 20:59:08 +12:00
|
|
|
import { admin } from "stores/portal"
|
2021-09-22 07:21:15 +12:00
|
|
|
import analytics from "analytics"
|
2021-04-11 22:35:55 +12:00
|
|
|
|
|
|
|
export function createAuthStore() {
|
2021-08-05 20:59:08 +12:00
|
|
|
const auth = writable({
|
|
|
|
user: null,
|
|
|
|
tenantId: "default",
|
|
|
|
tenantSet: false,
|
|
|
|
loaded: false,
|
2022-01-14 03:07:49 +13:00
|
|
|
postLogout: false,
|
2021-08-05 20:59:08 +12:00
|
|
|
})
|
|
|
|
const store = derived(auth, $store => {
|
2021-05-22 00:17:09 +12:00
|
|
|
let initials = null
|
2021-05-22 04:12:25 +12:00
|
|
|
let isAdmin = false
|
|
|
|
let isBuilder = false
|
2021-08-05 20:59:08 +12:00
|
|
|
if ($store.user) {
|
|
|
|
const user = $store.user
|
|
|
|
if (user.firstName) {
|
|
|
|
initials = user.firstName[0]
|
|
|
|
if (user.lastName) {
|
|
|
|
initials += user.lastName[0]
|
2021-05-22 00:17:09 +12:00
|
|
|
}
|
2021-08-05 20:59:08 +12:00
|
|
|
} else if (user.email) {
|
|
|
|
initials = user.email[0]
|
2021-05-22 04:12:25 +12:00
|
|
|
} else {
|
|
|
|
initials = "Unknown"
|
2021-05-22 00:17:09 +12:00
|
|
|
}
|
2021-08-05 20:59:08 +12:00
|
|
|
isAdmin = !!user.admin?.global
|
|
|
|
isBuilder = !!user.builder?.global
|
2021-05-22 00:17:09 +12:00
|
|
|
}
|
|
|
|
return {
|
2021-08-05 20:59:08 +12:00
|
|
|
user: $store.user,
|
|
|
|
tenantId: $store.tenantId,
|
|
|
|
tenantSet: $store.tenantSet,
|
|
|
|
loaded: $store.loaded,
|
2022-01-14 03:07:49 +13:00
|
|
|
postLogout: $store.postLogout,
|
2021-05-22 00:17:09 +12:00
|
|
|
initials,
|
2021-05-22 04:12:25 +12:00
|
|
|
isAdmin,
|
|
|
|
isBuilder,
|
2021-05-22 00:17:09 +12:00
|
|
|
}
|
|
|
|
})
|
2021-04-11 22:35:55 +12:00
|
|
|
|
2021-08-05 20:59:08 +12:00
|
|
|
function setUser(user) {
|
|
|
|
auth.update(store => {
|
|
|
|
store.loaded = true
|
|
|
|
store.user = user
|
|
|
|
if (user) {
|
|
|
|
store.tenantId = user.tenantId || "default"
|
|
|
|
store.tenantSet = true
|
|
|
|
}
|
|
|
|
return store
|
|
|
|
})
|
2021-09-22 07:21:15 +12:00
|
|
|
|
|
|
|
if (user) {
|
|
|
|
analytics.activate().then(() => {
|
|
|
|
analytics.identify(user._id, user)
|
2021-09-29 05:18:56 +13:00
|
|
|
analytics.showChat({
|
|
|
|
email: user.email,
|
2021-10-08 00:03:08 +13:00
|
|
|
created_at: (user.createdAt || Date.now()) / 1000,
|
2021-11-11 07:40:55 +13:00
|
|
|
name: user.account?.name,
|
2021-09-29 05:18:56 +13:00
|
|
|
user_id: user._id,
|
|
|
|
tenant: user.tenantId,
|
2021-11-11 07:40:55 +13:00
|
|
|
"Company size": user.account?.size,
|
|
|
|
"Job role": user.account?.profession,
|
2021-09-29 05:18:56 +13:00
|
|
|
})
|
2021-09-22 07:21:15 +12:00
|
|
|
})
|
|
|
|
}
|
2021-08-05 20:59:08 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-10 05:40:31 +13:00
|
|
|
async function setInitInfo(info) {
|
2022-01-25 03:32:27 +13:00
|
|
|
await API.setInitInfo(info)
|
2021-11-10 06:00:32 +13:00
|
|
|
auth.update(store => {
|
2021-11-11 00:12:33 +13:00
|
|
|
store.initInfo = info
|
2021-11-10 06:00:32 +13:00
|
|
|
return store
|
|
|
|
})
|
2021-11-11 00:12:33 +13:00
|
|
|
return info
|
2021-11-10 06:00:32 +13:00
|
|
|
}
|
|
|
|
|
2022-01-14 03:07:49 +13:00
|
|
|
async function setPostLogout() {
|
|
|
|
auth.update(store => {
|
|
|
|
store.postLogout = true
|
|
|
|
return store
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-10 06:00:32 +13:00
|
|
|
async function getInitInfo() {
|
2022-01-25 03:32:27 +13:00
|
|
|
const info = await API.getInitInfo()
|
2021-11-10 06:00:32 +13:00
|
|
|
auth.update(store => {
|
2022-01-25 03:32:27 +13:00
|
|
|
store.initInfo = info
|
2021-11-10 06:00:32 +13:00
|
|
|
return store
|
|
|
|
})
|
2022-01-25 03:32:27 +13:00
|
|
|
return info
|
2021-11-10 05:40:31 +13:00
|
|
|
}
|
|
|
|
|
2021-04-11 22:35:55 +12:00
|
|
|
return {
|
2021-05-07 01:49:11 +12:00
|
|
|
subscribe: store.subscribe,
|
2021-11-11 00:12:33 +13:00
|
|
|
setOrganisation,
|
2021-11-10 06:00:32 +13:00
|
|
|
getInitInfo,
|
2021-11-10 05:40:31 +13:00
|
|
|
setInitInfo,
|
2021-08-05 20:59:08 +12:00
|
|
|
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)
|
|
|
|
},
|
2021-05-07 01:49:11 +12:00
|
|
|
checkAuth: async () => {
|
2022-01-25 03:32:27 +13:00
|
|
|
const user = await API.fetchBuilderSelf()
|
|
|
|
setUser(user)
|
2021-05-07 01:49:11 +12:00
|
|
|
},
|
2021-05-04 22:32:22 +12:00
|
|
|
login: async creds => {
|
2021-08-05 20:59:08 +12:00
|
|
|
const tenantId = get(store).tenantId
|
2022-01-25 03:32:27 +13:00
|
|
|
const response = await API.logIn({
|
|
|
|
username: creds.username,
|
|
|
|
password: creds.password,
|
|
|
|
tenantId,
|
|
|
|
})
|
|
|
|
setUser(response.user)
|
2021-04-11 22:35:55 +12:00
|
|
|
},
|
|
|
|
logout: async () => {
|
2022-01-25 03:32:27 +13:00
|
|
|
await API.logOut()
|
2021-11-10 05:40:31 +13:00
|
|
|
await setInitInfo({})
|
2021-08-05 20:59:08 +12:00
|
|
|
setUser(null)
|
2022-01-14 03:07:49 +13:00
|
|
|
setPostLogout()
|
2021-04-12 21:47:48 +12:00
|
|
|
},
|
2021-05-22 02:23:39 +12:00
|
|
|
updateSelf: async fields => {
|
2021-08-05 20:59:08 +12:00
|
|
|
const newUser = { ...get(auth).user, ...fields }
|
2022-01-25 03:32:27 +13:00
|
|
|
await API.updateSelf(newUser)
|
|
|
|
setUser(newUser)
|
2021-05-20 01:05:08 +12:00
|
|
|
},
|
2021-05-19 02:26:09 +12:00
|
|
|
forgotPassword: async email => {
|
2021-08-05 20:59:08 +12:00
|
|
|
const tenantId = get(store).tenantId
|
2022-01-25 03:32:27 +13:00
|
|
|
await API.requestForgotPassword({
|
|
|
|
tenantId,
|
2021-05-19 02:26:09 +12:00
|
|
|
email,
|
|
|
|
})
|
|
|
|
},
|
2022-01-25 03:32:27 +13:00
|
|
|
resetPassword: async (password, resetCode) => {
|
2021-08-05 20:59:08 +12:00
|
|
|
const tenantId = get(store).tenantId
|
2022-01-25 03:32:27 +13:00
|
|
|
await API.resetPassword({
|
|
|
|
tenantId,
|
|
|
|
password,
|
|
|
|
resetCode,
|
|
|
|
})
|
2021-04-21 04:17:44 +12:00
|
|
|
},
|
2021-04-11 22:35:55 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const auth = createAuthStore()
|