Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | 1x 1x 1x 1x 1x 4x 4x 68x 68x 68x 68x 68x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import {createRouter, createWebHistory, type RouteRecordRaw} from 'vue-router'
import {routes as baseRoutes } from './config'
import {useAuth} from '@/composables/useAuth'
import {useSchemaLogger} from '@/libs/logger/logger'
import {useLoadingStore} from '@/stores/loading'
function normalizeRoutes(routes: RouteRecordRaw[]): RouteRecordRaw[] {
routes.forEach((route) => {
// meta補完(そのままミューテート)
route.meta ??= {}
if (route.meta.requiredAuth === undefined) {
route.meta.requiredAuth = true
}
// 子も再帰的に
if (route.children?.length) {
normalizeRoutes(route.children)
}
})
return routes
}
const routes = normalizeRoutes(baseRoutes)
const router = createRouter({
history: createWebHistory(),
linkActiveClass: 'active',
routes,
})
const logger = useSchemaLogger({ module: 'router' })
router.beforeEach(async (to, from, next) => {
const s = useLoadingStore()
;(router as any).__loadingToken = s.start('route')
logger.debug('beforeEach', {
from: from,
to: to,
})
// const isLoggedIn = !!auth.currentUser
const { isAuthenticated, refreshIdToken } = useAuth()
console.log(`is logged in : ${isAuthenticated}`)
if (to.meta.requiresAuth && !isAuthenticated) {
logger.warn('blocked (unauthorized.)', { from: from,to: to, })
next({ name: 'Forbidden' })
} else if (to.meta.requiresAuth && !await refreshIdToken(false)) {
logger.warn('blocked (can not refresh idToken.)', { from: from,to: to, })
next({ name: 'Home' })
}
if (to.meta.guestOnly && isAuthenticated) {
logger.warn('blocked guest Only (authorized)', { from: from,to: to, })
next({ name: 'Dashboard' })
}
next()
})
router.afterEach(async (to, from, failure) => {
if (failure) {
logger.error('afterEach (failed)', {
from: from,
to: to,
failure,
})
} else {
logger.debug('afterEach (success)', {
from: from,
to: to,
})
}
const s = useLoadingStore()
const token: symbol | undefined = (router as any).__loadingToken
if (token) s.stop(token)
;(router as any).__loadingToken = undefined
})
// エラーハンドリング
router.onError((err, to) => {
logger.error('router error', {
to: to,
error: err,
})
})
export default router
|