amplify-js: List of Cognito authentication error codes and messages

Is this related to a new or existing framework?

No response

Is this related to a new or existing API?

Authentication

Is this related to another service?

Cognito

Describe the feature you’d like to request

I would like to be able to find a list of common Cognito authentication error codes and messages so that I could provide a mapping for translation purposes.

Describe the solution you’d like

Either one of the following would be ideal:

OR/AND

  • Documentation article which provides this list of error codes/messages

So that I could do something like this:

try {
  await Auth.signIn(email, password);
} catch(error) {
  const { code, message }: { code: AuthSignInErrorCode | undefined, message: AuthSignInErrorMessage | undefined } = error;
  switch(code) {
    case AuthSignInErrorCode.USER_NOT_CONFIRMED: {
      // do something if user is not confirmed
    }
  }
}

Describe alternatives you’ve considered

I have considered encountering the errors myself manually and creating my own types, however I’m not aware of each of the errors

Additional context

No response

Is this something that you’d be interested in working on?

  • 👋 I may be able to implement this feature request
  • ⚠️ This feature might incur a breaking change

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 22
  • Comments: 26 (3 by maintainers)

Most upvoted comments

I ended up with a hook returning keyed object like that, here’s an example:

export const useAuthErrors = () => {
  const { t } = useTranslation('auth')

  const errorMap: Record<
    string,
    {
      message: string
      action: null | ((email?: string) => void)
    }
  > = {
    /** Thrown by amplify when email or password doesn't match */
    /** FIXME: The same exception is returned when you try to confirm already confirmed account */
    NotAuthorizedException: {
      message: t('validator.accountCredentialsInvalid'),
      action:() => {
        ...perform an action (optional) like a redirect or displaying toast
      },
    },
  }

  return {
    errorMap,
  }
}

This way I am able to translate the errors easily and control what will be displayed to the user + side effects

@joelzwarrington , I would be interested to know what authentication service you switched to. I’m working with amplify but it’s to much hassle for the lack of support / lock-in. Handle errors feels fundamental to authentication. It doesn’t look like v6 will be out anytime soon either.

I’m sure I’ll add one more comment to the https://github.com/aws-amplify/amplify-js/issues/11113 thread. Thank you very much for your quickly answering.

This will greatly improve development for people in LATAM. It will help build more user-friendly interfaces.

Have a nice week @cwomack

Hello, @IamCocoDev and thanks for the feedback. Would you mind leaving your comments on our TypeScript Improvements RFC where feature requests and improvements are being consolidated for Amplify JS?

Thank you!

Just checked out the linked RFC, and it looks great.

Hi @abdulramonjemil, we are currently working on improving our typescript support for the library, and I recommend that you take a look at the Typescript Improvements RFC.

As part of the initiative, the plan is to throw AuthError which will map to the service errors, and you will not need to create ENUMs.

If you have any comments or feedback, please provide them on the Linked RFC above. Thanks!

Something definitely has to be done about this. I also had to create my own enum based on the errors returned by the underlying cognito APIs

@joelzwarrington woops, I skimmed this thread in a rush (it’s been a long day), apologies.

That did the trick for now, thank you very much. 😄

@mkbctrl I’ve sent you an email!

Thanks for the quickest reply ever 🍻 great input!

Because of the feedback from the Amplify team who didn't understand the ask/problem

this is worrying… I would imagine this would add a lot of value to anyone implementing custom registration/login/reset views where designers have their own requirements.

I will have a look at your old PR, do some research on my own, maybe this time this issue will have more luck. Let’s see how it goes, I will keep you posted