Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the "Created" time for a Cognito user with AWS Amplify (or aws-sdk)?

I have a React Native mobile app in which I'm using AWS Amplify to work with my Cognito user pool.

I am trying to get the "Created" date of the currently signed in user because I want to show existing users an alert when the app starts, but I don't want new users to get that alert. I plan on using the creation date of the user to know if they should see the alert.

When looking at my user pool in the Cognito service in AWS Console, I see that each user does have a "Created" property:

enter image description here

however, I am unsure how I can access that property from within the app. The only relevant method I could find in the docs was Auth.currentAuthenticatedUser(), but that doesn't seem to include the "Created" timestamp in its response.

I did find a reference to AdminGetUser in the Cognito docs which does include a UserCreateDate property, however I don't want to have admin credentials used within the app itself, so I am looking for an alternative.

Any suggestions would be appreciated!

like image 246
Saad Avatar asked Apr 18 '19 14:04

Saad


People also ask

How do you use amplify with Cognito?

Import existing Amazon Cognito resources into your Amplify project. Get started by running amplify import auth command to search for & import an existing Cognito User Pool & Identity Pool in your account. The amplify import auth command will: automatically populate your Amplify Library configuration files (aws-exports.

What is Mau in AWS Cognito?

If you are using Cognito Identity to create a User Pool, you pay based on your monthly active users (MAUs) only. A user is counted as a MAU if, within a calendar month, there is an identity operation related to that user, such as sign-up, sign-in, token refresh, password change, or a user account attribute is updated.

How long do AWS Cognito tokens last?

By default, Amazon Cognito refresh tokens expire 30 days after a user signs in to a user pool. When you create an app, you can set the app's refresh token expiration to any value between 60 minutes and 10 years.


1 Answers

There is a way that you can use the AdminGetUser API without providing direct admin access to the users. Since you are using Amplify, you can publish an API (either REST or GraphQL) that calls a Lambda function. The Lambda function in turn is configured with the required cognito-idp:AdminGetUser privilege for reading the user data and then returning it through the api.

like image 177
matsev Avatar answered Oct 13 '22 05:10

matsev