Skip to content

Checking if signed in beforeEnter in Vue-router #738

Answered by tawjaw
tawjaw asked this question in Questions
Discussion options

You must be logged in to vote

I couldn't check beforeEnter the status of the user so now I have the following Mixin

export const loggedInMixin = Vue.extend({
  beforeMount() {
    if (this.isLoggedIn) this.$router.push("/");
  },

  computed: {
    isLoggedIn: function() {
      return store.state.isLoggedIn;
    },
    userInfo: function() {
      return store.state.user;
    }
  },
  watch: {
    isLoggedIn: {
      handler: function(value) {
        if (value) router.push({ path: "/" });
      }
    }
  }
});

and in every view I add this Mixin

export default Vue.extend({
  mixins: [loggedInMixin]
});

I don't know if there is a better way for this.

One advantage of this is that it works also with multiple tabs if …

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by tawjaw
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant