apollo-server icon indicating copy to clipboard operation
apollo-server copied to clipboard

Unable to set Authorization headers in Response [apollo-server-koa]

Open avishwakarma opened this issue 8 years ago • 4 comments

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.

avishwakarma avatar Nov 20 '18 19:11 avishwakarma

I think you're supposed to send the token to the client via Cookies

ViableClanMember avatar Dec 11 '18 21:12 ViableClanMember

@ashokvishwakarma Did you find a way?

Xample avatar Mar 05 '19 10:03 Xample

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

avishwakarma avatar Mar 05 '19 10:03 avishwakarma

We won't be directly maintaining integrations with specific frameworks in Apollo Server 3, so closing this issue.

martijnwalraven avatar Jul 08 '19 22:07 martijnwalraven