I have implemented a router guard to protect certain pages in my app. The goal is for users to be prompted to log in when attempting to access these protected pages. Currently, the guard works as expected within the app itself. However, if a user refreshes the page or copies the URL and pastes it into a browser where they are already logged in, they should still be able to access the protected pages. Unfortunately, that is not happening at the moment, and I'm encountering this error: [vue-router] uncaught error during route navigation. My desired functionality is to redirect users to the login page if they try to access a guarded page without being logged in, but allow them to view the page if they are logged in.
beforeEnter: (to, from, next) => {
if (!store.state.isUserLoggedIn) {
next({
path: '/login', // redirect to login page
query: {
redirectFrom: to.fullPath
}
})
} else {
next()
}