2022-07-21 21:20:10 +00:00
|
|
|
import type { NavigationGuardNext, RouteLocationNamedRaw, RouteLocationNormalized } from 'vue-router'
|
2022-07-04 22:52:53 +00:00
|
|
|
import type { Permission } from '~/store/auth'
|
|
|
|
|
2022-07-04 18:17:58 +00:00
|
|
|
import store from '~/store'
|
|
|
|
|
|
|
|
export const hasPermissions = (permission: Permission) => (to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) => {
|
|
|
|
if (store.state.auth.authenticated && store.state.auth.availablePermissions[permission]) {
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
|
2023-09-05 15:01:12 +00:00
|
|
|
console.log('Not authenticated. Redirecting to library.')
|
2022-07-04 18:17:58 +00:00
|
|
|
next({ name: 'library.index' })
|
2022-07-21 01:21:36 +00:00
|
|
|
}
|
2022-07-21 21:20:10 +00:00
|
|
|
|
2022-07-30 16:52:01 +00:00
|
|
|
export const requireLoggedIn = (fallbackLocation?: RouteLocationNamedRaw) => (to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) => {
|
2022-07-21 21:20:10 +00:00
|
|
|
if (store.state.auth.authenticated) return next()
|
2023-09-05 15:01:12 +00:00
|
|
|
console.log('!', to)
|
2022-11-28 18:40:12 +00:00
|
|
|
return next(fallbackLocation ?? { name: 'login', query: { next: to.fullPath } })
|
2022-07-21 21:20:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const requireLoggedOut = (fallbackLocation: RouteLocationNamedRaw) => (to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) => {
|
|
|
|
if (!store.state.auth.authenticated) return next()
|
|
|
|
return next(fallbackLocation)
|
|
|
|
}
|