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

54 lines
1.2 KiB
JavaScript
Raw Normal View History

2022-09-20 19:59:52 +00:00
import store from '~/store'
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) => {
if (!store.getters['auth/check'] &&
store.getters['auth/token'] !== null &&
store.getters['auth/token'] !== undefined
) {
try {
2023-10-27 09:58:01 +00:00
store.dispatch('auth/fetchUser').then((user) => {
initCrisp(user)
initSentry(user)
})
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()
}