I'm currently working on an app that utilizes Vue.js and Firebase for authentication. I am trying to implement a verification process using the beforeEach
method in my router file to ensure that users can only sign in if their displayName
matches a specific value. However, I have encountered an error when using the else if
statement.
Snippet from my router file:
import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import firebase from 'firebase/app';
import 'firebase/auth';
Vue.use(VueRouter)
const routes = [
// Routes configuration
]
const router = new VueRouter({
// Router configuration
})
router.beforeEach((to, from, next) => {
const currentUser = firebase.auth().currentUser;
const requiresAuth = to.matched.some(record => record.meta.requiresAuth);
if (requiresAuth && !currentUser) next('login');
else if (!requiresAuth && currentUser) next('home');
// Verify user's displayName before granting access
else if (requiresAuth && currentUser && currentUser.displayName !== "3") next('login');
else next();
});
The issue arises in this line:
else if(requiresAuth && currentUser && currentUser.displayName !== "3") next('login');
. If anyone has suggestions on how to resolve this error, please let me know.