opnform/resources/js/middleware/check-auth.js

57 lines
1.4 KiB
JavaScript
Raw Normal View History

2023-12-19 17:57:31 +00:00
import { useAuthStore } from '../stores/auth'
import { useWorkspacesStore } from '../stores/workspaces'
2023-10-08 12:35:15 +00:00
import * as Sentry from '@sentry/vue'
2022-09-20 19:59:52 +00:00
export function initCrisp (user) {
return new Promise((resolve, reject) => {
const intervalId = window.setInterval(function () {
2023-10-13 10:29:34 +00:00
if (!user) {
resolve()
return
}
2022-09-20 19:59:52 +00:00
if (window.$crisp) {
window.$crisp.push(['set', 'user:email', user.email])
window.$crisp.push(['set', 'user:nickname', user.name])
window.$crisp.push(['set', 'session:data', [[
2022-12-22 10:54:01 +00:00
['pro-subscription', user?.is_subscribed ?? false],
2022-09-20 19:59:52 +00:00
['id', user.id]
]]])
window.clearInterval(intervalId)
resolve()
}
2022-12-13 10:00:43 +00:00
}, 50000)
2022-09-20 19:59:52 +00:00
})
}
2023-10-08 12:35:15 +00:00
export function initSentry (user) {
2023-10-13 10:29:34 +00:00
if (!window.config.sentry_dsn || !user) {
2023-10-08 12:35:15 +00:00
return
}
Sentry.configureScope((scope) => {
scope.setUser({
id: user.id,
email: user.email,
subscription: user?.is_subscribed
})
})
}
2022-09-20 19:59:52 +00:00
export default async (to, from, next) => {
const authStore = useAuthStore()
if (!authStore.check &&
authStore.token !== null &&
authStore.token !== undefined
2022-09-20 19:59:52 +00:00
) {
try {
authStore.fetchUser().then((user) => {
2023-10-27 09:58:01 +00:00
initCrisp(user)
initSentry(user)
2023-12-19 17:57:31 +00:00
useWorkspacesStore().fetchWorkspaces()
2023-10-27 09:58:01 +00:00
})
2022-09-20 19:59:52 +00:00
} catch (e) {
2023-10-27 09:58:01 +00:00
console.error(e)
2022-09-20 19:59:52 +00:00
}
}
next()
}