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

Setting cookie in the response header is not working #473

Open
rasyhad opened this issue Aug 26, 2022 · 0 comments
Open

Setting cookie in the response header is not working #473

rasyhad opened this issue Aug 26, 2022 · 0 comments

Comments

@rasyhad
Copy link

rasyhad commented Aug 26, 2022

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant