Feathers: Trouble setting up feathers/client

Created on 14 Mar 2016  ·  4Comments  ·  Source: feathersjs/feathers

I have the following code

import feathers from 'feathers/client';
import rest from 'feathers-rest/client';
import request from 'browser-request';
import config from './config';

import User from 'app/utilities/user';

const client = request.defaults({
  headers: {
    Authorization: `Bearer ${User.get().token}`
  }
});

export default feathers().configure(
  rest(config.host).request(client)
);

However, when I call create on a service it does not send the Authorization header.

Alternatively, how can I set up feathers with superagent to send along specific headers?

Thanks!

All 4 comments

For superagent and any other support REST library you can also set params.headers when making a service request:

app.service('todos').find({ headers: {
    Authorization: `Bearer ${User.get().token}`
  }
});

In the feathers-authentication client we are using that in a hook.

The problem with Request here is probably that the headers get overwritten now by those settings (even when they are empty).

Awesome! :smile: This worked perfectly.

Ended up going with this:

import feathers from 'feathers/client';
import rest from 'feathers-rest/client';
import hooks from 'feathers-hooks';
import superagent from 'superagent';

import User from 'app/utilities/user';
import config from './config';

const app = feathers()
  .configure(hooks())
  .configure(
    rest(config.host).superagent(superagent)
  );

function authHook(hook) {
  hook.params.headers = Object.assign({}, {
    Authorization: `Bearer ${User.get().token}`
  }, hook.params.headers);
}

export default {
  service(url) {
    let s = app.service(url);
    s.before({
      all: authHook
    });
    return s;
  }
};

Looks great! One other thing you could do is make it a service mixin so it will get added to all services automatically:

app.mixins.push(function(service) {
  service.before(authHook);
});

Mixins are not publicly documented yet but it's what all the plugins use to hook into a service.

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue with a link to this issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

huytran0605 picture huytran0605  ·  3Comments

rstegg picture rstegg  ·  3Comments

rrubio picture rrubio  ·  4Comments

arkenstan picture arkenstan  ·  3Comments

Vincz picture Vincz  ·  4Comments