first commit
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
import { tokenService } from '@/services/api/token-service'
|
||||
import { useAuthStore } from '@/store/auth'
|
||||
|
||||
const APP_TITLE = 'بنو'
|
||||
|
||||
const isPublic = (to) => to.meta?.public === true
|
||||
|
||||
export const authGuard = (to) => {
|
||||
if (isPublic(to)) return
|
||||
if (to.meta?.requiresGuest) return
|
||||
if (!tokenService.hasToken()) {
|
||||
return { name: 'login', query: { redirect: to.fullPath } }
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
export const guestGuard = (to) => {
|
||||
if (!to.meta?.requiresGuest) return
|
||||
if (tokenService.hasToken()) {
|
||||
const auth = useAuthStore()
|
||||
return auth.role === 'admin' ? { name: 'admin-dashboard' } : { name: 'student-dashboard' }
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
export const roleGuard = (to) => {
|
||||
if (!to.meta?.role) return
|
||||
const auth = useAuthStore()
|
||||
if (auth.role !== to.meta.role) {
|
||||
return { name: 'home' }
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
export const titleGuard = (to) => {
|
||||
const title = to.meta?.title
|
||||
document.title = title ? `${APP_TITLE} - ${title}` : APP_TITLE
|
||||
}
|
||||
|
||||
export const guards = [authGuard, guestGuard, roleGuard]
|
||||
Reference in New Issue
Block a user