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