Analyzing the Ionic AWS Full-Stack Starter: The Features

In this tutorial we will see how the application makes good use of the Providers we analyzed previously.

The application is divided in two parts:

  1. The authentication process
  2. The tasks manipulation

Many intermediate Ionic concepts are present here, if you have any issue, you can have a look at my book to quickly update your knowledge.

Authentication

Starting with the login.html file:

Two interesting parts here.

Either the user doesn’t have an account and goes to the signup page or he enters his credentials, which will be stocked in the loginDetails property.

Note that, even though there is no:

The button will trigger the form submission.

Moving to the login.ts file:

Aside from the classic imports, we have our User Provider that will be used to log our user, followed by some Pages:

  1. Tabs: Where the secure content is available
  2. Signup: That will register the user
  3. Confirm: If the user hasn’t yet confirmed his registration code

Finally the LoadingController that will be used to show a loader.

The logic of the authentication process is simple:

  1. Show the loader
  2. Register, confirm or login
  3. Hide the loader

Having a quick look at the LoginPage:

The login details are cleanly formatted by the LoginDetails Class.

This property is then used once the user taps on login:

We will frequently encounter this in the application, the loadingCtrl will show the loader until the end of the process.

The loginDetails are then passed to the User Provider to log the user. Three cases here:

  1. There are no errors so the user is confirmed and we show the TabsPage
  2. There is an error because the user has not entered his registration code so we move to the ConfirmPage while passing the username
  3. There is a different error and we do nothing. If we want to, we can give some feedback to the user, otherwise he won’t understand that there is something wrong with the credentials

The signup.html file is pretty similar to the login one:

The data are stocked in the userDetails property and if there is an error (ex: weak password), the error message is displayed at the top.

Moving to the signup method in the signup.ts file:

Once again, using the User Provider we register the user using the userDetails property.
The username and the password are credentials whereas the email is an attribute.
If everything is good, the user accesses the ConfirmPage with the username as parameter.

The confirm.html file:

One form that uses the code property and a link to resend the registration code.

As you can expect, the confirm.ts file has two methods, however let’s focus on the initialization first:

There we grab the username that was passed as a NavParam.

The two useful methods:

Both methods use the User Service’s methods to do the work.
As for confirm, it will move the user to the LoginPage once the code is validated.

The logout is very simple.

As in many applications nowadays, it’s located in the SettingsPage. Here is the code from the settings.ts file:

Let’s now focus on some Ionic Native feature, this feature only works on a device so no need to try it in the browser.

The Account

It starts in the account.html file:

If there is an image, it will be used as background image in an area. If not, the user can upload it by using its camera through the selectAvatar method.

Which leads us to the account.ts file:

The Ionic Native Camera plugin is imported there.

Subscribe to my mailing list to get access to a Free 7-day Ionic 3 introduction email course!
100% Privacy. I'll never spam you!

The initialization is very important:

Many properties there:

  1. s3: The AWS S3 service
  2. avatarPhoto: Will contain the url to our photo
  3. selectedPhoto: Will contain the Blob that will be stocked in the AWS Cloud
  4. attributes: The user information (name, email, etc)
  5. sub: Will contain AWS credentials identity ID

Once again, the self variable is required to keep the this object.

First important thing in the constructor: the AWS S3 initialization (bucket, region). We then set the sub using the AWS config and we acquire the user’s attributes.

Moving on to the selectAvatar method that will allow us to take a picture:

As usual with Ionic the process is simple.
We first configure some options for the camera (quality, height, width , etc.). Note that some default constants are available for use.

Finally, we use the camera object’s getPicture method.

Once the picture is taken, the imageData variable is available. The loader is shown until the end of the upcoming process: the upload.

Before uploading, the image is transformed so we can stock it.

The upload method is as follow:

If the picture is ready, we use the s3 object to upload the data, passing the following information:

  • a unique Key
  • the Body or content
  • ContentType so AWS knows how to handle it

Once the upload is good, the refreshAvatar method is used:

The getSignedUrl method will securely launch an operation: getObject.
This operation requires one important parameter: the key.

S3 then returns the url where the image is stocked and we can assign it to the avatarPhoto property.

Quite a gymnastic there, here is a summary:

Process

You might have noticed those lines:

