question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unhandled Rejection (Error): GraphQL error: Authentication required

See original GitHub issue

Hi there,

I’ve been trying to implement authentication to a project of mine using apollo-client and apollo-server. It is really simple:

  1. on app load I fetch the user’s info from a me query
  2. if the user isn’t logged in, the server throws an AuthenticationError
  3. after login, I refetch the user’s info from the me query, to update the client’s cache
  4. on logout, I clear the cache using client.resetStore
  5. because of that, my app rerenders and tries to get the user’s info from the me query again
  6. the user isn’t logged in, the server throws an AuthenticationError
  7. but this time a see huge error screen in my React application

Looking at the dev tools, everything looks fine.

At page load, the user is not logged in. The me query returns null (and an AuthenticationError error) image

After login, I get data from the me query image

Finally, after logout, the me query returns again null (and an AuthenticationError error) image

But this time my client app breaks with this error image

I find this a bit weird because the cache at page load and after logout is the same (empty cache). And the server responses are identical, yet the client behaves differently.

My implementation is pretty similar to the one described in the official documentation (using cookies).

Does anybody have an idea why this happens?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:29
  • Comments:39 (1 by maintainers)

github_iconTop GitHub Comments

12reactions
sunhak-houtcommented, Jul 25, 2020

This temporarily helps me avoid the pain of using try, catch block.

const [login, { data, loading, error }] = useLoginMutation({ onError: () => {} });

I hope to see the solution to this problem soon. Thanks for your hard work, Apollo team!

10reactions
adildostmohamedcommented, Apr 8, 2020

not sure if this helps but i had a similar issue where throwing a AuthenticationError from Apollo-Server was causing my Apollo-Client react error page to appear due to an unhandled rejection. In my case, I wrapped my mutation in a try catch block and awaited the mutation response and it seemed to work.

IE This leads to an unhandled rejection error:

  const onSubmit = (e: React.ChangeEvent<HTMLFormElement>) => {
    e.preventDefault();
    login({ variables: { input: loginFormState } });
  };

This works and allows me to grab the error from the mutation to display on the UI

  const onSubmit = async (e: React.ChangeEvent<HTMLFormElement>) => {
    e.preventDefault();
    try {
      await login({ variables: { input: loginFormState } });
    } catch (e) {
      console.error(e);
    }
  };
Read more comments on GitHub >

github_iconTop Results From Across the Web

Unhandled Rejection (Error): GraphQL error: Not authenticated
But the error Unhandled Rejection (Error): GraphQL error: Not authenticated is thrown after reseted it with client!.resetStore()
Read more >
Handling operation errors - Apollo GraphQL Docs
The onError link can retry a failed operation based on the type of GraphQL error that's returned. For example, when using token-based authentication,...
Read more >
GraphQL error handling to the max with Typescript, codegen ...
A GraphQL API will always return a 200 OK Status Code, even in case of error. You'll get a 5xx error in case...
Read more >
Top GraphQL Errors and How to fix them
GraphQL is strongly typed and it performs validation of all queries before executing them. It can reject a query if it is invalid...
Read more >
Troubleshooting and Common Mistakes - AWS AppSync
This section discusses some common errors and how to troubleshoot them. Incorrect DynamoDB Key Mapping. If your GraphQL operation returns the following ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found