Unable to set Authorization headers in Response [apollo-server-koa]
Hey there,
I am using [email protected] and unable to set authorization headers from the resolver.
// auth resolver
.....
login(root, args, ctx) {
// after successfull login
// I am adding the token in the response headers the same way I was using in other Koa projects
ctx.response.set('Authorization', token)
}
....
In response, there are no authorization headers being added to the client.
I think you're supposed to send the token to the client via Cookies
@ashokvishwakarma Did you find a way?
@ashokvishwakarma Did you find a way?
Yes, using the formatResponse property
First set the _token in context in callback
login(root, args, context) {
// do you logic
context._token = '<your_token>';
}
Then use the same in formatResponse property to set in headers
const server = new ApolloServer({
formatResponse: (response, options) => {
if(response.errors) {
logger.error(response.errors);
}
if(options.context._token) {
options.context.ctx.response.set('Authorization', options.context._token);
}
return response;
}
});
Hope this helps.
We won't be directly maintaining integrations with specific frameworks in Apollo Server 3, so closing this issue.