aws-amplify / amplify-category-api

The AWS Amplify CLI is a toolchain for simplifying serverless web and mobile development. This plugin provides functionality for the API category, allowing for the creation and management of GraphQL and REST based backends for your amplify project.
https://docs.amplify.aws/
Apache License 2.0
89 stars 77 forks source link

Testing in Amplify mock api get Unauthorized #1884

Open juri-diener opened 1 year ago

juri-diener commented 1 year ago

Before opening, please confirm:

JavaScript Framework

React Native

Amplify APIs

Authentication, GraphQL API

Amplify Categories

auth, api

Environment information

``` # Put output below this line System: OS: macOS 13.4 CPU: (8) arm64 Apple M1 Memory: 137.44 MB / 16.00 GB Shell: 5.9 - /bin/zsh Binaries: Node: 20.6.1 - /opt/homebrew/bin/node npm: 9.8.1 - /opt/homebrew/bin/npm Watchman: 2023.09.04.00 - /opt/homebrew/bin/watchman Browsers: Chrome: 116.0.5845.187 Safari: 16.5 npmPackages: @aws-amplify/ui-react-native: ^1.2.27 => 1.2.27 @babel/core: ^7.20.0 => 7.22.17 @react-native-async-storage/async-storage: 1.18.2 => 1.18.2 @react-native-community/netinfo: 9.3.10 => 9.3.10 @types/react: ~18.2.14 => 18.2.21 HelloWorld: 0.0.1 amazon-cognito-identity-js: ^6.3.5 => 6.3.6 amazon-cognito-identity-js/internals: undefined () aws-amplify: ^5.3.11 => 5.3.11 aws-amplify-react-native: ^7.0.6 => 7.0.6 expo: ~49.0.8 => 49.0.9 expo-constants: ~14.4.2 => 14.4.2 expo-linking: ~5.0.2 => 5.0.2 expo-router: ^2.0.0 => 2.0.5 expo-status-bar: ~1.6.0 => 1.6.0 react: 18.2.0 => 18.2.0 react-native: 0.72.4 => 0.72.4 react-native-gesture-handler: ~2.12.0 => 2.12.1 react-native-get-random-values: ^1.9.0 => 1.9.0 react-native-safe-area-context: 4.6.3 => 4.6.3 react-native-screens: ~3.22.0 => 3.22.1 react-native-url-polyfill: ^2.0.0 => 2.0.0 (1.3.0) react-native-web: ~0.19.6 => 0.19.9 typescript: ^5.1.3 => 5.2.2 npmGlobalPackages: @aws-amplify/cli: 10.6.2 n: 9.1.0 npm: 9.8.1 ```

Describe the bug

Get Unauthorized as response.

{
  "data": {
    "createUser": null
  },
  "errors": [
    {
      "message": "Unauthorized",
      "errorType": "Unauthorized",
      "data": {},
      "errorInfo": {},
      "path": [
        "createUser"
      ],
      "locations": [
        {
          "line": 2,
          "column": 3,
          "sourceName": "GraphQL request"
        }
      ]
    }
  ]
}

Expected behavior

Should just work!!!

Reproduction steps

amplify mock api open the url run mutation

mutation MyMutation {
  createUser(input: {id: "1233"}) {
    id
  }
}

Code Snippet

// Put your code below this line.

Log output

``` // Put your logs below this line ```

aws-exports.js

const awsmobile = { "aws_project_region": "us-east-1", "aws_appsync_graphqlEndpoint": "http://192.168.0.172:20002/graphql", "aws_appsync_region": "us-east-1", "aws_appsync_authenticationType": "API_KEY", "aws_appsync_apiKey": "da2-fakeApiId123456", "aws_appsync_dangerously_connect_to_http_endpoint_for_testing": true, "aws_cognito_identity_pool_id": "us-east-1:961e69fb-f2d3-4e38-9ba4-...", "aws_cognito_region": "us-east-1", "aws_user_poolsid": "us-east-1.....", "aws_user_pools_web_client_id": "....", "oauth": {}, "aws_cognito_username_attributes": [ "EMAIL" ], "aws_cognito_social_providers": [], "aws_cognito_signup_attributes": [ "NAME" ], "aws_cognito_mfa_configuration": "OFF", "aws_cognito_mfa_types": [ "SMS" ], "aws_cognito_password_protection_settings": { "passwordPolicyMinLength": 8, "passwordPolicyCharacters": [ "REQUIRES_LOWERCASE", "REQUIRES_NUMBERS", "REQUIRES_SYMBOLS", "REQUIRES_UPPERCASE" ] }, "aws_cognito_verification_mechanisms": [ "EMAIL" ] };

export default awsmobile;

Manual configuration

No response

Additional configuration

No response

Mobile Device

No response

Mobile Operating System

No response

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

type User @model @auth(rules: [{ allow: public }]) { id: ID! email: String bio: String profileImg: String isChurch: Boolean nofPosts: Int nofFollowers: Int nofFollowings: Int }

AnilMaktala commented 1 year ago

Hi @juri-diener, Thank you for bringing up this issue. It appears that you are utilizing an outdated version of the Amplify CLI (10.6.2). Would you please consider updating to the most recent version and then attempting the mock functionality again?

juri-diener commented 1 year ago

Hi @juri-diener, Thank you for bringing up this issue. It appears that you are utilizing an outdated version of the Amplify CLI (10.6.2). Would you please consider updating to the most recent version and then attempting the mock functionality again?

The problem is, I had the latest version but it was even worse. I could not do anything in the explorer. Was only a white field. It looked something like this. And where the text is, this was as well just a blank white field.

Screenshot 2023-09-26 at 21 20 35

I does not recognize my schema with the latest version.

Only when I did a downgrade it worked. So that I could run some queries, mutations. The Problem now is that I get the Unauthorized error.

What is now the better way to fix this issue or update and then fix the issue that my schema is not recognized.

juri-diener commented 1 year ago

So I tried with a new project and chose as api Cognito user pool instead of api. And it's working now. But there is one issue I don't understand. If I change from private to public on this code: @auth(rules: [{ allow: private, operations: [read] }, { allow: owner }]) {

I get this error: InvalidDirectiveError: @auth directive with 'apiKey' provider found, but the project has no API Key authentication provider configured.

phani-srikar commented 1 year ago

Hi @juri-diener, did you add both API_KEY and Cognito user pools Auth providers to your API? You can do so via amplify update api and following: ? Select a setting to edit Authorization modes ? Choose the default authorization type for the API API key ✔ Enter a description for the API key: · ✔ After how many days from now the API key should expire (1-365): · 7 ? Configure additional auth types? Yes ? Choose the additional authorization types you want to configure for the API Amazon Cognito User Pool.

I am however able to reproduce the UnAuthorized error if using only API_KEY from the mock console. Marking it as a bug.