계정에 따른 권한별 lnb메뉴가 다르더라도 URL path를 직접 입력하면 접근이 불가능(안보이는 메뉴)하던 라우터에 진입이 가능하다.
이때, 라우터가드를 사용하여 URL path 접근을 막아줘야 한다.
//src/router/index.js
import {createRouter} from 'vue-router'
import routes from './routers'
const router = creaetRouter({
mode : 'history',
history : createWebHistory(),
routes,
})
router.beforeEach((to, from, next) => {
// to : 이동할 url,
// from : 현재 url,
// next : 훅의 변동을 호출할 때 사용
next(false)
)}
//src/router/routes.js
const routes = [
{
path : '/views/SignUp'
name : 'SignUp',
component : 'SignUp',
},
{
path : '/receiving/manage'
name : 'ReceivingManage',
component : 'ReceivingManage',
meta : {authRequired : true}
},
]
export default routes
//src/router/index.js
router.beforeEach((to, from, next) => {
// to : 이동할 url,
// from : 현재 url,
// next : 훅의 변동을 호출할 때 사용
const authRequiredList = routers.filter(router=> router.meta.authRequired)
if(authRequiredList) alert('로그인이 필요합니다')
else next()
)}
//src/router/routes.js
const routes = [
{
path : '/views/SignUp'
name : 'SignUp',
component : 'SignUp',
},
{
path : '/receiving/manage'
name : 'ReceivingManage',
component : 'ReceivingManage',
meta : {authRequired : true, allows : ['first']}
},
{
path : '/receiving/manage2'
name : 'ReceivingManage2',
component : 'ReceivingManage2',
meta : {authRequired : true, allows : ['first', 'second']}
},
]
export default routes
//src/router/index.js
router.beforeEach((to, from, next) => {
// to : 이동할 url,
// from : 현재 url,
// next : 훅의 변동을 호출할 때 사용
const authRequiredList = routers.filter(router=> router.meta.authRequired)
if(authRequiredList) {
alert('로그인이 필요합니다')
to.matched.
}
else next()
)}