This post is also available on the blog of my software company, Go Tripod.
Amazon’s cloud offering, AWS, provides a wealth of resources for developers and infrastructure engineers. In this article, I’ll show how Amazon Cognito can be used to record user preferences and data without having to set up a server or database. I’ll build a small React Native application which allows a user to log in with Facebook and save some data to the cloud for later retrieval.
You may need to perform some other steps here, as per the Facebook documentation. Go and have a read of that section just in case. Let’s install the pods and then we’ll have to faff around in Xcode for a bit.
Notice that we now use the xcworkspace file rather than the xcodeproj file; this is required for CocoaPods to do its thing.
Expand the FacebookAwsReactNative > Libraries folder and select all of the items in there and delete the references to them. This is because we’ve opted to use CocoaPods for React Native (see the pod 'React' line we added to the Podfile) so the items in Libraries are duplicates.
Wonderful. Run the project and get your simulator running because we’re now ready to add our login button.
We now need to add onLoginFinished and onLogoutFinished methods to our FacebookAwsReactNative class.
Less simple, but pretty self-explanatory. At this point, you should be able to run the app and login via Facebook, with the various alert messages firing as appropriate.
But we want to take one more step; we want to be able to link this login to persistent data stored on Amazon Cognito. This needs two steps: firstly to get a unique token to identify the Facebook login, and secondly, to sync local data up to Cognito. Fortunately this becomes super-easy thanks to the Amazon SDK and the React Native Cognito project.
Next, we initialise the credentials for Cognito and sync up some data!
You’ll need to have created a Federated Identity Pool and set up Facebook as an Authentication Provider. Populate the awsIdentityPoolId and awsRegion variables accordingly and the above code can be run at any point after a successful login.
Visit the Identity Pool in the AWS Console and you’ll see a dataset called “testDataset” with a single key/value entry.
This has been a whistlestop tour on moving to a serverless setup with Facebook logins and Amazon Cognito. The full code can be found on GitHub.