Gravid Banner

Using AWS Amplify to authenticate a Flutter App

I recently needed to create a new app which requires user authentication and a data backend. Rather than build this for scratch I decided to use an existing authentication provider.

Firebase or Amplify?

There are two main contenders for this, Google’s Firebase and Amazon AWS. I did a bit of reading around. The general consensus seems to be that Firebase is easier to set up and better documented, but that AWS scales better and may be cheaper with large installed base. Firebase seems to be a logical choice for Flutter as both are Google products and should play well together.

Ultimately we chose to evaluate AWS simply because we have an existing investment and quite a lot of resources already in the AWS environment.

Getting the right version of Node to run Amplify

My initial thought was to use AWS Cognito – but in my research I realised that AWS Amplify has Cognito and DynamoDB built in and is supposed to be an easy entry point. There is also a relatively comprehensive guide to using AWS Amplify with Flutter.

The quickstart instructions are a good place to start and once I had Amplify installed properly I found them to be easy to follow and they worked as advertised. The biggest issue I had was getting the right version of Node JS on my computer to install Amplify in the first place

When I first installed the Amplify CLI it appeared to be successful but the CLI did nothing. That is to say typing any amplify command at the command prompt gave no output at all (no data and no error) – similar to this Stack Overflow. The solution in the end was to completely remove Node and reinstall it

Once I had installed Node I tried again, and this time I got the error.

TypeError: localStorage.getItem is not a function

The issue here was that I had installed Node v25, and Amplify requires an even numbered (LTS) Node installation. See this issue on Github.

Once I had the right version of Node installed, the rest of the quickstart work as identified. It sets up the AWS Cognito User pool and creates the appropriate security policies for you. It has the potential to save quite a lot of work. It is, however, worth reading through the whole document and paying particular attention to the section on Authentication as this allows you to be clear about who can and cannot view, and update your data.

It is also worth noting that at the time of writing many of the non-Amazon blog articles about Amplify are for Gen 1 which works slightly differently to the Gen 2 approach given in the Amazon documentation.

Using DynamoDB with Amplify

The Amazon DataStore used by Amplify is stored in DynamoDB – you can use the AWS Console to look at the tables. For simple usage, the examples given in the quickstart may suffice, however, the List operation which is supported to retrieve multiple rows of data uses DynamoDB’s Scan operation which pulls back the entire table before applying the filter. This can be costly both in terms of finance and bandwidth. If you (as I did) wish to use DynamoDB’s Query operation you cannot currently use the Datastore, but will need to connect to an external DynamoDB table.

The documentation for this section is not complete, so we will look at how to do this in more detail in the next article.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *