Firebase beforeeach() fires a lot of times (Vue app)










0















I am building posts app using Firebase and Vue js. I have some issue:
My router:



import Vue from 'vue'
import Router from 'vue-router'
import Dashboard from '@/components/Dashboard'
import Login from '@/components/Login'
import SignUp from '@/components/SingUp'
import RecentPosts from '@/components/RecentPosts';
import firebase from 'firebase'

Vue.use(Router)

let router = new Router(
routes: [
...... some routes

path: '/recentPosts',
name: 'RecentPosts',
component: RecentPosts,
meta:
requireAuth: true


]
)
router.beforeEach((to, from, next) =>
let currentUser = firebase.auth().currentUser;
let requiresAuth = to.matched.some(r => r.meta.requiresAuth);
console.log("CURRENTUSER",currentUser);
if(!currentUser && requiresAuth)
next('Login');
else if(currentUser && !requiresAuth)
next('Dashboard');
else if (currentUser && requiresAuth)
next();
else
next();

);
export default router;


After i go to /recentPosts my function beforeeach fires so many times so that it blocks my app.
Why I cant access /recentPosts ? I have logged in user so it should resolve my rout, but it's not.



else if (currentUser && requiresAuth) 
next();



It should resolbe my '/recentsposts' route, but it isn't










share|improve this question


























    0















    I am building posts app using Firebase and Vue js. I have some issue:
    My router:



    import Vue from 'vue'
    import Router from 'vue-router'
    import Dashboard from '@/components/Dashboard'
    import Login from '@/components/Login'
    import SignUp from '@/components/SingUp'
    import RecentPosts from '@/components/RecentPosts';
    import firebase from 'firebase'

    Vue.use(Router)

    let router = new Router(
    routes: [
    ...... some routes

    path: '/recentPosts',
    name: 'RecentPosts',
    component: RecentPosts,
    meta:
    requireAuth: true


    ]
    )
    router.beforeEach((to, from, next) =>
    let currentUser = firebase.auth().currentUser;
    let requiresAuth = to.matched.some(r => r.meta.requiresAuth);
    console.log("CURRENTUSER",currentUser);
    if(!currentUser && requiresAuth)
    next('Login');
    else if(currentUser && !requiresAuth)
    next('Dashboard');
    else if (currentUser && requiresAuth)
    next();
    else
    next();

    );
    export default router;


    After i go to /recentPosts my function beforeeach fires so many times so that it blocks my app.
    Why I cant access /recentPosts ? I have logged in user so it should resolve my rout, but it's not.



    else if (currentUser && requiresAuth) 
    next();



    It should resolbe my '/recentsposts' route, but it isn't










    share|improve this question
























      0












      0








      0








      I am building posts app using Firebase and Vue js. I have some issue:
      My router:



      import Vue from 'vue'
      import Router from 'vue-router'
      import Dashboard from '@/components/Dashboard'
      import Login from '@/components/Login'
      import SignUp from '@/components/SingUp'
      import RecentPosts from '@/components/RecentPosts';
      import firebase from 'firebase'

      Vue.use(Router)

      let router = new Router(
      routes: [
      ...... some routes

      path: '/recentPosts',
      name: 'RecentPosts',
      component: RecentPosts,
      meta:
      requireAuth: true


      ]
      )
      router.beforeEach((to, from, next) =>
      let currentUser = firebase.auth().currentUser;
      let requiresAuth = to.matched.some(r => r.meta.requiresAuth);
      console.log("CURRENTUSER",currentUser);
      if(!currentUser && requiresAuth)
      next('Login');
      else if(currentUser && !requiresAuth)
      next('Dashboard');
      else if (currentUser && requiresAuth)
      next();
      else
      next();

      );
      export default router;


      After i go to /recentPosts my function beforeeach fires so many times so that it blocks my app.
      Why I cant access /recentPosts ? I have logged in user so it should resolve my rout, but it's not.



      else if (currentUser && requiresAuth) 
      next();



      It should resolbe my '/recentsposts' route, but it isn't










      share|improve this question














      I am building posts app using Firebase and Vue js. I have some issue:
      My router:



      import Vue from 'vue'
      import Router from 'vue-router'
      import Dashboard from '@/components/Dashboard'
      import Login from '@/components/Login'
      import SignUp from '@/components/SingUp'
      import RecentPosts from '@/components/RecentPosts';
      import firebase from 'firebase'

      Vue.use(Router)

      let router = new Router(
      routes: [
      ...... some routes

      path: '/recentPosts',
      name: 'RecentPosts',
      component: RecentPosts,
      meta:
      requireAuth: true


      ]
      )
      router.beforeEach((to, from, next) =>
      let currentUser = firebase.auth().currentUser;
      let requiresAuth = to.matched.some(r => r.meta.requiresAuth);
      console.log("CURRENTUSER",currentUser);
      if(!currentUser && requiresAuth)
      next('Login');
      else if(currentUser && !requiresAuth)
      next('Dashboard');
      else if (currentUser && requiresAuth)
      next();
      else
      next();

      );
      export default router;


      After i go to /recentPosts my function beforeeach fires so many times so that it blocks my app.
      Why I cant access /recentPosts ? I have logged in user so it should resolve my rout, but it's not.



      else if (currentUser && requiresAuth) 
      next();



      It should resolbe my '/recentsposts' route, but it isn't







      javascript firebase vue.js firebase-authentication vue-router






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 12:17









      lukluk

      345




      345






















          1 Answer
          1






          active

          oldest

          votes


















          2














          This is probably because you have a typo at require(s):



           
          path: '/recentPosts',
          name: 'RecentPosts',
          component: RecentPosts,
          meta:
          requireAuth: true // <- without "s" at the end of require


          ]
          })
          router.beforeEach((to, from, next) =>
          let currentUser = firebase.auth().currentUser;
          let requiresAuth = to.matched.some(r => r.meta.requiresAuth); // <- with an "s" at the end of requires





          share)
          router.beforeEach((to, from, next) =>
          improve this answer





















            share)
            router.beforeEach((to, from, next) =>
            let currentUser = firebase.auth().currentUser;
            let requiresAuth = to.matched.some(r => r.meta.requiresAuth); // <- with an "s" at the end of requires





            share)
            router.beforeEach((to, from, next) => {
            let currentUser = firebase.auth().currentUser;
            let requiresAuth = to.matched.some(r => r.meta.requiresAuth); // <- with an "s" at the end of requires






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 14 '18 at 12:29









            Renaud TarnecRenaud Tarnec

            11.6k21531




            11.6k21531





























                draft saved

                draft discarded
















































                Thanks for contributing an answer to Stack Overflow!


                • Please be sure to answer the question. Provide details and share your research!

                But avoid


                • Asking for help, clarification, or responding to other answers.

                • Making statements based on opinion; back them up with references or personal experience.

                To learn more, see our tips on writing great answers.




                draft saved


                draft discarded














                StackExchange.ready(
                function ()
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300065%2ffirebase-beforeeach-fires-a-lot-of-times-vue-app%23new-answer', 'question_page');

                );

                Post as a guest















                Required, but never shown





















































                Required, but never shown














                Required, but never shown












                Required, but never shown







                Required, but never shown

































                Required, but never shown














                Required, but never shown












                Required, but never shown







                Required, but never shown







                這個網誌中的熱門文章

                How to read a connectionString WITH PROVIDER in .NET Core?

                Node.js Script on GitHub Pages or Amazon S3

                Museum of Modern and Contemporary Art of Trento and Rovereto