funkwhale/front/src/router/guards.ts

25 wiersze
1.1 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'
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()
}
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
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()
console.log('!', 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)
}