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

49 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 () {
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) {
if (!window.config.sentry_dsn) {
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 {
const user = await store.dispatch('auth/fetchUser')
initCrisp(user)
2023-10-08 12:35:15 +00:00
initSentry(user)
2022-09-20 19:59:52 +00:00
} catch (e) {
console.log(e, 'error')
}
}
next()
}