【发布时间】:2019-08-24 02:15:03
【问题描述】:
目前,当我重新加载仪表板时,它首先重定向到 /login,然后如果用户已经登录,则重定向到 /dashboard。它看起来很有线。如果用户登录,我该如何解决,使其直接降落到 /dashboard。
在 main.js 中创建函数
created: function() {
try {
firebase.initializeApp(firebaseConfig);
}
catch(error){
return;
}
const store = this.$store;
firebase.auth().onAuthStateChanged(function(user){
if(typeof user !== 'undefined' && user !== null){
store.dispatch('loginUserOnLoad', user);
}
});
}
LoginUserOnlOad 操作
loginUserOnLoad: function({ commit }, user){
commit('authUser',{
email: user.email,
fullname: 'Guest'
})
},
这里是完整的路由器配置,
Vue.use(Router);
const router = new Router({
mode: 'history',
routes: [
{
path: '/',
name: 'Welcome',
component: Welcome
},
{
path: '/tasks',
name: 'Tasks',
component: Layout,
meta: {
requireAuth: true
}
},
{
path: '/login',
name: 'Login',
component: Signin,
meta: {
guestAuth: true
}
},
{
path: '/register',
name: 'Signup',
component: Signup,
meta: {
guestAuth: true
}
},
{
path: '*',
name: 'NotFound',
component: NotFound
}
]
});
router.beforeEach((to, from, next) => {
const currentUser = firebase.auth.currentUser;
const requireAuth = to.matched.some(record => record.meta.requireAuth);
if(requireAuth && !currentUser){
next({ name: 'Login'});
}
else if(!requireAuth && currentUser){
next({ name: 'Tasks'});
}
else {
next();
}
});
export default router;
【问题讨论】:
-
你用路由器吗?
-
路由器添加到问题请检查。
-
仪表板的路径是什么?
-
@renaud tarnec /task
标签: firebase vue.js vuejs2 firebase-authentication vue-router