I'm having a hard time passing Data (in this case userInfo-Token) from inside a beforeEach(to, from, next)
Vue-Router middleware to the coressponding Vue-Component. I'm working on Vue-SinglePage components files as following:
App.js (entry Point)
<template>
<div id="app">
<Navigation/>
<router-view/>
<Footer/>
</div>
</template>
Router.js (router-view)
routes: [
{
path: '/',
meta: {requiresAuth: true}
}
]
router.beforeEach((to, from, next) => {
if(to.meta.requiresAuth){
// getting token form local storage
const token = localStorage.getItem('id_token');
// retrieving User Information
axios.defaults.headers.common['Authorization'] = "Bearer "+ token;
axios.post('http://localhost:4000/auth').then((result) => {
next(); // <-- how will the following component be able to work with the result?
}
}
}
Dashboard.js ('./' Component)
export default {
computed: {
welcomMsg: () => 'Hello ' + result.userName
}
}
What i did so far: I tried a passing the userInfo from Entry-Point -> Router -> Component as properties. However it didnt work due to the information being asynchronos.
I tried to attach the data inside the beforeEach
to the meta-Object. But I find myself unable to access the meta-Object inside the Component.
Maybe my approach is totally wrong. In this case: Is there a better way to pass the recieved UserData to Vue Components and make them avaliable there?
Thank you in advance.