0
votes

In vue/cli 4 / vuejs 2.6.10 / vuex,3.0.1 app I make axios request

axios.get(apiUrl + '/personal/user_groups/'+user_id )  /
   .then((response) => {

to backend app(Laravel 5/jwt-auth) with routes in jwt.auth group :

Route::group(['middleware' => 'jwt.auth',  'prefix' => 'personal', 'as' => 'personal.'], function ($router) {
    Route::post('user_groups', 'API\PersonalController@user_groups');

and got error :

"error":"UNAUTHORIZED_REQUEST"

I found a way of sending the POST request with config parameter and token parameter, which I got on successful login:

let config = {
    withCredentials:true, 
    headers: {
        'Content-Type': 'application/json',
        'Access-Control-Allow-Origin': '*',
    }
}
axios({ url: apiUrl + '/personal/user_groups', data: {user_id:user_id, token: this.getters.token}, method: 'POST', config : config })
    .then(response => {

But if there is a way to send the GET request and I made in the topic start?

1

1 Answers

0
votes

I found solution in adding Authorization parameter to request headers

settingCredentialsConfig.headers.Authorization = "Bearer " + this.getters.token;
axios.get(apiUrl + '/personal/user_groups/' + user_id, settingCredentialsConfig)
    .then((response) => {

where settingCredentialsConfig is defined in settings.js as

export const settingCredentialsConfig = {
    withCredentials:true, 
    headers: {
        'Content-Type': 'application/json',
        'Access-Control-Allow-Origin': '*',
    }
}