CORS error when accessing API Gateway with Cognito authentication from React application

581 views Asked by At

I am using API gateway with cognito for Authorize a POST method endpoint. if I am running it through Postman (JSON) it works great. BUT when I am running through a react app in CHROME I get Access to XMLHttpRequest at 'https://wjkgn56pb8.execute-api.us-east-1.amazonaws.com/test' from origin 'http://localhost:3000' has been blocked by CORS policy:

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Details over my API gateway API

  1. POST request with cognito authorizaer with expect a Authorization header

  2. The post request has lambda proxy that return the following header:

    "headers": 'Access-Control-Allow-Credentials':'true', 'Access-Control-Allow-Headers': 'Content-Type,X-Amz-Date,X-Amz-Security-Token,Authorization,X-Api-Key,X-Requested-With,Accept', 'Access-Control-Allow-Methods':'DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT', 'Access-Control-Allow-Origin':'', 'X-Requested-With':''

  3. OPTIONS method wihtout cognito authorizer

  4. I enable CORS on the endpoint.

result:

  1. without cognito on the POST it works -> i get content into my react app

  2. the moment I add cognito authIi get CORS error

  3. the part of react code I am running for this is:

    async function testClick() {
        try {
            const data = {
                clientId: user.userId,
            };
           const config = {
                headers: {
                    Authorization: user.token,
                },
           };
           const res = await axios.post(
               "https://wjkgn56pb8.execute-api.us-east-1.amazonaws.com/test",
               data,
               config
           );
           const messageData = res.data;
           console.log(messageData);
      } catch (err) {
           console.error(err);
        } }
    

I don't have any idea.

0

There are 0 answers