[Vuejs]-Store fetched data and use in components in Nuxtjs

0👍

Okey I find out a valid solution.

I add following lines to the middleware function:

export default async function ({ store, route, redirect, from, req }) {
    if (!from){
        let token = cookieparser.parse(req.headers.cookie).access_token;
        console.log('request send');
        await store.dispatch('users/getUserInfo', token);
    }
}

and my getUserInfo looks like this:

async getUserInfo({ commit }, token=null) {
        let access_token;
        (token != null) ? access_token = token : access_token = Cookie.get('access_token');
        const path = '/api/get_user_info';
        const res = await this.$axios.get(path, {
            headers: {
                Authorization: `Bearer ${access_token}`
            },});
        commit('SET_USER_INFO', res.data);
}

I think this is a valid solution. If you share your comments, I would be glad

reference: https://www.youtube.com/watch?v=sSbahlvDfS4

Leave a comment