How to fix your Amplify app when you delete an AWS Cognito user but forget to log out first.. ๐Ÿ˜…

ยท

2 min read

So I had been testing some AWS Cognito triggers for a project. I was deleting users to then re-sign up the user and test further.

My Next.js app suddenly went into meltdown with this endless loop maxing out requests to Cognito.

I thought, OK, I've left the users auth cookies on the browser so just delete the cookies.

Nope!

I then tried to call Auth.signOut() before my auth code. This... also didn't work. I was getting UserNotFoundException returned.

Hmm. After further digging around on the aws-amplify/amplify-js repo issues, I found this issue: github.com/aws-amplify/amplify-js/issues/2301

await Auth._storage.clear()

Yes!! This finally solved this endless loop of death for me. Calling this, before any of your auth code, will do the trick. Since AWS Amplify appears to be using local storage to store the users' session.

Just remember to sign out before you start deleting users!

If you're reading this then I've managed to include enough keywords for you to find this post on the interwebs!