funkwhale/front/src/router/guards.ts

28 wiersze
1.2 KiB
TypeScript
Czysty Zwykły widok Historia

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'
2023-09-01 12:09:58 +00:00
import useLogger from '~/composables/useLogger'
2022-07-04 18:17:58 +00:00
import store from '~/store'
2023-09-01 12:09:58 +00:00
const logger = useLogger()
2022-07-04 18:17:58 +00:00
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-01 12:09:58 +00:00
logger.warn('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
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-01 12:09:58 +00:00
logger.debug('!', to)
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)
}