This is not used anymore.
It’s an HTML Element that triggers the upload method, but it’s cleaner to directly use the upload method.

Moving to the last feature: the Task List.

Tasks

At the top of this page, the addTask button:

We will analyze this one later.

Followed by an <ion-refresher> Component:

When pulling down, the refreshData method will be triggered.

The following list will be updated:

Just a traditional <ion-list> of tasks, it will create many <ion-item-sliding> Elements with the task information and a delete button shown when sliding.

The tasks.ts file’s initialization is more interesting:

The ModalController is acquired, the task creation process will be located in another area: a Modal.

The content of the modal is located in the TasksCreatePage.

The TasksPage has:

  1. An items property to show the tasks
  2. The refresher we earlier talked about
  3. The tasktable that will contain the name of the DynamoDB table

Finally, the refreshTasks method is called in the constructor.

Which is as follow:

The DynamoDB Provider is used to access the document client.
We request the tasks that belong to the user by passing the userId. The results are ordered by creation date because we use the index that was declared in the aws-config.js file:

All of this is quite complicated, this generally takes one or two lines of code with MongoDB 😕.

This query returns a promise method that’s resolved with the data.
Those data are then stocked in the items property. If a refresh is in progress, it’s stopped.

Taking a step back, a Task Service could be used here, typically calling taskService.getTasks().

Let’s take a break and go easy with this small method:

Once again using the document client: the delete method.
Much simpler, we need the table’s name, the user’s ID and the task’s ID. Once the task is deleted in AWS, the item needs to be deleted locally in the application, the splice method is good enough for this job.

The last action on this page is the addTask method:

The generateId method is used to create a unique task ID. Nothing really interesting to comment on this method, let’s focus on addTask 😉.

The modalCtrl and TasksCreatePage that were imported earlier are finally used with the task’s ID.
Skipping the task creation for now, a callback is created to handle when the modal dismiss Event is triggered, this callback receives an item that will be stocked in the remote database.
The user’s ID and created date are added to this item before using the document client to put it the database.

Finally the modal is shown.

The last feature: the task creation.

Let’s start with the TypeScript file.

The tasks-create.ts file:

The isReadyToSave property can be ignored, it’s not used anywhere.
The item property will contain our new task and the isAndroid boolean will help us in the UI to make some platform specific changes.

This boolean is set in the constructor by using Ionic’s Platform is method with the ‘android’ parameter.
By default the item’s category is set to ‘Todo’, the task’s ID is retrieved from the navParams.

Two simple methods remain:

The cancel method will just use the viewCtrl to dismiss the modal and the done method will do the same, however, it will return the new task.

Moving to the tasks-create.html file:

Taking a closer look at the <ion-header> Element, we can see that the Ionic Team cared a lot about the details (as we all should in our applications).

Ionic AWS starter modal

By using the showWhen attribute. The iOS platform will only display the ‘Cancel’ label.
On the other platforms, a button using the icon-only attribute will be coupled with the X icon.
On every platforms, a tap on the button will trigger the cancel method.

Below this header, the form is displayed:

A traditional form that will put the data in the item property.
At the end of this form, the ‘Create task’ button that will trigger the done method and Voila!
We have our new task.

Conclusion

Many AWS Services covered in this application:

  • Cognito for authentication
  • DynamoDB for the NoSQL database
  • S3 for stocking the pictures

And it’s only the beginning, the AWS Mobile Hub is still in beta so we can expect new features to pop up!

The AWS sdk is a pure JavaScript library, this collaboration might lead to an Angular AWS library or an Ionic AWS plugin.
It’s very easy to create the backend services with the Mobile Hub, however, sometime those services aren’t enough and we might need some custom backend APIs.
This is possible with the Amazon API Gateway and that’s exactly what we will see in the next tutorial!

Until next time.

2 Comments

  1. alberttrotter alberttrotter
    June 12, 2017    

    i am facing issue in login.ts file at line
    import { Component } from ‘@angular/core’;
    can you please provide me the reasons???

    • Matthieu Drula Matthieu Drula
      June 12, 2017    

      Sure, what is the error?
      Does it say something like “can’t find Component” or “@angular/core”?
      Everything should be working out of the box in this project, the only issue might be npm that didn’t properly install the libraries.

Leave a Reply to Matthieu Drula Cancel reply

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

6 Shares