Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion assets/vue/router/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default {
{
name: "TermsConditionsView",
path: "terms-conditions/view",
meta: { requiresAdmin: true, showBreadcrumb: true, },
meta: { showBreadcrumb: true, },
component: () => import("../views/terms/Terms.vue"),
},
],
Expand Down
49 changes: 38 additions & 11 deletions assets/vue/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,13 @@ const router = createRouter({
],
})

let navWaitTimer
router.beforeEach(async (to, from, next) => {
document.body.classList.add("cursor-wait")
if (navWaitTimer) clearTimeout(navWaitTimer)
navWaitTimer = window.setTimeout(() => {
document.body.classList.remove("cursor-wait")
}, 4000)

const securityStore = useSecurityStore()

Expand Down Expand Up @@ -307,26 +312,48 @@ router.beforeEach(async (to, from, next) => {
}
}

if (to.matched.some((record) => record.meta.requiresAuth)) {
if (!securityStore.isLoading) {
try {
if (!securityStore.user && !securityStore.isLoading) {
await securityStore.checkSession()
}

if (securityStore.isAuthenticated) {
next()
} else {
const needsAuth = to.matched.some(r => r.meta?.requiresAuth)
if (needsAuth && !securityStore.isAuthenticated) {
sessionStorage.clear()
next({
path: "/login",
query: { redirect: to.fullPath },
})
return next({ path: "/login", query: { redirect: to.fullPath } })
}
} else {
next()

const wantsAdmin = to.matched.some(r => r.meta?.requiresAdmin === true)
const wantsSessionAdmin = to.matched.some(r => r.meta?.requiresSessionAdmin === true)

let allowed = true
if (wantsAdmin && wantsSessionAdmin) {
allowed = !!securityStore.isAdmin || !!securityStore.isSessionAdmin
} else if (wantsAdmin) {
allowed = !!securityStore.isAdmin
} else if (wantsSessionAdmin) {
allowed = !!securityStore.isSessionAdmin
}

if (!allowed) {
return next({ name: "Home", replace: true })
}

return next()
} catch (err) {
console.error("[RouterGuard] Unhandled error:", err)
return next({ name: "Home", replace: true })
}
})

router.afterEach(() => {
if (navWaitTimer) clearTimeout(navWaitTimer)
document.body.classList.remove("cursor-wait")
})

router.onError((err) => {
console.error("[Router] onError:", err)
if (navWaitTimer) clearTimeout(navWaitTimer)
document.body.classList.remove("cursor-wait")
})

Expand Down
Loading