We continue the authentication track by adding Login with Amazon (LWA) to our iOS Swift app. In this two-part series, we are going to add LWA to an iOS app, allowing your users to authenticate using their Amazon account. LWA is a very powerful experience to provide as a developer because you are leveraging a brand 200 million customer’s trust while getting something back, like their name, email address, and zip code to build more personalized experiences.

Collecting user information like name, email, phone number, or age is one of the top reasons why mobile developers add social login or 3rd party login providers into their app.

When developers collect information about their users, they can start engaging with those users. It’s a win/win, the user can quickly and easily sign-in with a known provider and credentials and you get to collect a little bit of information about them 😊.

Compare this with users of your app that do not login. You know nothing about them personally except for their app activity and basic demographics.

So, this is part 1 of 2 for adding Login with Amazon to your iOS app. This first article will focus on registering and creating a project with LWA and then downloading and implementing the LWA SDK into an iOS Swift app. Part 2 will focus on federating LWA with Amazon Cognito to obtain temporary credentials for app users to securely access your AWS resources. Part 2 is similar to how we handled the social login providers, however, LWA is not natively supported by the AWS Mobile SDK AWSMobileClient that did all the heavy lifting for us when implementing Facebook and Google.

In my previous Google Sign-In and Facebook Login integration articles, the user simply chose the Sign in with Google or Continue with Facebook button in your app and authenticated using their Google or Facebook credentials, respectively, all while staying within your app flow. Login with Amazon is the same interaction from the user perspective.

Note: Your app can support Facebook Login, Google Sing-In, and Login with Amazon at the same time. You’re not limited to only one provider.

Let’s get started!

Part I — Amazon Developer and LWA Setup

Register for Login with Amazon

1. Login to the Amazon Developer portal: https://developer.amazon.com/login-with-amazon

2. Click on Sign up for a developer account under Get Started. Or click here.

3. Click on Create a New Security Profile.

4. Complete the Security Profile Management form by providing a Security Profile Name and Security Profile Description as shown below.

FYI: The Security Profile Name is the name that appears in the consent screen shown to the mobile users.

Add your iOS App Identifier to the LWA Security Profile

5. Click on the cog wheel for your security profile and select iOS Settings

6. Under the iOS Settings tab, select Add an API Key button.

7. Provide an API Key Name and Bundle Id. The Bundle Id needs to match your iOS project bundle identifier. e.g. com.domain.appname

8. Select Show under API Key to show the HUGE API key along with the API Key Name and Bundle Id. COPY the API Key and save for later. We’ll use this in our iOS project Info.plist file in a few minutes.

Part II — iOS App

Clone this Login with Amazon iOS starter project. This is a base project with the Login with Amazon SDK already added with a basic Swift proxy for interacting with the Objective-C SDK.

Add your LWA iOS API Key to Info.plist

1. Copy the API key from Step #8 above. If you forgot to copy and need to reference it again, click here and choose Show Client ID and Client Secret for your security profile name.

2. Within your Xcode project, select the Info.plist

3. Add a new key called APIKey of type String and paste in the iOS API key into the value as shown.

Add a URL Scheme to your Info.plist

Within your Xcode project, select your target project > Info and then expand the URL Types (0) Click on the ‘+’ to create new URL Type In the Identifier field, type in your iOS app identifier In the URL Schemes field, enter your app identifier but make sure you add the amzn- prefix. e.g. amzn-YOURAPPIDENTIFIER

Adding URL Schemes for Login with Amazon

Initializing the LWA Library in the AppDelegate

In the AppDelegate.swift file we did the following:

· Add import LoginWithAmazon

· Add the open url function

We added the LoginWithAmazonProxy.swift class to our Xcode project.

LoginWithAmazonProxy.swift gist code

We overwrote the default ViewController.Swift with the following:

Modify the ViewController.swift file to implement AIAuthenticationDelegate and add the required functions requestDidSucceed, requestDidFail, and the login button activity.

Main ViewController for handling Login with Amazon auth flow

Run the App and test Sign-on flow

Typical user experience flow for Login with Amazon using Single Sign-on flow:

Step 1: User taps Login with Amazon Button

Step 2: The Amazon Login Screen (user authenticates using Amazon credentials)

Step 3: The Consent Screen (this screen is skipped if you don’t require any user details but what is the fun in that?)

After a customer has completed the login and consent flow, they are automatically redirected back to your iOS app. You can retrieve the Amazon.com user profile (name, email, and zip code) at any time after they have authenticated.

Final Thoughts

With over 200 million Amazon accounts, using Login with Amazon is another great 3rd party login provider to offer your users. Remember, the biggest benefit of 3rd party login provider integration for a developer is the ability to collect user information. The other benefits the user as they don’t need to remember yet another username/password.

Now that we have authenticated our users via Login with Amazon (LWA), we can now integrate LWA with Amazon Cognito to give our mobile users temporary, limited AWS credentials to call our AWS resources. Stay tuned for part 2 where we continue using LWA, cloud enabling our app using AWS Amplify and add federated login using Amazon Cognito.