A
A
Artem2018-06-07 21:05:50
Vue.js
Artem, 2018-06-07 21:05:50

How to fix authorization in VueJS + Vuex + Router + Firebase?

Hello!
Started learning this wonderful VueJS framework and already faced one unsolvable problem. Maybe someone with more experience will chime in.
I made a project with authorization and a database on firebase.
In main.js I do

fb.auth().onAuthStateChanged(function (user) {
      if (user) {
        p.$store.dispatch('autoLoginUser', user)
      }
    })

Accordingly, in autoLoginUser I assign the value of the authorized user from firebase to the global variable store.user.
In each necessary route I check:
if (store.getters.user) {
    next()
  } else {
    next('/login?loginError=true')
  }

Each relevant route is, of course, secured with
beforeEnter: AuthGuard
Generally works fine until the user hits F5 while on a secure route (eg localhost:8080/orders)
At this point, redirects to the login page.
It seems that the beforeEnter check is triggered first, and then firebase's onAuthStateChanged, although logically it should be the other way around.
Tell me where to dig?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question