AWS Cognito user able to sign in, but API “Does not exist” when using I can access using access token on console?

前提是你 提交于 2021-02-11 12:50:20

问题


Im a bit lost here and would love some help. I am trying to connect my React Native app to a basic Lambda function.

I have no bother Registering a user, Confirming a user or Signing in. I can see all of these things in the Cognito Console.

I also am able to test the access to the API with the Authorizers part of the API Gateway and get a Response Code: 200 with the correct email from cognito.

Here is the code:

      async signIn(email: string, password: string) {
        try {
          //sign in the user
          const user = await Auth.signIn(email, password);
    
          AlertHelper.show('info', 'Signed in!', user)
    
          const token = user.signInUserSession.idToken.jwtToken
    
          console.log('token:', token )
    
          const requestInfo = {
           headers: {
            Authorization: token
           }
    
//get the Data Here
        const data = await API.get('API_NAME','/hello',requestInfo)
    
        console.log('Data: ',data )
    
      }
     
        }
        catch(error){
          console.log('Error: ', error)      
        }
    
      }

AWS-Exports.js file:

const awsmobile = {
    "aws_project_region": "eu-west-3",
    "aws_cognito_identity_pool_id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "aws_cognito_region": "eu-west-3",
    "aws_user_pools_id": "eu-west-3_xxxxxxxxx",
    "aws_user_pools_web_client_id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "oauth": {},
    "aws_cloud_logic_custom": [
        {
            "name": "API_NAME",
            "endpoint": "https://endpoint",
            "region": "eu-west-3"
        }
    ]
};

Error recieved:

API API_NAME does not exist

index.js from the function in Amplify

exports.handler = async (event) => {
    // TODO implement


    if(event.requestContext.authorizer){
        console.log('Claims: ',event.requestContext.authorizer.claims)
    }

    const response = {
        statusCode: 200,
    //  Uncomment below to enable CORS requests
     headers: {
         "Access-Control-Allow-Origin": "*",
         "Access-Control-Allow-Headers": "*"
     }, 
        body: JSON.stringify('Hello from Lambda!'),
    };
    return response;
};

来源:https://stackoverflow.com/questions/66014223/aws-cognito-user-able-to-sign-in-but-api-does-not-exist-when-using-i-can-acce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!