Find your friends using Digits and AWS

Wednesday, 7 October 2015

Digits is a great way to easily authenticate your mobile app users. One of the benefits is the “Find Your Friends,” which allows people to upload their address book contacts to find other Digits users in your app whom they already know. This alleviates the Empty Room Problem by letting them interact with their contacts from the first moment they launch your app. “Find Your Friends” works across both mobile platforms, so your iOS users can find their contacts using Android devices and vice versa.

This functionality requires a backend. There are many to choose from, including Amazon Web Services, Google App Engine, and Microsoft Azure, to name a few. I wrote a sample Swift app to demonstrate “Find Your Friends” using Amazon Cognito for managing user identity and Amazon DynamoDB for storing user data.

Among its other features, Cognito enables developers to easily give end users access to other AWS products such as DynamoDB. Amazon recently added support for Digits, and it can be enabled by simply setting your Digits (Twitter) API credentials in the identity pool settings.

Find your friends using Digits and AWS

Using AWS Identity and Access Management, you can assign policies to your identity pool’s roles. In this case, I enabled users authenticating using Digits to have access to DynamoDB:

Find your friends using Digits and AWS

Linking Cognito and Digits identities is simple:

let digitsSession = Digits.sharedInstance().session()

let cognito = AWSCognitoCredentialsProvider(regionType: AWSRegionType.USEast1, identityPoolId: "YourCognitoPoolId")

cognito.logins = [ "www.digits.com" : digitsSession.authToken + ";" + digitsSession.authTokenSecret ]

I store user data in DynamoDB using an object mapper, but you can also use Cognito Events. It’s important to note that Digits ids are stable; they will not change even if a user changes her phone number via the dashboard.

let dynamo = AWSDynamoDBObjectMapper.defaultDynamoDBObjectMapper()

let user = User()
user.cognitoId = cognito.identityId
user.digitsId = digitsSession.userID
user.phoneNumber = digitsSession.phoneNumber

dynamo.save(user).continueWithBlock({ (task) -> AnyObject! in
if (task.result != nil) {
// ...
}
return nil
})

Digits provides several methods to work with the phone’s contacts. To start the “Find Your Friends” flow, you need to call one of the

startContactsUploadWith

 methods. Variations are available to override properties such as custom appearance, title, or presenting view controller:

let contacts = DGTContacts(userSession: digitsSession)
contacts.startContactsUploadWithCompletion { result, error in
if (result != nil) {
// ...
}
}

Once a user has uploaded their contacts, Digits can lookup address book matches with other Digits users who are using the app by calling lookupContactMatchesWithCursor:completion:. Matches will return the Digits userIDs of contacts that that user has in their address book and the Digits userIDs of people that have that user in their own address book. Matched contacts will be merged and returned in a single list:

contacts.lookupContactMatchesWithCursor(nil) { matches, nextCursor, error in
if (matches != nil) {
for item in matches as! [DGTUser] {
// ...
}
}
}

“Find Your Friends” only returns other users’ Digits IDs, so you need to query the database to find the rest of the data. I’ve set digitsId as a Global Secondary Index in DynamoDB, so I can query against it by providing a matched user’s Digits ID.

let dynamo = AWSDynamoDBObjectMapper.defaultDynamoDBObjectMapper()

let queryExpression = AWSDynamoDBQueryExpression()
queryExpression.hashKeyValues = item.userId
queryExpression.hashKeyAttribute = "digitsId"
queryExpression.indexName = "digitsId-index"

dynamo.query(User.self, expression: queryExpression).continueWithBlock { (task) -> AnyObject! in
if (task.result != nil)
{
let users = task.result.items as! [User]
// ...
}
}

Once you have the rest of the data, you can create connections between your users and allow them to interact between each other. For example, you can enable users to message or send each other gifts. Digits, combined with products like Amazon Cognito and DynamoDB, enables you to easily create your own in-app social graph.

You can find my sample in a GitHub repository. If you have any questions, please ping me on Twitter.