This sample code demonstrates using Twitter Digits to enable phone-based authentication, and using the AWS platform -- Cognito, Lambda and DynamoDB -- to provide simple and scalable back-end services for your mobile application.
Once you have this sample running, you can imagine other possible phone-based use cases, including:
- Deliver notifications or offers to user via Amazon SMS
- Connect with existing CRM solution to pre-populate user experience (Frequent Flyer status, Rewards memberships, etc.)
- Track in-app activity for personalization or A/B testing
- Protect against bot/spam abuse through phone verification
The requirements needed to use this sample code are:
- Download and install Android Studio (http://developer.android.com/tools/studio/index.html)
- Download and install Fabric (https://fabric.io)
- Amazon Web Services account (http://aws.amazon.com/)
Follow the below steps to setup the Android App and the back-end AWS services:
- Set up Digits for Android App (details below)
- Set up AWS Cognito (details below)
- Create an AWS Dynamo instance (details below)
As a final note, ensure that all AWS instances are located in US-East, so that they can make full use of Cognito and can connect to one another.
- Via Fabric, install the Digits Kit into your Android app
- Log into your Fabric dashboard (https://fabric.io/dashboard)
- Select your app from the top drop-down and then click on the Digits icon to the left
- Your key/secret should appear on the page to the right
Once you have your token/secret, you want to add them into a properties file that the app reads. Copy the app.properties.template
file into app.properties
and add them under TWITTER_KEY and TWITTER_SECRET.
In order to get your access key and secret for the HTML example, follow the below instructions:
- Log into the Amazon Cognito console (https://console.aws.amazon.com/cognito)
- Click on "Create new Identity pool"
- Specify a name for the pool and also specify your Twitter key/secret
Below is a screenshot of the identity pool creation page, for your reference:
You'll now want to add this into the app.properties
file in the AWS_IDENTITY_POOL_ID property.
To store and retrieve records on the server, the Lambda service connects to a DynamoDB instance. To easily create a store, follow the below instrutions:
- Log into the Amazon DynamoDB console (https://console.aws.amazon.com/dynamodb)
- Click on the "Create Table" button
- Specify the table name as "digits-with-lambda"
- Specify the Primary Key Type as "Hash" with Hash Name as "phoneNumber" and String type
After your app is fully deployed and running, you can also view the saved entries via the "Explore Table" button on the DynamoDB console. You should see records that look like the following:
To create a scalable and simple Lambda service for this code, following the below steps:
- Log into the Amazon Lambda console (https://console.aws.amazon.com/lambda)
- Click on the "Create a Lambda function" button
- Specify the Name as "digitsLogin"
- Specify the Runtime to be Node.js
- Copy the code from
lambda/server.js
into the code area on the page - Specify the Handler as "index.handler"
- For Role, choose a "Basic with Dynamo" role (you may need to create a new one)
- Save the service
To test that your service works properly, you can open the digitsLogin detail page and use the "Sample event" + Invoke button to execute the code. The sample JSON for testing is located in lambda/server_event.json
file. The test area should like the below:
The following documents serve as additional information on the Amazon platform and Twitter Digits.