Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trouble setting up feathers/client #262

Closed
RickEyre opened this issue Mar 13, 2016 · 4 comments
Closed

Trouble setting up feathers/client #262

RickEyre opened this issue Mar 13, 2016 · 4 comments

Comments

@RickEyre
Copy link

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!

@daffl
Copy link
Member

daffl commented Mar 13, 2016

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).

@RickEyre
Copy link
Author

Awesome! 😄 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;
  }
};

@daffl
Copy link
Member

daffl commented Mar 14, 2016

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.

@lock
Copy link

lock bot commented Feb 7, 2019

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.

@lock lock bot locked as resolved and limited conversation to collaborators Feb 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants