简体   繁体   English

如何解决页面重新加载,Firebase和Vuejs上身份验证的重新加载问题

[英]How to fix reloading issue of authentication on page reload reload, Firebase and Vuejs

Currently when I reload dashboard first its redirect to /login then /dashboard if user already login. 当前,当我重新加载仪表板时,首先将其重定向到/ login,然后如果用户已经登录,则重定向到/ dashboard。 Its look quite wired. 它看起来很有线。 How Can I fix so that its land directly to /dashboard if user logged in. 如果用户登录,我该如何解决,使其直接进入/ dashboard。

Created function in main.js 在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 action LoginUserOnlOad操作

loginUserOnLoad: function({ commit }, user){
        commit('authUser',{
            email: user.email,
            fullname: 'Guest'
        })
    },

Here is complete router configuration, 这是完整的路由器配置,

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;

I don't know how you configure and export Firebase, but I think that you should modify your router code as follows (see comments in the code): 我不知道如何配置和导出Firebase,但我认为您应该按以下方式修改路由器代码(请参阅代码中的注释):

router.beforeEach((to, from, next) => {

    //Instead of     const currentUser = firebase.auth.currentUser;  do
    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();
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 即使在具有 firebase 身份验证的 nextjs 应用程序中重新加载页面后,如何保持用户登录? - how can I keep a user looged in even after page reload in nextjs app with firebase authentication? firebase currentUser 在页面重新加载时是 null - firebase currentUser is null on page reload 在没有 window 重新加载的情况下自动刷新 firebase 身份验证? - Auto refreshing firebase authentication without a window reload? 从firebase删除数据后如何重新加载页面? - How to reload page after deleting data from firebase? 如何在执行 Firebase 删除请求后重新加载页面 - How to reload the page after a Firebase delete request has executed 在页面重新加载时处理Firebase中的异步身份验证,以获取需要用户uid的列表 - Handle asynchronous authentication in Firebase on page reload to get list that needs user's uid Firebase存储不会运行,除非在vuejs2中通过热重载更改了代码 - firebase storage not running unless code changed with hot reload in vuejs2 Firebase 身份验证在重新加载页面时返回 false - Firebase authentication returning false when reloading the page Firebase个人资料图片在页面重新加载时消失 - Firebase profile picture dissappears on page reload Firebase Auth Web:注销并重新加载页面 - Firebase Auth Web: Logout with page reload
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM