Skip to content

Setting cookie in the response header is not working #473

@rasyhad

Description

@rasyhad

Hi,

Basically, I am trying to create an authentication through cookie instead of storing the JWT in the localStorage.
I have a REST API endpoint (built with Loopback4) for login and I am making a graphql request from the frontend for user login.

On successful login, I am setting the cookie in the response. Note that this is happening in the controller of my REST API.

response.cookie('SESSIONID', 'something', {...})

I understand that openapi-to-graphql works in a way that the graphql request is converted to make a call to the REST API and then converts back the response into a graphql response.

However, seems like the cookie in the header got lost in the process. Even any header that I set in the login process is not carried to the graphql response header.

In a standalone GraphQL server with typeDefs and resolver, we can pass the context into the resolver and set the cookie in th response.

In the getResolver function, could we not simply set the cookie since variable resolveData hold the response headers data?

Example of modification.

if (response.headers.get('content-type').includes('application/json')) {
   let responseBody;
   try {
      responseBody = JSON.parse(body);
   }
   catch (e) {
       const errorString = `Cannot JSON parse response body of ` +
       `operation ${operation.operationString} ` +
        `even though it has content-type 'application/json'`;
         httpLog$1(errorString);
          throw new Error(errorString);
    }
    resolveData.responseHeaders = {};
    response.headers.forEach((val, key) => {
         resolveData.responseHeaders[key] = val;

         // add this
         context.res.setHeader(key, val)

    });

    ...

}

Can someone help to point me in the right direction?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions