Vue · Middle · Question 32 of 75
How do you protect routes that require authentication?
router.beforeEach((to) => {
if (to.meta.requiresAuth && !auth.isLoggedIn) {
return { name: "login", query: { redirect: to.fullPath } };
}
});- ✓Correct answer: A global
beforeEachguard that checksto.metaand returns a redirect location - 2Hide the links in the menu - that is enough
- 3Check auth in every component's
onMounted
Explanation
Returning false cancels navigation, returning a route redirects, returning nothing allows it. Real protection must still happen on the server API.