Vue-Laravel-SPA icon indicating copy to clipboard operation
Vue-Laravel-SPA copied to clipboard

Passing token via general.js file

Open emil126a opened this issue 7 years ago • 4 comments

axios.defaults.headers.common["Authorization"] does not work. Each request call says "Token not provided". Would be nice if you fixed.

emil126a avatar Oct 10 '18 07:10 emil126a

@emil126a I had the same issue with the Authorization Header is not set after page refresh. I solve this by creating created method and reinitialize the Authorization Header if user is found in the local storage.

var app = new Vue({
    el: '#app',
    router,
    store,
    components: {
        'main-app': MainComponent
    },
    created() {
        const user = getUser();
        if(user) {
            setAuthenticationHeader(user.token);
        }
    }
});

zayyarmin3 avatar Oct 11 '18 07:10 zayyarmin3

Make sure the value of Authorization has a prefix of "Bearer "+token Take note it should have a space in between, something like "Bearer ad45tegdzgftryhgcxbsydhtv5y"

ajcastro avatar Oct 16 '18 12:10 ajcastro

const user = getUser();

getUser is not defined...

boyet007 avatar Dec 09 '18 07:12 boyet007

Ah. Sorry I forgot to mention. You gotta have to import those methods from auth.js in helper folder. LIke This

import {setAuthenticationHeader, getUser} from './helpers/auth';

zayyarmin3 avatar Dec 09 '18 09:12 zayyarmin3