I'm doing a little practice with Vue
and Firebase
, I have already managed to create users and configure Firebase
but I have a problem when restricting routes with VueRouter
, with what I have read it should be enough with the property requiresAuth
to restrict it if there is no logged in user, but it does not work in this way:
{
path: '/home',
name: 'Home',
component: Home,
meta: { requiresAuth: true }
}
But the route is still accessible even though no user has logged in. Do you need any extra configuration?
My route file is this:
Vue.use(Router)
export default new Router({
routes: [
{
path: '*',
redirect: '/'
},
{
path: '/',
name: 'Index',
component: Index
},
{
path: '/register',
name: 'Register',
component: Register
},
{
path: '/home',
name: 'Home',
component: Home,
meta: { requiresAuth: true }
}
]
})
Router.beforeEach((to, from, next) => {
let usuarioActivo = firebase.auth().currentUser;
let requiresAuth = to.matched.some(record => record.meta.requiresAuth);
if (requiresAuth && !usuarioActivo) next('index')
else if (!requiresAuth && usuarioActivo) next('home')
else next()
})
By adding the beforeEach and asking if the route requires authorization, I'm getting this error: