I am currently working on a web project using Vue, and I have noticed that the following code snippet works when executed in the console:
document.getElementById('app').__vue__.$router.push("some_route")
Despite having meta tags defined in the beforeEach navigation guard (requireAuth), certain routes can still be accessed by unauthorized users. In my Flask API, I return a token and a role for validation purposes. While entering the URL directly in the address bar triggers authentication, accessing the same route through the console bypasses this verification process.
router.beforeEach(async (to, from, next) => {
if(to.matched.some(record => record.meta.requiresAuth)){
let user = JSON.parse(localStorage.getItem('user')) // User is a JSON with a JWT token
if (user && user.token){
await user_service.isLogged(to.path).then(response => {
// Wait for a response from the server to validate the token
switch (response.status){
case 200:
next()
break;
case 401:
next({path: '/login'})
break;
case 403:
next({path: '/403'})
break;
}
})
} else {
next({path: '/login'})
}
}
next()
})
Your feedback and suggestions are highly appreciated. Thank you!