0

I am kinda new to VueJS and I am trying to pass the role of a user from the login path after he/she has been authenticated to the home page where I can then determine what links the user can see using v-if. Below is my login.vue code:

this.$axios({
                      method: 'post',
                      url: 'api/role',
                      data: {
                        'email': this.username
                      },
                      headers: {'Authorization': 'Bearer '+ this.$tokens.getToken()}
                    })
                    .then(response => {
                        this.role = response.data['role'];
                        
                    })
    				this.$router.push('/');
    			})
    			.catch(function(error){
    				console.log(error);
    			})

How can I pass the 'this.role' value to path '/' so I can access it from there?

2

1 Answer 1

0

You can use a query to pass the role.

So add a query where you programmatically navigate the user to homepage:

this.$router.push({path:'/', query:{role: this.role}});

Then in your home page component you can retrieve the route queries for your functionality

For example:

//homme page component

created(){
  if(this.$route.query.role === 'admmin){
    //this is admin
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your answer. But I actually want to avoid passing the role via url as the user can easily edit the url parameters and access restricted pages.
Do you have any clue how to avoid passing it by parameters @Kingsley ?
Urrhh.. @Kelvin, What I eventually had to do was to use the Vue state management tool (Vuex) to handle role management and other sensitive information like token which I didn't want to pass via url.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.