Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save AWS Cognito Users in DynamoDB

I recently started experimenting with AWS AppSync but I had some questions around AWS Cognito.

I would like for users to be able to authenticate with Facebook but I need their profile picture, name and email as data for my public user profiles in my app. So far, I noticed Cognito integrates with Facebook Auth but it does not allow access to the user information and this info does not get saved in a DynamoDB table.

My question is, how can I create a new User in DynamoDB when Cognito receives a new sign in, or return an existing user/id when the user already exists in the db.

like image 728
Rein Van Imschoot Avatar asked Dec 18 '17 12:12

Rein Van Imschoot


People also ask

How do you add a Cognito user to DynamoDB using lambda?

Access your IAM Management console and select Roles from the left menu. Click Create role and select the AWS Service Lambda role. Once both are highlighted, click Next: Permissions. Name your role whatever you want, as long as it's recognizable to you, and click Create role.

Can you export users from Cognito?

Cognito does not allow a way to export users from a user pool.

Does Cognito store user data?

You can programmatically create a data set associated with Cognito Identity and start saving data in the form of key/value pairs. The data is stored both locally on the device and in the Cognito sync store. Cognito can also sync this data across all of the end user's devices.

What is Cognito sync trigger?

Amazon Cognito raises the Sync Trigger event when a dataset is synchronized. You can use the Sync Trigger event to take an action when a user updates data. The function can evaluate and optionally manipulate the data before it is stored in the cloud and synchronized to the user's other devices.


2 Answers

I was trying to achieve the same a few weeks ago.

After reading the docs for hours, I realised that Cognito may not help us in regards to the data that comes back from FB or how to save it.

I ended up doing the following:

(1) Using FB-SDK, pulled in the user data.

(2) Invoked a Lambda function that saved this data (like FB_id,etc) to DynamoDB.

(3) If user logged in again, their FB_id (or email) was used to check against DynamoDB entries to retrieve their data.

If Cognito is able to help us and I missed it somehow, I would love to know.

Happy Coding!

like image 126
nishkaush Avatar answered Oct 13 '22 13:10

nishkaush


You could use custom attributes and federating user from Facebook in your user pool to achieve this. Here are the steps at high level to do this.

  • You will first have to define custom attributes for the profile information you want to save in each user profile.
  • Define attribute mapping to link the custom attributes to Facebook attributes you want to save.
  • Build you application using Cognito hosted pages and federation to allow your users to log in using Facebook.

After this, on each new user log in in your app a new user is created in your user pool with all the attributes that were defined in attribute mapping and values which Cognito gets in the Facebook token. Your app will get these attribute values in the IDToken issued after authentication and you app can use these.

Additionally, if you want to store these attribute values outside of Cognito user pools profile, like your own DynamoDB table, you can configure a PreSignUp trigger in the pool which will be invoked on all new user creations. You can export the user attributes from this trigger to any database of your choice.

Hope this helps.

like image 45
Chetan Mehta Avatar answered Oct 13 '22 11:10

Chetan Mehta