Answer the question
In order to leave comments, you need to log in
How to properly set up routing protection in Vue?
There is such a route
{
path: '/orders',
name: 'Orders',
component: Orders,
beforeEnter: (from, to, next) => {
if(store.getters.user) {
next()
} else {
next('/login')
}
}
mounted() { //проверка при перезагрузке на зареген ли пользователь или нет
firebase.auth().onAuthStateChanged(user => { //проверка при первой загрузке и перезагрузке на то зареген ли пользователь
if (user) {
this.$store.dispatch('autoLoginUser', user)
}
});
this.$store.dispatch('fetchAds');
}
router.beforeEach((to, from, next) => {
if (!isAuthenticated) next('/login')
else next()
})
Answer the question
In order to leave comments, you need to log in
Below is an example of a route guard with firebase:
router.beforeEach((to, from, next)=>{
const currentUser = firebase.auth().currentUser;
const requireAuth = to.matched.some(record => record.meta.auth);
if(requireAuth && !currentUser){
next({name:'signIn'})
}else{
next();
}
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question