Reading timeยท4 min
To get the identityId
of a Cognito user in a Lambda function we have to call
the getId()
method on the CognitoIdentity
class.
Let's look at the complete code of a helper method, which retrieves and returns
the identityId
of a Cognito user.
import AWS from 'aws-sdk'; export function getCognitoIdentityId( jwtToken: string, ): Promise<string> | never { const params = getCognitoIdentityIdParams(jwtToken); const cognitoIdentity = new AWS.CognitoIdentity(); // ๐ get and return the identityId return cognitoIdentity .getId(params) .promise() .then(data => { if (data.IdentityId) { return data.IdentityId; } throw new Error('Invalid authorization token.'); }); } // ๐ construct the parameters for the getId method function getCognitoIdentityIdParams(jwtToken: string) { const { USER_POOL_ID, ACCOUNT_ID, IDENTITY_POOL_ID, AWS_DEFAULT_REGION, } = process.env; const loginsKey = `cognito-idp.${AWS_DEFAULT_REGION}.amazonaws.com/${USER_POOL_ID}`; return { IdentityPoolId: IDENTITY_POOL_ID, AccountId: ACCOUNT_ID, Logins: { [loginsKey]: jwtToken, }, }; }
Let's go over the code snippet.
We have created a getCognitoIdentityId
function, which:
identityId
of a Cognito userjwtToken
of the user making the request, most
commonly sent via the Authorization
HTTP HeadergetId
method on the CognitoIdentity
class via the aws-sdk
We have also created a getCognitoIdentityParams
function, which is responsible
for constructing the parameters the getId
method takes.
In this implementation, the USER_POOL_ID
, IDENTITY_POOL_ID
and ACCOUNT_ID
values have been passed to the lambda as environment variables. The
AWS_DEFAULT_REGION
value would also have to be replaced with the region your
User Pool is deployed in.
Authorization
header. Based on the JWT token the Cognito service is able to return the user's identityId
.To test the getCognitoIdentityId
function, I've created a simple CDK stack
that provisions:
Clone the GitHub repository
Install the dependencies
npm install
npx aws-cdk deploy \ --outputs-file ./cdk-outputs.json
Let's test the lambda function that gets the identityId
of a Cognito user.
To test the flow, we have to:
cdk-outputs.json
file in the root directory of your project. Alternatively, you can grab them using the AWS Console.aws cognito-idp sign-up \ --client-id YOUR_USER_POOL_CLIENT_ID \ --username "test@test.com" \ --password "password123"
aws cognito-idp admin-confirm-sign-up \ --user-pool-id YOUR_USER_POOL_ID \ --username "test@test.com"
At this point if you look at the Cognito User Pool, you would see that the user is confirmed and ready to sign in:
aws cognito-idp initiate-auth \ --auth-flow USER_PASSWORD_AUTH \ --auth-parameters \ USERNAME="test@test.com",PASSWORD="password123" \ --client-id YOUR_USER_POOL_CLIENT_ID
You will get a verbose response because of the length of the tokens. We only
care about the IdToken
, so copy and paste it into a notepad because we will
need it.
IdToken
, not the Access or Refresh Tokens.identityId
. You can find the API URL in the
cdk-outputs.json
file in the root directory, or by opening the API gateway
console.curl --location --request GET 'YOUR_API_URL/cognitoid' \ --header 'Authorization: YOUR_ID_TOKEN'
The response should look as follows.
{"identityId": "eu-central-1:5b117ba7-3a85-4fa4-a6c9-a8415d7ccdc8"}
At this point, we know that we were able to get a Cognito user's identity id in our lambda function.
To delete the provisioned resources, run the destroy
command:
npx aws-cdk destroy
We are able to access the Cognito user's identityId
by calling the
CognitoIdentity().getId
method.
Based on the JWT token that was sent in the Authorization header, Cognito will
determine the user's identityId
.
You can learn more about the related topics by checking out the following tutorials: