Search code examples
feathersjs

Axios Patch On FeathersJs No auth token


I have a problem with Patch in FeathersJS.

I want to update the data with axios.patch

but the message that appears is always No auth token

{"name":"NotAuthenticated","message":"No auth token","code":401,"className":"not-authenticated","data":{},"errors":{}}

This my axios :

Aktifasi() {
  axios.patch(process.env.ROOT_API+'/ek_user?id_user=2',
  qs.stringify({
    headers: {
      'Authorization': 'Bearer ' + localStorage.getItem('token'),
      'Content-Type': 'application/json',
    },
    active_user: 1
  }))
  .then(request => this.AktifasiSuccesSend(request))
  .catch((error) => this.AktifasiFailedSend(error))
},
AktifasiSuccesSend (request) {
  console.log('Yay');
},
AktifasiFailedSend (error) {
  console.log('Oh Fail');
}

And this Hook on FeathersJS :

   before: {
    all: [],
    find: [ authenticate('jwt') ],
    get: [ authenticate('jwt') ],
    create: [ hashPassword() ],
    update: [ hashPassword(),  authenticate('jwt') ],
    patch: [ hashPassword(),  authenticate('jwt') ],    
    remove: [ authenticate('jwt') ]
  },

Solution

  • As the Axios configuration documentation shows, headers are passed as a separate option not as a stringified query string (which shouldn't be necessary at all):

    const data = {
      active_user: 1
    };
    const config = {
      headers: {
        'Authorization': 'Bearer ' + localStorage.getItem('token'),
        'Content-Type': 'application/json',
      }
    };
    
    axios.patch(process.env.ROOT_API + '/ek_user?id_user=2', data, config);