const router = createRouter({
history: createWebHistory(),
routes,
});
// ...
const allowedToAnonymous = [
/^\/login$/g,
/^\/signup$/g,
/^\/home$/g,
/^\/emailconfirm\/[0-9a-zA-Z]{8}$/g,
/^\/serviceGuide$/g,
/^\/$/g
];
router.beforeEach((to, from) => {
for (var regex of allowedToAnonymous) {
console.log(regex);
console.log(to.path);
if (regex.test(to.path)) {
console.log('return');
return;
} else {
console.log(regex.test(to.path));
console.log('do not return');
}
}
const checkLogin = stores.getters['userStore/checkLogin'];
if (!checkLogin) return '/login';
});
I implemented this code in my Vue.js project. It directs users to the login page if certain regular expressions do not match the current path and they are not logged in. All console.log
statements are for debugging purposes. When testing in a web browser, I observed the following behavior:
/^\/login$/g
/serviceGuide
false
do not return
/^\/signup$/g
/serviceGuide
false
do not return
/^\/home$/g
/serviceGuide
false
do not return
/^\/emailconfirm\/[0-9a-zA-Z]{8}$/g
/serviceGuide
false
do not return
/^\/serviceGuide$/g
/serviceGuide
true
do not return
/^\/$/g
/serviceGuide
false
do not return
/^\/login$/g
/login
return
Pay attention to the output near /^\/serviceGuide$/g
. Even when regex.test
returns true, it seems to follow the do not return
path. This inconsistency occurs after browsing the website multiple times, typically 2 to 4 instances.
What could be causing this issue and how can it be resolved?