3

I'm using Laravel API passport for my SPA Authentication using vue. So far what I made is each time I need to access my backend side I need to call header in able to accept by my protected route

const userObj = JSON.parse(window.localStorage.getItem('token'));
var header = {
    'Accept' : 'application/json',
    'Authorization' : 'Bearer '+ userObj.access_token
}
axios.get('/prod/test',{headers : header})
    .then(response=>{
        console.log(response);

    });
}

Is there any much cleaner way to do this thanks

1 Answer 1

6

You can set default values to axios: https://github.com/axios/axios#global-axios-defaults

axios.defaults.headers.common['Accept'] = 'application/json'
axios.defaults.headers.common['Authorization'] = 'Bearer '+ userObj.access_token;

In Laravel, you already have the file bootstrap.js which contain some pre-configuration that you can edit.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.