amplify-js: "No current user" Error When Accessing AppSync Query using API.graphql
Do you want to request a feature or report a bug? bug
What is the current behavior? Attempting to access an AppSync query that has no access control on it results in a “No current user” error.
If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn’t have dependencies other than AWS Amplify.
Create an AppSync API with “Amazon Cognito User Pool” as the Authorization type and “ALLOW” as the Default action. Connect it to a user pool. Create a query with that does not have @aws_auth directive as described in the AppSync security documentation here
The query I created looks like this:

What is the expected behavior? The query should be run without requiring a user to sign in
Which versions of Amplify, and which browser / OS are affected by this issue? Did this work in previous versions? Chrome Version 67.0.3396.99 Firefox 61.0 AWS-Amplify 1.0 AWS-Amplify-React 1.0
You can turn on the debug mode to provide more info for us by setting window.LOG_LEVEL = 'DEBUG'; in your app.
Debug info indicates that the program is attempting to get the user from the user pool even though the user has not attempted to sign in yet:

About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 11
- Comments: 63 (12 by maintainers)
Links to this issue
Commits related to this issue
- Create second graphql api for public access See https://stackoverflow.com/questions/52675213/aws-appsync-unauthorized-exception https://github.com/aws-amplify/amplify-js/issues/1252 — committed to Flamous/flamous-backend by christiankaindl 6 years ago
- Add api key auth for public queries This allows unauthenticated users to view and search. Only authenticated users can upload images or make mutations. Amplify autogenerated configs will come in sepa... — committed to elliott-king/freedom-js-app by elliott-king 5 years ago
So basically when using AMAZON_COGNITO_USER_POOLS there cant be any public api? Really? Make zero sense. Having a way to create public api while still using cognito for auth users should be basic core functionality, i dont understand how its not here.
I configured multiple authentication modes on my
@modelas described here but requests would be authenticated using only the default authentication mode as specified usingamplify api configure.As show below, I had tried specifying the auth mode inside my service class, but this would be overwritten every time I updated my schema.
I eventually found that I could instead change the auth mode programatically by doing the following:
I hope this helps someone.
We’re working on making this easier via the CLI, but for now you can use the following steps to allow both Authenticated & Unauthenticated access to your AWS AppSync API:
In the AppSync API dashboard settings, change the authentication type to AWS Identity and Access Management (IAM)
In
aws.exports.json the client app, changeaws_appsync_authenticationTypetoAWS_IAMIn the Cognito dashboard, click “Manage Identity Pools” & click on your identity pool.
Click “Edit Identity Pool” to see your “Unauthenticated role” & “Authenticated Role”
Open the IAM console & find the “Unauthenticated role” from step 8
Click “Add inline policy”
Open the IAM console & find the “Authenticated role” from step 8
Click “Add inline policy”
If you’d like to access the unique identity of the logged in user for user authorization & fine grained access control, you can access the $context.identity.cognitoIdentityId) in the resolver.
Any solutions or workarounds to this? I’m getting the same error keeping track of user identities. Tried sending a Bearer Token manually with an Interceptor as well, but didn’t work. Going to try removing all
@authstatements from my GraphQL schema next.Edit: I think my problem is that I’m expecting DynamoDB access without having a logged-in user, which Amplify doesn’t seem to support at this point. So, I’ll have to store data locally in the meantime, and then create a syncing service layer to create rows in my DynamoDB table once the user has registered.
@vparpoil regarding the way
aws_appsync_authenticationTypeis overridden you can do this to preserve your change outside editting theaws-exports.jsdirectly.@reggie3 @GroveDev @selipso @aldarund @etwillbefine @rayhaanq to clarify, it sounds like you want to allow public access to your AppSync GraphQL API for all or part of your schema. Currently AppSync supports one auth mode at a time though supporting multiple auth modes simultaneously is something that is being evaluated for the 2019 roadmap. In the meantime if this is something you’re looking to do let me provide some options:
First, the most common solution is to use Cognito Identity and set your AppSync Authorization mode to AWS_IAM. You can then enable “unauthenticated access” in the Cognito Identity Pool which will allow the client to assume a role without logging in. You can set IAM permissions on that role for whatever parts of your GraphQL types you want to execute as outlined here: https://docs.aws.amazon.com/appsync/latest/devguide/security.html#aws-iam-authorization
Enabling UnAuthenticated access can be done with the amplify CLI when you run
amplify add authoramplify update authand do not select the default configuration (it’s disabled by default for least privilege). Alternatively you can perform this in the Cognito Identity console.Another alternative that some customers have done is to create two GraphQL APIs, one for unauthenticated/anonymous access and one for when the user is authenticated. The unauthenticated one uses a strict subset of the schema (for example, queries only) for the other API and is configured with API key for authorization. They both use the same data sources. Your client then uses the appropriate endpoint with authentication configuration as appropriate when you call
amplify.configureat which point yourAPI.graphqlcalls will respect this configuration.Hope this is helpful for the time being.
We’re looking at putting an example together for this. As outlined above if you want to allow multiple types of access permissions on the same API, at this point you will need to use IAM as AppSync doesn’t yet support multi-auth.
This statement by @chriszirkel “enabling unauth access means that possibly everyone on the frontend could call my APIs” needs clarification. Enabling unauthenticated roles in Identity Pools means the client can receive AWS credentials which are separate from the authenticated role. This allows you to assign different IAM permissions to those roles, per the link I pointed to earlier: https://docs.aws.amazon.com/appsync/latest/devguide/security.html#aws-iam-authorization
To be crystal clear, this means that you can have unauthenticated access which the IAM policy for that role can just do one thing - like run queries only. Then the other authenticated role (which could be when users log in or from a Lambda function) can do more like perform mutations. In the link I posted above note that the
Resourcein the policy is set on a type in your schema like so:So you can use this to set appropriate values in the
../types/XXXXXXdefinition.@aireater I would suggest you open up a new issue with details on the CLI repo as your question seems to be related to something else with your API deployment.
I went with exactly this approach (IAM) and gave this whole lib up.
https://github.com/aws-amplify/amplify-js/issues/2363#issuecomment-449011422
Can I recommend that there be some way to call specific queries without requiring a token then? User Pool groups seem like a perfect way to authenticate. They are simple to reason about, analogous to non AWS techniques for grouping users, and powerful. Additionally, I think group permissions will become the “happy path” once you guys integrate Facebook, Google, and other logins into user pools without requiring using the Hosted UI.
I couldn’t get the IAM method to work even when I gave the unauthenticated role full permissions as specified by this example taken from the documentation you linked:
I don’t think learning IAM should be a requirement for what I’m trying to do so for now I’ll go with the API_KEY method, and see where you guys take the library in the future.
[edited] - found the answer
I ran into the same issue, which is really blocking in a real use of amplify to build an app. It make me really question the choice we made of using amplify… I just tried the proposal given above in order to allow unauthenticated users to run a mutation with the following : update: worth to mention that the
MutationNamebellow can be found in the aws console > AppSync > schema > find the mutation type and look at the keys of the object.Here is my JS - I had an error here when calling
graphqlOperation, I had updated the code with the correction :And I always get a GraphQLError : “Request failed with status code 401”. => now this is ok
@undefobj Hey man, again, thanks for taking the time to respond to my questions. I’m not sure if we’re on the same page here (i hope i’m not missing something) 😃
Basically, i got that working (allowing access to unauthenticated and authenticated users by using AWS_IAM auth mode). However, i need to have different permissions for the authenticated users e.g. admin, author, user etc. and i’m trying to do that by putting them in a group, based on which i’ll allow or deny access to specific queries and mutations.
The thing is (like you said here) that user pool groups are not available when using AWS_IAM auth mode (i can’t access them in the resolver).
So my question is, how should i go about solving this issue?
Hi @reggie3 thanks for your feedback. When you specify Amazon Cognito User Pool as the authorization type on your AppSync API, when you try to call the API it always need a jwt to authenticate. When you don’t have directives on your query like you mention, it means that any user of the User Pool has access to the query, it doesn’t mean that the API is open to everyone.
For the use case you have I can suggest you to create an Amazon Cognito Identity Pool. Identity pools can have configured an unauth an auth roles like the case you mention unauth role (No signed in user).
To configure IAM permissions you can take a look here.
Please let me know how it goes
@dabit3 what’s the process for adding another auth type to an existing graphql api (cognito). Can be done using the latest Amplify CLI or only the AppSync console?
Another issue with this setup that happens to me : when running
amplify push, the aws-exports.js file is overwritten and theaws_appsync_authenticationTypeswitch back to AMAZON_COGNITO_USER_POOLS, resulting in 403 errors. It’s quite annoying, but is it a bug or a feature ?@mkaschke It’s working now. I can get a unauthenticated user to run a graphql mutation using this setup
All - Please see @dabit3 instructions above or the sample we posted here: https://github.com/dabit3/appsync-auth-and-unauth
As mentioned before we are looking to make this easier with CLI tooling in the following RFC: https://github.com/aws-amplify/amplify-cli/issues/766 For the short term hopefully this helps you get up and running.
@undefobj An example of setting up two separate APIs would be very helpful. 🙂
I’m running into the same trouble and the workaround I’m currently using is to have 2 APIs, one using API_KEY and the other using AMAZON_COGNITO_USER_POOLS. The first one is mostly used on the server side and the later is on the client side, where a pre-created guest account is used and logged in to enable public features.
@mkaschke As far as i know, there isn’t. You’ll have to either use AWS_IAM auth mode (check this comment) or have two API’s (one public, one private). But even then, there are far too many issues e.g. there’s no docs on how to configure two different apollo profiles based on the user’s state (see this comment), you can’t access user pool groups (see this comment) which is very important etc. My conclusion at this point is that AppSync paired with Cognito is garbage. Yes, you can build very simple apps, quickly. But, if your intention is to build complex, real world application, run away.
@joebernard
I haven’t tested it, but something like this might work and help you https://codesandbox.io/s/25l7or15r
The idea is to wrap the
<ApolloProvider>in a component that passes a different client depending on the auth state. Note that you might need to cleat the apollo client cache when swapping clients.@dimitarrusev unfortunately the Cognito User Pool groups are not available when using IAM.
@undefobj How can i get access to the user’s groups when using AWS_IAM auth mode?
@undefobj If I am using this method like you described with Cognito Identity and IAM permissions would I be able to use the user context within the resolvers to have fine grained access control?
Right now, I’m leaning towards creating a separate table in DynamoDB to track user specific information using the user pool users’ “sub” attribute or Cognito IAM ID as an ID for each user in that table, and keeping track of each user’s groups there.
Unfortunately, that would still mean that I couldn’t take advantage of the group directive in the AppSync schema. I’d work around that by using Lambdas to do the authorization and resolving on the queries that I’d want to restrict.