Menu

In my last post, I introduced the basic form of authentication, hooked onto a button and using the default UI. The default UI does not include any branding. When you are creating your own app, you will want to use your backgrounds, colors, fonts and logos. You will want to create your own Authenticator component that deals with this for you. Fortunately, AWS Amplify makes this simple.

Authentication follows a state diagram. You start unauthenticated and sitting at the sign-in screen. As you perform authentication operations (sign-in, submit-mfa, sign-up, confirm-signup, etc.), you transition from one state to another, gathering and mutating the authentication data and communicating with Amazon Cognito along the way. The Authenticator component is a state management system that displays the right component based on the state and allows the underlying component to sign-in:

In this particular flow matrix (which models the requirements of user pools in Amazon Cognito), we need to model three different screens – SignIn, SignUp and ForgotPassword. Each one is passed the current state and the current authentication data blob (which is made up of return values from Amazon Cognito).

The SignUp Component

Each flow has a main screen where it asks for information and then submits it to the backend, and a potential confirmation – a token that is sent via email or text message, which I will gather with a modal. Let’s look at the event handlers for the SignUp component:

The event handlers handle submission of each form – one on the main page and one on the modal for gathering the confirmation token. The magic in this is actually one line – line 68 does all the submission to Amazon Cognito and returns (asynchronously) the result from Amazon Cognito. It handles all the request signing and validation necessary to perform the action. The request signing is done in native code on React Native. Similarly, line 84 submits the confirmation token to Amazon Cognito and returns success or failure.

This is a form that asks the user to enter the data. I’ve done a little bit of clean-up by forcing lower case on the username, but nothing major. There is certainly a lot pre-submission validation and clean-up you could do. For instance, the phone number must be in “international” format. If I am in the US (which I am), I don’t generally enter phone numbers that way. I could use the google-libphonenumber package to validate the phone number and provide type-ahead formatting so that it is easier to enter for the user. There are components that wrap that library to make it easier.

I have used some special components. The Wrapper component places the UI over a background image with the logo top and center. IconTextInput is a TextInput component with an icon (using react-native-vector-icons) by the side, and ModalTokenInput is a modal dialog (using react-native-modal) wrapping the form. The Button is from react-native-elements. It’s almost always a good idea to composite components based on existing work – you don’t have to invent everything yourself.

When the user is shown the SignUp component, he/she will fill in the form and click the submit button, which then calls the onSignUp event handler. That submits the data to Amazon Cognito, then brings up the modal to gather the token input. When the token input is successfully submitted, the component uses the passed in onAuthStateChange property to change the auth state back to sign-in so the user can log in with the new credentials.

The SignIn Component

The SignIn component, UI-wise, is very similar, so I’m only going to concentrate on the event handlers:

Again, we have two important lines. One initiates the sign-in process to request a session from Amazon Cognito. The other deals with multi-factor authentication. The user object contains a lot of interesting data. Here is an example (with some stuff cut out):

In the onSignin() event handler, I’m keying off whether there is a valid user session. However, I should probably key off if there is a challengeName included in the response, as that is only present if an MFA prompt is required.

One other point is that the SignIn screen is the first screen shown in the authenticator, so you need to ensure you add links to the sign-up and forgot-password pages within the UI.

Other things you can do

With a custom Authenticator like this, there are a number of things you can do. Some of my favorites:

Store the username in AsyncStorage for a “remember me” functionality.

Add TouchID or FaceID support for storing credentials.

Add error checking for username, password, and phone number.

Ask for additional custom attributes that are submitted via Auth.signUp.

4 thoughts

Thank you so much for taking the time to explain all of this. Is there any chance you could release a git repo with the code applied somehow? I’m having a little trouble figuring out where all of this fits in in my current RN project. Login works perfectly with the default settings but I would very much like to at least change the UI. Do I have to do all of the session state handling and re-create the JSX just to modify the font size or the color of the buttons, for example? Again, thank you for these blog posts, they are much appreciated!