Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
292 views
in Technique[技术] by (71.8m points)

javascript - Vue router Auth-guard function doesn't check Vuex store state

I am trying to set-up an auth-guard for a profile page. I am using Vue (2.0) with vue-router and Vuex for state. My backend is firebase, but I don't think that's imporant here? My auth-guard function seems unable to properly fetch the requiresAuth state.

This is my auth-guard function:

  {
path: "/profile",
name: "Profile",
component: Profile,
meta: {
  requiresAuth: true
}


}
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes
});

router.beforeEach((to, from, next) => {  
  console.log('-----------ROUTE GUARD PROPS--------');
  console.log(store.state.user.isAuthorized);
  console.log(store.state.user);
  console.log('----------- END ROUTE GUARD PROPS--------');
  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (store.state.user.isAuthorized) {
      next()
    } else {
      next("/Login")
    }
  } else {
      next()
  }
})

And the result in the browser console when changing pages is the following: Browser Console

The issue here, to be clear, is why the console.log contradicts the isAuthorized in the print-out of the user module's state. For reference, my store has a user and shared module. The user module has a state with two members: user and isAuthorized. Hence the browser output.

Any ideas why these two conflict?

question from:https://stackoverflow.com/questions/65878659/vue-router-auth-guard-function-doesnt-check-vuex-store-state

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is happening because when you expand an object in the console, it will evaluate the object and its values based on the time of expanding, and not based on the time of logging it. It should tell you when the value was evaluated, if you hover the small blue square with an ! in it. enter image description here

Meaning that, when you log the object. isAuthorized is indeed false, as you can see when you log the property directly, but when you go and expand the object in the console it's true. Because at some point after logging it, some part of your program has changed ′isAuthorized′ to true.

The display also varies a little whether your console is open at the time of logging the value of not. The below screenshots are based on the snippet at the end of this post, so you can try it yourself.

The first image is how it looks if the console is closed at the time of logging. Here it simply shows Object, and when you expand it, it shows state is true.

enter image description here

In the second picture, the console was open when logging, and here it shows state: false, and when you expand it, it shows state: true, because it's fetching the current state of the object.

enter image description here

const object = {
  state: false
}

console.log('State:', object.state)
console.log('Object:', object)

object.state = true
Open up your browsers console to see the output.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...